获得异常:无法实例化活动ComponentInfo

时间:2011-08-07 18:58:40

标签: android exception

我收到以下异常:java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{cs.workshop.solvedroid/cs.workshop.solvedroid.SolveCaptureActivity}: java.lang.NullPointerException

当我运行应用程序时,转到MainMenuActivity并尝试打开SolveCaptureAvctivity我得到异常。

我有关于此异常的其他Q,但他们都指向AndroidManifest.xml和它 对我来说似乎很好。它能是什么?请帮忙

public class SolveCaptureActivity extends Activity {

Button bCamera; ImageView captured_image = ( ImageView ) findViewById( R.id.image_capture_photo);

private static final int PICTURE_REQUEST = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.solve_capture);
    Log.e("mydebug", "ERROR ERROR ERROR");
    /*
    // "Back" button 
    Button bBack = (Button) findViewById(R.id.button_back);
    bBack.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent gotoBack = new Intent();
            setResult(RESULT_OK, gotoBack);
            finish();
        }
    });
    */

    // "Camera" button
    bCamera = (Button) findViewById(R.id.button_camera);
    bCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent camera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);   
            startActivityForResult(camera, PICTURE_REQUEST);
            int caemraRequestCode = 0, CameraResultCode = -1;
            Intent data = new Intent();
            try{
            onActivityResult(caemraRequestCode, CameraResultCode, data);
            }
            catch(Exception e){
                Log.e("mydebug", "ERROR ERROR ERROR");
            }


        }

        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            Log.d("mylog", "here1");
            Log.d("mylog", Integer.toString(requestCode));
            Log.d("mylog", Integer.toString(resultCode));
            if (requestCode == PICTURE_REQUEST) {
                Log.d("mylog", "here2");
                if (resultCode == Activity.RESULT_OK){
                    // Display image received on the view;
                    Log.d("mylog", "here3");
                    Log.d("mylog", "here4");
                    Bitmap photo = (Bitmap) data.getExtras().get("data");                   
                    if (photo != null) { // Display your image in an ImageView in your layout (if you want to test it)
                        Log.d("mylog", "here5");
                        ImageView pictureHolder = (ImageView) findViewById(R.id.image_capture_photo);
                        pictureHolder.setImageBitmap(photo);                             
                    }
                }
            }
        }
    });


}

}

public class SolveCaptureActivity extends Activity {

08-07 18:33:23.854:INFO / ActivityManager(70):显示cs.workshop.solvedroid / .MainMenuActivity:+ 4s385ms 08-07 18:33:26.784:INFO / ActivityManager(70):从pid 330开始:Intent {cmp = cs.workshop.solvedroid / .AboutActivity} 08-07 18:33:26.984:DEBUG / dalvikvm(330):GC_EXTERNAL_ALLOC释放11K,53%免费2582K / 5379K,外部7607K / 9499K,暂停51ms 08-07 18:33:28.434:INFO / ActivityManager(70):显示cs.workshop.solvedroid / .AboutActivity:+ 1s68ms 08-07 18:33:29.104:WARN / KeyCharacterMap(330):没有用于id 0的键盘 08-07 18:33:29.104:WARN / KeyCharacterMap(330):使用默认键映射:/system/usr/keychars/qwerty.kcm.bin 08-07 18:33:30.674:INFO / ActivityManager(70):从pid 330开始:Intent {cmp = cs.workshop.solvedroid / .SolveCaptureActivity} 08-07 18:33:30.834:DEBUG / AndroidRuntime(330):关闭VM 08-07 18:33:30.834:WARN / dalvikvm(330):threadid = 1:线程退出,未捕获异常(组= 0x40015560) 08-07 18:33:30.874:ERROR / AndroidRuntime(330):致命异常:主要 08-07 18:33:30.874:ERROR / AndroidRuntime(330):java.lang.RuntimeException:无法实例化活动ComponentInfo {cs.workshop.solvedroid / cs.workshop.solvedroid.SolveCaptureActivity}:java.lang.NullPointerException 08-07 18:33:30.874:ERROR / AndroidRuntime(330):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1544) 08-07 18:33:30.874:ERROR / AndroidRuntime(330):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638) 08-07 18:33:30.874:ERROR / AndroidRuntime(330):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117) 08-07 18:33:30.874:ERROR / AndroidRuntime(330):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:928) 08-07 18:33:30.874:ERROR / AndroidRuntime(330):在android.os.Handler.dispatchMessage(Handler.java:99) 08-07 18:33:30.874:ERROR / AndroidRuntime(330):在android.os.Looper.loop(Looper.java:123) 08-07 18:33:30.874:ERROR / AndroidRuntime(330):在android.app.ActivityThread.main(ActivityThread.java:3647) 08-07 18:33:30.874:ERROR / AndroidRuntime(330):at java.lang.reflect.Method.invokeNative(Native Method) 08-07 18:33:30.874:ERROR / AndroidRuntime(330):at java.lang.reflect.Method.invoke(Method.java:507) 08-07 18:33:30.874:ERROR / AndroidRuntime(330):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839) 08-07 18:33:30.874:ERROR / AndroidRuntime(330):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 08-07 18:33:30.874:ERROR / AndroidRuntime(330):at dalvik.system.NativeStart.main(Native Method) 08-07 18:33:30.874:ERROR / AndroidRuntime(330):引起:java.lang.NullPointerException 08-07 18:33:30.874:ERROR / AndroidRuntime(330):在android.app.Activity.findViewById(Activity.java:1647) 08-07 18:33:30.874:ERROR / AndroidRuntime(330):at cs.workshop.solvedroid.SolveCaptureActivity。(SolveCaptureActivity.java:15) 08-07 18:33:30.874:ERROR / AndroidRuntime(330):at java.lang.Class.newInstanceImpl(Native Method) 08-07 18:33:30.874:ERROR / AndroidRuntime(330):at java.lang.Class.newInstance(Class.java:1409) 08-07 18:33:30.874:ERROR / AndroidRuntime(330):在android.app.Instrumentation.newActivity(Instrumentation.java:1021) 08-07 18:33:30.874:ERROR / AndroidRuntime(330):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1536) 08-07 18:33:30.874:ERROR / AndroidRuntime(330):... 11更多

private static final int PICTURE_REQUEST = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.solve_capture);
    Log.e("mydebug", "ERROR ERROR ERROR");
    /*
    // "Back" button 
    Button bBack = (Button) findViewById(R.id.button_back);
    bBack.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent gotoBack = new Intent();
            setResult(RESULT_OK, gotoBack);
            finish();
        }
    });
    */

    // "Camera" button
    bCamera = (Button) findViewById(R.id.button_camera);
    bCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent camera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);   
            startActivityForResult(camera, PICTURE_REQUEST);
            int caemraRequestCode = 0, CameraResultCode = -1;
            Intent data = new Intent();
            try{
            onActivityResult(caemraRequestCode, CameraResultCode, data);
            }
            catch(Exception e){
                Log.e("mydebug", "ERROR ERROR ERROR");
            }


        }

        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            Log.d("mylog", "here1");
            Log.d("mylog", Integer.toString(requestCode));
            Log.d("mylog", Integer.toString(resultCode));
            if (requestCode == PICTURE_REQUEST) {
                Log.d("mylog", "here2");
                if (resultCode == Activity.RESULT_OK){
                    // Display image received on the view;
                    Log.d("mylog", "here3");
                    Log.d("mylog", "here4");
                    Bitmap photo = (Bitmap) data.getExtras().get("data");                   
                    if (photo != null) { // Display your image in an ImageView in your layout (if you want to test it)
                        Log.d("mylog", "here5");
                        ImageView pictureHolder = (ImageView) findViewById(R.id.image_capture_photo);
                        pictureHolder.setImageBitmap(photo);                             
                    }
                }
            }
        }
    });


}

1 个答案:

答案 0 :(得分:4)

在调用findViewById()之前,您无法使用setContentView()