我处于某种程度,我需要一些帮助。我现在坚持这个问题几天了。 (我是Android编程的新手)
所以我在我的第一个应用程序中,想要创建一个ListView,其中填充了SQLite
数据库中的条目。
不幸的是,当我尝试设置我的适配器时,我得到了NullPointerException
。
我希望你们有一个想法,如何解决这个或暗示我可以照顾解决方案。对于这段代码,我经历了几个教程,所以我可能会错过一些重要的部分,或者做了一个相当"愚蠢的"因为我是一个完整的新手。
继承我的守则:
方法,填充ListView:
//Populates the List View
public void readDB(View view){
Log.d("ReadDB","Start");
//Read DB
Cursor myCursor = dbTimer.rawQuery("Select * from Times", null);
myCursor.moveToFirst();
//Outputs DB-Entries in Console (for checking if there are entries)
printDB(myCursor);
//Find ListView to populate
lvItems = (ListView) findViewById(R.id.listView_timer);
//Setup Cursor Adapter with the ResultSet
TimerCursorAdapter myAdapter = new TimerCursorAdapter(this, myCursor);
//Attach Cursor adapter to the ListView
lvItems.setAdapter(myAdapter);
Log.d("ReadDB","Finish");
}
适配器类
public class TimerCursorAdapter extends CursorAdapter {
public TimerCursorAdapter(Context context, Cursor cursor){
super(context, cursor, 0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent){
return LayoutInflater.from(context).inflate(R.layout.item_list_view, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor){
// Find TextFields for pupolation
TextView tvTitle = (TextView) view.findViewById(R.id.textView_title);
TextView tvCup = (TextView) view.findViewById(R.id.textView_cup);
TextView tvGram = (TextView) view.findViewById(R.id.textView_gram);
TextView tvMilliliter = (TextView) view.findViewById(R.id.textView_milliliter);
TextView tvBlooming = (TextView) view.findViewById(R.id.textView_blooming);
TextView tvSteeping = (TextView) view.findViewById(R.id.textView_steeping);
TextView tvPressing = (TextView) view.findViewById(R.id.textView_pressing);
// Extract properties from Cursor
String strTitle = cursor.getString(cursor.getColumnIndexOrThrow("Title"));
String intCup = cursor.getString(cursor.getColumnIndexOrThrow("Cup"));
String intGram = cursor.getString(cursor.getColumnIndexOrThrow("Gram"));
String intMilliliter = cursor.getString(cursor.getColumnIndexOrThrow("Milliliter"));
String intBlooming = cursor.getString(cursor.getColumnIndexOrThrow("Blooming"));
String intSteeping = cursor.getString(cursor.getColumnIndexOrThrow("Steeping"));
String intPressing = cursor.getString(cursor.getColumnIndexOrThrow("Pressing"));
//Sets TextVies in the Layout
tvTitle.setText(strTitle);
tvCup.setText(intCup);
tvGram.setText(intGram);
tvMilliliter.setText(intMilliliter);
tvBlooming.setText(intBlooming);
tvSteeping.setText(intSteeping);
tvPressing.setText(intPressing);
}
错误代码
10-06 12:03:16.064 862-862/clausing.sebastian.de.eztimer E/AndroidRuntime: FATAL EXCEPTION: main
Process: clausing.sebastian.de.eztimer, PID: 862
Theme: themes:{}
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:289)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21158)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21158)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at clausing.sebastian.de.eztimer.MainActivity.readDB(MainActivity.java:114)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21158)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)