致命异常:主要由java.lang.NullPointerException菜单按钮引起

时间:2012-02-09 22:02:18

标签: java android fatal-error

我在Android上是全新的。我试着设置菜单按钮并遇到了这个问题。我在这个论坛上检查了这一点,发现它意味着没有定义的东西,但无法弄清楚我的代码中哪一个未定义。请帮忙!任何建议都将不胜感激。

顺便说一下,如果我单独运行它,Camera_SettingActivity.java就能正常工作。

public class VideoViewActivity extends Activity {

    protected int my_num = 2280;
    protected Intent intent_camera;

    /** Called when the activity is first created. */
            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                MenuInflater inflater = getMenuInflater();
                inflater.inflate(R.layout.menu, menu);
                return true;
            }

            @Override
            public boolean onOptionsItemSelected(MenuItem item) {

                intent_camera = new Intent();

                switch (item.getItemId()) {

                    case R.id.camera:
                        intent_camera.setClass(VideoViewActivity.this, Camera_SettingActivity.class);
                        startActivityForResult(intent_camera, my_num);
                        break;

                }
                return true;
            }

}



public class Camera_SettingActivity extends Activity {


protected int my_num = 1228;
private Bundle bundle;

public static String url = "rtsp://v3.cache8.c.youtube.com/CiILENy73wIaGQmXovF6e-Rf-BMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp";
private VideoView videoView1 = null;
private Context ctx = null;
private ProgressBar prog1 = null;

protected String str_IP;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_camera_layout);

    bundle = this.getIntent().getExtras();

    str_IP = bundle.getString("editIP");

    Button test1 = (Button) findViewById(R.id.button_test1);

    test1.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View v) {

            prog1 = (ProgressBar)findViewById(R.id.progressBar1);
                    videoView1  = (VideoView) findViewById(R.id.videoView1);
                    Uri video   = Uri.parse(url);

                    videoView1.setVideoURI(video);

                    videoView1.setOnErrorListener(new OnErrorListener() {

                        @Override
                        public boolean onError(MediaPlayer mp, int what, int extra) {
                            // TODO Auto-generated method stub
                            Toast.makeText(ctx, "Error occured", 500).show();
                            return false;
                        }
                    });

                    videoView1.setOnPreparedListener(new OnPreparedListener() {

                        public void onPrepared(MediaPlayer arg0) {
                            prog1.setVisibility(View.GONE);
                            videoView1.start();
                        }
                    });
             }

    });
}

 @Override
 protected void onDestroy() {
     try{
         videoView1.stopPlayback();
     }catch(Exception e){
         //
     }
     super.onDestroy();
 }

}

1 个答案:

答案 0 :(得分:1)

每个活动都应该有onCreate()方法。你的代码遗漏了它。这可能会导致错误。在方法中添加onCreate()和setContentView()。