我创建了一个方法,它给了我一个Cursor对象,我尝试使用它来填充listview,当我在onCreate中调用该方法时,或者在按钮单击它时给我NullPointerException。 表创建,我试图通过界面填充它之前我创建了Cursor函数并且正在工作,我无法理解为什么它给了我这个错误,我验证了视图和意图发送者到这个类,只有当我使用该方法会抛出此异常
06-01 18:30:43.361 3430-3430/com.example.george.myfirstaplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.george.myfirstaplication/com.example.george.myfirstaplication.Genoflexiuni}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
at `enter code here`android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.access$600(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5370)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.george.myfirstaplication.Genoflexiuni.populateListView(Genoflexiuni.java:88)
at com.example.george.myfirstaplication.Genoflexiuni.onCreate(Genoflexiuni.java:62)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1146)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315)
这是方法
public Cursor getAllGenoflexiuni(){
SQLiteDatabase sqLiteDatabase = helper.getWritableDatabase();
String[] columns = {DataBaseHelper.DATE_TIME, DataBaseHelper.GREUTATE_EXERCITIU, DataBaseHelper.NUMBER_SETURI, DataBaseHelper.NUMBER_REPETITIONS};
Cursor cursor = sqLiteDatabase.query(DataBaseHelper.GENOFLEXIUNI_TABLE, columns, null, null, null, null, null);
if(cursor != null)
{
cursor.moveToFirst();
}
return cursor;
}
这里我想用它
private String geDateTime(){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
Date date = new Date();
return dateFormat.format(date);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.genoflexiuni);
weightText=(EditText)findViewById(R.id.weightText);
setsText=(EditText)findViewById(R.id.setsText);
repetitionsText=(EditText)findViewById(R.id.repetitionsText);
insertButton = (Button)findViewById(R.id.insertButton);
listView=(ListView)findViewById(R.id.listElemente);
populateListView();
insertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String weight = weightText.getText().toString();
String sets = setsText.getText().toString();
String repetitions = repetitionsText.getText().toString();
String dateTime = geDateTime();
long id = helper.insertGenoflexiuni(dateTime, weight, sets, repetitions);
if (id > 0)
System.out.println("Succes MADAFAKAAAAA");
else
System.out.println("Failed miserably");
populateListView();
}
});
}
private void populateListView(){
Cursor cursor = helper.getAllGenoflexiuni();
String[] campuri = {HelperOwner.DataBaseHelper.DATE_TIME, HelperOwner.DataBaseHelper.GREUTATE_EXERCITIU, HelperOwner.DataBaseHelper.NUMBER_SETURI, HelperOwner.DataBaseHelper.NUMBER_REPETITIONS};
int[] id = {R.id.dateView, R.id.weightViewItem, R.id.setsViewItem, R.id.repetitionsViewItem};
SimpleCursorAdapter myCursorAdapter;
myCursorAdapter = new SimpleCursorAdapter(getBaseContext(),R.layout.inregistrare_exercitiu,cursor,campuri,id,0);
listView.setAdapter(myCursorAdapter);
}
}
答案 0 :(得分:0)
您已忘记初始化帮助实例,因为您尝试拨打getAllGenoflexiuni()
并将getBaseContext()
更改为Genoflexiuni.this
< / p>