以下是main activity class
. 它将内容设置为main.xml
,其中包括一个按钮btn
。单击此按钮时,内容设置为pic.xml
,其中有两个按钮,btn1
和btn2
。单击时btn1
,它应该将内容设置回,main.xml
但这没有发生。
package com.asin.amit;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
public class AsinActivity extends Activity {
/** Called when the activity is first created. */
private TextView tv ;
private VideoView myVideoView;
private Button btn;
private Button btn1;
private Button btn2;
@Override
public void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.b);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
String str= "/sdcard/DCIM/a.mp4";
tv = (TextView) findViewById(R.id.tv1);
myVideoView = (VideoView)findViewById(R.id.myvideoview);
myVideoView.setVideoPath(str);
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
myVideoView.start();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myVideoView.pause();
setContentView(R.layout.pic);
}
});
btn1.setOnClickListener(new ButtonListener());
}
catch (Exception e) {
// handle any errors
Log.e("HelloWorld", "1", e); // log the error
// Also let the user know something went wrong
Toast.makeText(
getApplicationContext(),
e.getClass().getName() + " " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
class ButtonListener implements View.OnClickListener{
@Override
public void onClick(View v) {
setContentView(R.layout.main);
}
}
}
在电话线上btn1.setOnClickListener(new ButtonListener());
,logcat 说java.lang.NullPointerException
我在做什么错?