0

我想在单击 ListView 项目后开始新的活动,但应用程序崩溃了。如果我注释掉意图初始化,并且只留下 toast 消息应用程序不会崩溃。Сonsequently 问题在于 Intent。我不知道为什么会这样。任何帮助将不胜感激。

   public class MainActivity extends Activity {

        private ArrayList<Cinemas> cinema;
        private CinemasAdapter cinemaAdapter;
        private ListView list;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main); 

            cinema = new Handler().handle(); 
            cinemaAdapter = new CinemasAdapter(this, R.layout.movie_data_row, cinema);
            list = (ListView) findViewById (R.id.listing1);     
            list.setAdapter(cinemaAdapter);
            setContentView(list);

            list.setOnItemClickListener(new OnItemClickListener(){
            public void onItemClick (AdapterView<?> parent, View view, int position, long id) 
            {
                Context context = null;
                Cinemas movie = cinemaAdapter.getItem(position);
                Toast.makeText(getBaseContext(), "Click", Toast.LENGTH_LONG).show();
                Intent intent = new Intent (context,More.class);
                startActivity(intent); 
            }
            });
        }    
    }

这是logcat:

03-16 13:33:28.882: E/AndroidRuntime(2536): FATAL EXCEPTION: main
03-16 13:33:28.882: E/AndroidRuntime(2536): java.lang.NullPointerException
03-16 13:33:28.882: E/AndroidRuntime(2536):     at android.content.ComponentName.<init>(ComponentName.java:75)
03-16 13:33:28.882: E/AndroidRuntime(2536):     at android.content.Intent.<init>(Intent.java:3491)
03-16 13:33:28.882: E/AndroidRuntime(2536):     at com.example.bakumovies.MainActivity$1.onItemClick(MainActivity.java:47)
03-16 13:33:28.882: E/AndroidRuntime(2536):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
03-16 13:33:28.882: E/AndroidRuntime(2536):     at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
03-16 13:33:28.882: E/AndroidRuntime(2536):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
03-16 13:33:28.882: E/AndroidRuntime(2536):     at android.widget.AbsListView$1.run(AbsListView.java:3423)
03-16 13:33:28.882: E/AndroidRuntime(2536):     at android.os.Handler.handleCallback(Handler.java:725)
03-16 13:33:28.882: E/AndroidRuntime(2536):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-16 13:33:28.882: E/AndroidRuntime(2536):     at android.os.Looper.loop(Looper.java:137)
03-16 13:33:28.882: E/AndroidRuntime(2536):     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-16 13:33:28.882: E/AndroidRuntime(2536):     at java.lang.reflect.Method.invokeNative(Native Method)
03-16 13:33:28.882: E/AndroidRuntime(2536):     at java.lang.reflect.Method.invoke(Method.java:511)
03-16 13:33:28.882: E/AndroidRuntime(2536):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-16 13:33:28.882: E/AndroidRuntime(2536):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-16 13:33:28.882: E/AndroidRuntime(2536):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

2

您已使用 null 初始化上下文

  Context context = null;

 Intent intent = new Intent (context,More.class);

而且您从上下文开始意图,这导致NullPointerException

所以这样做

Intent intent = new Intent (MainActivity.this, More.class);
于 2013-03-16T13:40:07.710 回答
1

采用:

Intent intent = new Intent (MainActivity.this, More.class);

代替

Intent intent = new Intent (context,More.class);

你得到了一个NullPointerException,因为,好吧,你分配contextnull.

于 2013-03-16T13:38:43.547 回答