我在一个非常简单的活动中遇到了一个非常奇怪的错误。代码在
之下String s[];
ListView saved_lst;
public ListDatabaseHandler getname;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_saved);
getname=new ListDatabaseHandler(getApplicationContext());
Toast.makeText(getApplicationContext(), Integer.toString(getname.getAllName().size()), 2000).show();
int y=getname.getAllName().size();
s=new String[y];
for (int i=0;i<y/2;i++){
s[i]=getname.getAllName().get(i);
}
saved_lst=(ListView) findViewById(R.id.savedlist);
ArrayAdapter<String> saveadapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,s);
saved_lst.setAdapter(saveadapter);
}
getname.getAllName返回数据库中所有表名称的ArrayList。我已经将问题精确定位到最后一个语句saved_lst.setAdapter(saveadapter);
日志cat显示存在未捕获的异常。
这可能是什么原因导致无法运行?
Logcat
07-30 17:22:00.480: W/dalvikvm(5458): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-30 17:22:00.550: E/AndroidRuntime(5458): FATAL EXCEPTION: main
07-30 17:22:00.550: E/AndroidRuntime(5458): java.lang.NullPointerException
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.AbsListView.obtainView(AbsListView.java:2143)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.ListView.onMeasure(ListView.java:1158)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.view.View.measure(View.java:15518)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.view.View.measure(View.java:15518)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.view.View.measure(View.java:15518)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.view.View.measure(View.java:15518)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-30 17:22:00.550: E/AndroidRuntime(5458): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.view.View.measure(View.java:15518)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.view.View.measure(View.java:15518)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.view.View.measure(View.java:15518)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.view.View.measure(View.java:15518)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.TableRow.measureChildBeforeLayout(TableRow.java:247)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.TableRow.onMeasure(TableRow.java:114)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.view.View.measure(View.java:15518)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.view.View.measure(View.java:15518)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.view.View.measure(View.java:15518)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.view.View.measure(View.java:15518)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-30 17:22:00.550: E/AndroidRuntime(5458): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
07-30 17:22:00.550: E/AndroidRuntime(5458): at android.view.View.measure(View.java:15518)
XML文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/savedlist"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
getAllName代码
String name= new String();
ArrayList<String> dirArray = new ArrayList<String>();
public ArrayList<String> getAllName() {
//SQLHelper help=new SQLHelper(this, "lists.db", null, 1);
SQLiteDatabase DB = this.getReadableDatabase();
Cursor c = DB.rawQuery(
"SELECT name FROM sqlite_master WHERE type='table'", null);
c.moveToFirst();
while (!c.isAfterLast() ) {
name = c.getString(c.getColumnIndex("name"));
if (!name.equals("android_metadata") && !name.equalsIgnoreCase("temp")) {
dirArray.add(name);
}
c.moveToNext();
}
DB.close();
return dirArray;
}
答案 0 :(得分:0)
NullPointerException 是由于尝试访问空的对象而引起的。
getname = new ListDatabaseHandler(getApplicationContext());刚刚初始化,当你试图得到getname的大小,即(getname.getAllName()。size();)导致NullPointerException
更改ListDatabaseHandler类中的getAllName方法。它将提供解决方案