-1

我正在处理无法在列表项单击时开始新活动的应用程序。当我单击列表项时,应用程序崩溃并且我找不到错误。活动的代码是

public class CourierActivity extends Activity {
    DBController dbc=new DBController(this);
    Cursor c;
    SimpleCursorAdapter sca;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.courierlist);
        final ListView courierlists=(ListView) findViewById(R.id.courierview);
        dbc.open();
        c=dbc.getcouriername();
        String [] from ={"couriernames"};
        int [] to={R.id.couriertext};
        sca=new SimpleCursorAdapter(this, R.layout.courierlistrow, c, from, to);
        courierlists.setAdapter(sca);
        sca.notifyDataSetChanged();
        c.requery();
        courierlists.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                Intent i=new Intent(CourierActivity.this,ServiceActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);

            }
        });

    }

}

这是我的 logcat 输出:

08-24 07:03:54.103: E/AndroidRuntime(1377): FATAL EXCEPTION: main
08-24 07:03:54.103: E/AndroidRuntime(1377): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.datab/com.example.datab.ServiceActivity}: java.lang.NullPointerException
08-24 07:03:54.103: E/AndroidRuntime(1377):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-24 07:03:54.103: E/AndroidRuntime(1377):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-24 07:03:54.103: E/AndroidRuntime(1377):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-24 07:03:54.103: E/AndroidRuntime(1377):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-24 07:03:54.103: E/AndroidRuntime(1377):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-24 07:03:54.103: E/AndroidRuntime(1377):     at android.os.Looper.loop(Looper.java:137)
08-24 07:03:54.103: E/AndroidRuntime(1377):     at android.app.ActivityThread.main(ActivityThread.java:5041)
08-24 07:03:54.103: E/AndroidRuntime(1377):     at java.lang.reflect.Method.invokeNative(Native Method)
08-24 07:03:54.103: E/AndroidRuntime(1377):     at java.lang.reflect.Method.invoke(Method.java:511)
08-24 07:03:54.103: E/AndroidRuntime(1377):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-24 07:03:54.103: E/AndroidRuntime(1377):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-24 07:03:54.103: E/AndroidRuntime(1377):     at dalvik.system.NativeStart.main(Native Method)
08-24 07:03:54.103: E/AndroidRuntime(1377): Caused by: java.lang.NullPointerException
08-24 07:03:54.103: E/AndroidRuntime(1377):     at com.example.datab.ServiceActivity.onCreate(ServiceActivity.java:34)
08-24 07:03:54.103: E/AndroidRuntime(1377):     at android.app.Activity.performCreate(Activity.java:5104)
08-24 07:03:54.103: E/AndroidRuntime(1377):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-24 07:03:54.103: E/AndroidRuntime(1377):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-24 07:03:54.103: E/AndroidRuntime(1377):     ... 11 more
4

3 回答 3

1
listview.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub
                Intent in1 = new Intent(getBaseContext(), SecondActivity.class);
                    startActivity(in1);

                }
            });

还添加对清单文件的权限

 <activity android:name=".SecondActivity"  />
于 2013-08-24T06:42:12.313 回答
1

您可以在 listview 的 onItemClickListener 方法中编写意图代码,如下所示,这样无论您单击的 listview 的位置如何,它都会将您带到新的活动。

而且,不要忘记在清单中声明您的新活动。

listview.setOnItemClickListener(new OnItemClickListener() {

          //@Override
           public void onItemClick(AdapterView<?> parent, View view,
                  int position, long id) {

            Intent ide = new Intent(Activity1.this,Activity2.class);
            ide.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(ide);

           }
 });
于 2013-08-24T06:51:01.927 回答
0

错误点是

    Caused by: java.lang.NullPointerException
    08-24 07:03:54.103: E/AndroidRuntime(1377):     
at com.example.datab.ServiceActivity.onCreate(ServiceActivity.java:34)

检查中有空指针异常ServiceActivity line number 34,您可能正在使用任何未初始化的对象它应该可以解决您的问题

于 2014-03-05T13:22:29.630 回答