尝试更新listview时为Nullpointer

时间:2013-05-07 12:15:40

标签: android methods nullpointerexception call

在我的MainActivity类中,我有一个填充列表视图的方法listview_fuellen

public void listview_fuellen(){
        DBHelper db = new DBHelper(this);

    ListView lv = (ListView) findViewById(R.id.lvKinder);
    Cursor c = db.select();
    int count = c.getCount();
    String values[] = new String[count+1];

    List<String> auswahl = new ArrayList<String>();
    int i = 0;
    System.out.println("$$$$$$$$$$$$$$$$$$$$$$$1" + c.getCount());
    while(c.moveToNext())
    { 
        values[i]= c.getString(c.getColumnIndex("name"));
        auswahl.add(c.getString(c.getColumnIndex("name")));
        System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS" + auswahl.get(i).toString());
        i++;
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_black_text,R.id.list_content, auswahl);

    lv.setAdapter(adapter);
}

现在,在我将数据添加到listview显示的数据库的不同类中,我尝试调用此方法。

db.insert(name, zeit, geschlecht);                  
MainActivity ma = new MainActivity();
ma.listview_fuellen();

MainActivity的构造函数是默认构造函数:

public void MainActivity(){

}

现在,当我执行我的应用程序时,我的方法listview_fuellen()出现NullpointerException:

05-07 14:08:17.609: E/AndroidRuntime(25654): FATAL EXCEPTION: main
05-07 14:08:17.609: E/AndroidRuntime(25654): java.lang.NullPointerException
05-07 14:08:17.609: E/AndroidRuntime(25654):    at android.app.Activity.findViewById(Activity.java:1839)
05-07 14:08:17.609: E/AndroidRuntime(25654):    at com.example.unserekinder.MainActivity.listview_fuellen(MainActivity.java:129)
05-07 14:08:17.609: E/AndroidRuntime(25654):    at com.example.unserekinder.NeuesKind$1.onClick(NeuesKind.java:57)
05-07 14:08:17.609: E/AndroidRuntime(25654):    at android.view.View.performClick(View.java:4204)
05-07 14:08:17.609: E/AndroidRuntime(25654):    at android.view.View$PerformClick.run(View.java:17355)
05-07 14:08:17.609: E/AndroidRuntime(25654):    at android.os.Handler.handleCallback(Handler.java:725)
05-07 14:08:17.609: E/AndroidRuntime(25654):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-07 14:08:17.609: E/AndroidRuntime(25654):    at android.os.Looper.loop(Looper.java:137)
05-07 14:08:17.609: E/AndroidRuntime(25654):    at android.app.ActivityThread.main(ActivityThread.java:5041)
05-07 14:08:17.609: E/AndroidRuntime(25654):    at java.lang.reflect.Method.invokeNative(Native Method)
05-07 14:08:17.609: E/AndroidRuntime(25654):    at java.lang.reflect.Method.invoke(Method.java:511)
05-07 14:08:17.609: E/AndroidRuntime(25654):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-07 14:08:17.609: E/AndroidRuntime(25654):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-07 14:08:17.609: E/AndroidRuntime(25654):    at dalvik.system.NativeStart.main(Native Method)

有人可以告诉我为什么吗?

2 个答案:

答案 0 :(得分:1)

您不能使用new Activity()打开新活动,而是使用startActivity(new Intent(...))打开新活动。更多信息请访问:http://developer.android.com/training/basics/firstapp/starting-activity.html

答案 1 :(得分:1)

尝试阅读有关活动生命周期的信息

活动实例化和方法调用一切都将由Android通过生命周期方法处理。

将代码放在onCreate(Bundle savedinstancestate)的{​​{1}}覆盖方法中。

如果您想要导航到第二个活动,请使用Activity来执行此操作

intent