gpt4 book ai didi

java - 填充 TableLayout 时 Android Java NullPointerException

转载 作者:行者123 更新时间:2023-12-01 12:42:00 24 4
gpt4 key购买 nike

基本上我正在尝试将数据从 SQLite 加载到 Android tableLayout 中。

DBAdapter.java,我在其中初始化了数据库名称和所有表

// TABLE INFORMATTION
public static final String TABLE_EXERCISE = "exercise";
public static final String EXERCISE_TYPE = "exerciseType";

// DATABASE INFORMATION
static final String DB_NAME = "schoolAssignment";
static final int DB_VERSION = 1;

public DBAdapter(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEMBER);
onCreate(db);
}

SQLController.java 我放置 SQL 查询的位置

private DBAdapter dbhelper;
private Context ourcontext;
private SQLiteDatabase database;

public SQLController(Context c) {
ourcontext = c;
}

public SQLController open() throws SQLException {
dbhelper = new DBAdapter(ourcontext);
database = dbhelper.getWritableDatabase();
return this;
}

public void close() {
}

public Cursor readEntry() {

String[] allColumns = new String[] { DBAdapter.EXERCISE_TYPE };

Cursor c = database.query(DBAdapter.TABLE_EXERCISE, allColumns, null,
null, null, null, null);

if (c != null) {
c.moveToFirst();
}
return c;
}

exercise.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="@+id/TableLayout"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_gravity="center">
</TableLayout>
</LinearLayout>

Exercise.java,我将数据从 SQLite 加载到 tableLayout

TableLayout table_layout;
SQLController sqlcon;

ProgressDialog PD;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhost);
table_layout = (TableLayout) findViewById(R.id.TableLayout);
BuildTable();
}

private void BuildTable() {

sqlcon.open();
Cursor c = sqlcon.readEntry();

int rows = c.getCount();
int cols = c.getColumnCount();

c.moveToFirst();

// outer for loop
for (int i = 0; i < rows; i++) {

TableRow row = new TableRow(this);
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));

// inner for loop
for (int j = 0; j < cols; j++) {

TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tv.setGravity(Gravity.CENTER);
tv.setTextSize(18);
tv.setPadding(0, 5, 0, 5);

tv.setText(c.getString(j));
row.addView(tv);

}

c.moveToNext();

table_layout.addView(row);

}
sqlcon.close();
}

但是,我在 logCat 中遇到了空指针异常:

07-29 22:11:54.424: E/AndroidRuntime(22888): FATAL EXCEPTION: main
07-29 22:11:54.424: E/AndroidRuntime(22888): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.nyp.project/edu.nyp.project.Exercise}: java.lang.NullPointerException
07-29 22:11:54.424: E/AndroidRuntime(22888): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
07-29 22:11:54.424: E/AndroidRuntime(22888): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
07-29 22:11:54.424: E/AndroidRuntime(22888): at android.app.ActivityThread.access$600(ActivityThread.java:127)
07-29 22:11:54.424: E/AndroidRuntime(22888): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
07-29 22:11:54.424: E/AndroidRuntime(22888): at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 22:11:54.424: E/AndroidRuntime(22888): at android.os.Looper.loop(Looper.java:137)
07-29 22:11:54.424: E/AndroidRuntime(22888): at android.app.ActivityThread.main(ActivityThread.java:4512)
07-29 22:11:54.424: E/AndroidRuntime(22888): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 22:11:54.424: E/AndroidRuntime(22888): at java.lang.reflect.Method.invoke(Method.java:511)
07-29 22:11:54.424: E/AndroidRuntime(22888): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
07-29 22:11:54.424: E/AndroidRuntime(22888): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
07-29 22:11:54.424: E/AndroidRuntime(22888): at dalvik.system.NativeStart.main(Native Method)
07-29 22:11:54.424: E/AndroidRuntime(22888): Caused by: java.lang.NullPointerException
07-29 22:11:54.424: E/AndroidRuntime(22888): at edu.nyp.project.Exercise.BuildTable(Exercise.java:75)
07-29 22:11:54.424: E/AndroidRuntime(22888): at edu.nyp.project.Exercise.onCreate(Exercise.java:47)
07-29 22:11:54.424: E/AndroidRuntime(22888): at android.app.Activity.performCreate(Activity.java:4465)
07-29 22:11:54.424: E/AndroidRuntime(22888): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
07-29 22:11:54.424: E/AndroidRuntime(22888): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
07-29 22:11:54.424: E/AndroidRuntime(22888): ... 11 more
07-29 22:12:03.377: I/Process(22888): Sending signal. PID: 22888 SIG: 9

我想知道我的代码的哪一部分出了问题。提前致谢。

最佳答案

sqlcon 在开始时为空。您必须在调用 sqlcon.open(); 之前对其进行初始化;

放置:

sqlcon = new SQLController(this);

关于java - 填充 TableLayout 时 Android Java NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25017553/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com