新意图崩溃应用程序

时间:2013-03-16 13:37:36

标签: android android-layout

我想在点击ListView项目后启动新活动但应用程序崩溃。如果我注释掉意图初始化,并且只保留toast消息应用程序不会崩溃。因此问题出在意图中。我不知道为什么会这样。任何帮助将不胜感激。

   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)

2 个答案:

答案 0 :(得分:2)

您已使用null

初始化了上下文
  Context context = null;

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

并且您正在开始使用上下文,这会导致NullPointerException

所以这样做

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

答案 1 :(得分:1)

使用:

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

而不是

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

您收到了NullPointerException,因为您已将context分配给null