启动活动时出现NullPointerException

时间:2011-11-21 20:46:58

标签: android android-activity android-intent

这是我的代码如下。我正试图在此活动“PictureTakeActivity”中使用intent来调用我的包“Register”中的另一个活动。我在此活动中有另一个按钮,它使用意图调用相机。 当我尝试通过单击“GotoRegister”按钮调用Register.class时,程序总是停止。我在清单中添加了“注册”活动。

DDMS中的

  

无法实例化活动   ComponentInfo {Picture.Capture / Picture.Capture.Register}:   显示java.lang.NullPointerException

11-21 20:39:08.057: E/AndroidRuntime(539): FATAL EXCEPTION: main
11-21 20:39:08.057: E/AndroidRuntime(539): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{Picture.Capture/Picture.Capture.Register}: java.lang.NullPointerException
11-21 20:39:08.057: E/AndroidRuntime(539):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879)
11-21 20:39:08.057: E/AndroidRuntime(539):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
11-21 20:39:08.057: E/AndroidRuntime(539):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
11-21 20:39:08.057: E/AndroidRuntime(539):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
11-21 20:39:08.057: E/AndroidRuntime(539):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-21 20:39:08.057: E/AndroidRuntime(539):  at android.os.Looper.loop(Looper.java:137)
11-21 20:39:08.057: E/AndroidRuntime(539):  at android.app.ActivityThread.main(ActivityThread.java:4340)
11-21 20:39:08.057: E/AndroidRuntime(539):  at java.lang.reflect.Method.invokeNative(Native Method)
11-21 20:39:08.057: E/AndroidRuntime(539):  at java.lang.reflect.Method.invoke(Method.java:511)
11-21 20:39:08.057: E/AndroidRuntime(539):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-21 20:39:08.057: E/AndroidRuntime(539):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-21 20:39:08.057: E/AndroidRuntime(539):  at dalvik.system.NativeStart.main(Native Method)
11-21 20:39:08.057: E/AndroidRuntime(539): Caused by: java.lang.NullPointerException
11-21 20:39:08.057: E/AndroidRuntime(539):  at android.app.Activity.findViewById(Activity.java:1794)
11-21 20:39:08.057: E/AndroidRuntime(539):  at Picture.Capture.Register.<init>(Register.java:31)
11-21 20:39:08.057: E/AndroidRuntime(539):  at java.lang.Class.newInstanceImpl(Native Method)
11-21 20:39:08.057: E/AndroidRuntime(539):  at java.lang.Class.newInstance(Class.java:1319)
11-21 20:39:08.057: E/AndroidRuntime(539):  at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
11-21 20:39:08.057: E/AndroidRuntime(539):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
11-21 20:39:08.057: E/AndroidRuntime(539):  ... 11 more

任何人都可以帮我找到问题吗?

我是android的新手。我们如何在使用Android模拟器时有效地调试android程序?非常感谢你。

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);    
    final Button cameraButton = (Button)findViewById(R.id.camera_button); 
    // Get a handle to the button so we can add a handler for the click event 
    cameraButton.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v){
              String iString = Integer.toString(i+100);
              Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
              File file = new File("/sdcard",iString);
              outputFileUri = Uri.fromFile(file);
              intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
              startActivityForResult(intent, TAKE_PICTURE);
              i++;
           }// This will cause the onActivityResult event to fire once it's done

        });

    //Call Register.java

    Button GotoRegister = (Button) findViewById(R.id.GotoRegister);

}

public void registerhandler(View view)
{
    Intent GoRegister = new Intent(PictureTakeActivity.this,Register.class);
          startActivity(GoRegister);
    Toast toast = Toast.makeText(this, "Hello!", Toast.LENGTH_LONG);
    toast.show();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
 if (requestCode == TAKE_PICTURE) 
  {`

    Uri imageUri = null;

    // Check if the result includes a thumbnail Bitmap
    if (data != null)
    {
      if (data.hasExtra("data")) 
      {
        thumbnail = data.getParcelableExtra("data");
        // TODO Do something with the thumbnail
        SlidingDrawer mysliding= (SlidingDrawer) findViewById(R.id.drawer);
        ImageView myimage= (ImageView) findViewById(R.id.content);
        myimage.setImageBitmap(thumbnail);
        myimage.showContextMenu();
        mysliding.open(); 
      }

    //Problem!!!! This Bitmap does not have any function!
     else {
     // TODO Do something with the full image stored
     // in outputFileUri
        SlidingDrawer mysliding= (SlidingDrawer) findViewById(R.id.drawer);
        ImageView myimage= (ImageView) findViewById(R.id.content);
        myimage.setImageURI(outputFileUri);
        mysliding.open();
        }
     }
  }
}

注册课程:

public class Register extends Activity
{
public
TextView RegisterName= (TextView)findViewById(R.id.RegisterName);   
final int PICTURE_ACTIVITY = 1; 



private static int TAKE_PICTURE = 1;
@Override

public void onCreate(Bundle savedInstanceState)
{

    Bitmap bit=Picture.Capture.PictureTakeActivity.thumbnail;

    super.onCreate(savedInstanceState);
    setContentView(R.layout.register_layout);
    RegisterName.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v){

            RegisterName.clearComposingText();
        }


    });

    ImageView Registerimage= (ImageView)findViewById(R.id.RegisterPicture);

    // Relply intent from PictureTakeAcitivity and go Back

    Button GoBack=(Button)findViewById(R.id.GoBack);
    GoBack.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
            Intent replyIntent = new Intent();
            setResult(RESULT_OK, replyIntent);
            finish();
            AddEntry();
        }
    }

    );


}


private void AddEntry()
{
    final Button RegisterButton=(Button)findViewById(R.id.RegisterButton);
    RegisterButton.setOnClickListener
    (new OnClickListener()
     {
        @Override
        public void onClick(View v)
        {
              String EntryName=(String) RegisterName.getText();
              Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
              File file = new File(getFilesDir(),EntryName);
              Uri registryFileUri = Uri.fromFile(file);
              intent.putExtra(MediaStore.EXTRA_OUTPUT, registryFileUri);
              startActivityForResult(intent,TAKE_PICTURE);         
        }           
    }
    );

}

}

2 个答案:

答案 0 :(得分:0)

有你的问题。你没有在Register.java第31行实例化某些内容。

11-21 20:39:08.057: E/AndroidRuntime(539): Caused by: java.lang.NullPointerException
11-21 20:39:08.057: E/AndroidRuntime(539):  at android.app.Activity.findViewById(Activity.java:1794)
11-21 20:39:08.057: E/AndroidRuntime(539):  at Picture.Capture.Register.<init>(Register.java:31)

否则您用于调用第二个Activity的代码是正确的。

 Intent goRegister = new Intent (PictureTakeActivity.this, Register.class);
 startActivity(goRegister);

您已经说明了Register活动已添加到您的清单文件中。

编辑:

public TextView RegisterName= (TextView)findViewById(R.id.RegisterName);

此声明应该放在onCreate()方法内,紧接着:

super.onCreate(savedInstanceState);
setContentView(R.layout.register_layout);

在尝试从中访问元素之前,您必须加载布局。

这应始终是onCreate()方法中的前两行。 这意味着也要移动那条线:

  Bitmap bit=Picture.Capture.PictureTakeActivity.thumbnail;

最后,您的代码应如下所示:

public class Register extends Activity {

final int PICTURE_ACTIVITY = 1; 
private static int TAKE_PICTURE = 1;
public TextView RegisterName;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.register_layout);

    Bitmap bit=Picture.Capture.PictureTakeActivity.thumbnail;

    RegisterName = (TextView)findViewById(R.id.RegisterName);  

    RegisterName.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v){

            RegisterName.clearComposingText();
        }


    });
// etc..

答案 1 :(得分:0)

TextView RegisterName= (TextView)findViewById(R.id.RegisterName); 

在加载布局之前找不到textview,将其放在:

之后
setContentView(R.layout.register_layout);