3

我在我的模拟器上收到这些错误,并且应用程序在那里崩溃。

日志猫

08-30 22:18:20.066: E/AndroidRuntime(860): FATAL EXCEPTION: main
08-30 22:18:20.066: E/AndroidRuntime(860): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mojoo/com.example.mojoo.main}: java.lang.NullPointerException
08-30 22:18:20.066: E/AndroidRuntime(860):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-30 22:18:20.066: E/AndroidRuntime(860):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-30 22:18:20.066: E/AndroidRuntime(860):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-30 22:18:20.066: E/AndroidRuntime(860):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-30 22:18:20.066: E/AndroidRuntime(860):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-30 22:18:20.066: E/AndroidRuntime(860):  at android.os.Looper.loop(Looper.java:123)
08-30 22:18:20.066: E/AndroidRuntime(860):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-30 22:18:20.066: E/AndroidRuntime(860):  at java.lang.reflect.Method.invokeNative(Native Method)
08-30 22:18:20.066: E/AndroidRuntime(860):  at java.lang.reflect.Method.invoke(Method.java:507)
08-30 22:18:20.066: E/AndroidRuntime(860):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-30 22:18:20.066: E/AndroidRuntime(860):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-30 22:18:20.066: E/AndroidRuntime(860):  at dalvik.system.NativeStart.main(Native Method)
08-30 22:18:20.066: E/AndroidRuntime(860): Caused by: java.lang.NullPointerException
08-30 22:18:20.066: E/AndroidRuntime(860):  at com.example.mojoo.main.onCreate(main.java:26)
08-30 22:18:20.066: E/AndroidRuntime(860):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-30 22:18:20.066: E/AndroidRuntime(860):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-30 22:18:20.066: E/AndroidRuntime(860):  ... 11 more
08-30 22:18:21.965: I/Process(860): Sending signal. PID: 860 SIG: 9
08-30 22:19:08.275: D/AndroidRuntime(894): Shutting down VM
08-30 22:19:08.275: W/dalvikvm(894): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-30 22:19:08.285: E/AndroidRuntime(894): FATAL EXCEPTION: main
08-30 22:19:08.285: E/AndroidRuntime(894): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mojoo/com.example.mojoo.main}: java.lang.NullPointerException
08-30 22:19:08.285: E/AndroidRuntime(894):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-30 22:19:08.285: E/AndroidRuntime(894):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-30 22:19:08.285: E/AndroidRuntime(894):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-30 22:19:08.285: E/AndroidRuntime(894):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-30 22:19:08.285: E/AndroidRuntime(894):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-30 22:19:08.285: E/AndroidRuntime(894):  at android.os.Looper.loop(Looper.java:123)
08-30 22:19:08.285: E/AndroidRuntime(894):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-30 22:19:08.285: E/AndroidRuntime(894):  at java.lang.reflect.Method.invokeNative(Native Method)
08-30 22:19:08.285: E/AndroidRuntime(894):  at java.lang.reflect.Method.invoke(Method.java:507)
08-30 22:19:08.285: E/AndroidRuntime(894):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-30 22:19:08.285: E/AndroidRuntime(894):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-30 22:19:08.285: E/AndroidRuntime(894):  at dalvik.system.NativeStart.main(Native Method)
08-30 22:19:08.285: E/AndroidRuntime(894): Caused by: java.lang.NullPointerException
08-30 22:19:08.285: E/AndroidRuntime(894):  at com.example.mojoo.main.onCreate(main.java:26)
08-30 22:19:08.285: E/AndroidRuntime(894):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-30 22:19:08.285: E/AndroidRuntime(894):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-30 22:19:08.285: E/AndroidRuntime(894):  ... 11 more

这是我的 src main.java

package com.example.mojoo;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;


public class main extends Activity {

    ImageView iv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv=(ImageView) findViewById(R.id.imageView1);

        Button b = (Button) findViewById(R.id.button1);

        b.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                startActivityForResult(intent, 0);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Bitmap bm = (Bitmap) data.getExtras().get("data");
        iv.setImageBitmap(bm);
    }

}

编辑对于问题的一些参考,我正在添加xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.03"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="307dp"
            android:orientation="vertical" >
<LinearLayout

                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <LinearLayout

                        android:layout_width="172dp"
                        android:layout_height="match_parent"
                        android:orientation="vertical" >

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="fill_parent" >

                            <TextView
                                android:id="@+id/textView2"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentRight="true"
                                android:layout_alignParentTop="true"
                                android:text="MOJO ULTIMATE"
                                android:textAppearance="?android:attr/textAppearanceLarge" />

                            <ImageView
                                android:id="@+id/imageView1"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignLeft="@+id/textView2"
                                android:layout_alignParentBottom="true"
                                android:layout_alignParentRight="true"
                                android:layout_below="@+id/textView2"
                                android:src="@drawable/mojohdpi" />

                        </RelativeLayout>

                    </LinearLayout>

                    <LinearLayout

                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical" >

                        <RelativeLayout

                            android:layout_width="match_parent"
                            android:layout_height="match_parent" >

                            <ImageButton
                                android:id="@+id/imageButton1"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentTop="true"
                                android:layout_centerHorizontal="true"
                                android:layout_marginTop="14dp"
                                android:src="@drawable/camerahdpi" />

                            <ImageButton
                                android:id="@+id/imageButton2"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignRight="@+id/imageButton1"
                                android:layout_below="@+id/imageButton1"
                                android:src="@drawable/adohdpi" />

                            <ImageButton
                                android:id="@+id/imageButton3"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_below="@+id/imageButton2"
                                android:layout_centerHorizontal="true"
                                android:src="@drawable/vediohdpi" />

                            <ImageButton
                                android:id="@+id/imageButton4"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_below="@+id/imageButton3"
                                android:layout_centerHorizontal="true"
                                android:src="@drawable/livehdpi" />

                            <ImageButton
                                android:id="@+id/imageButton5"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentBottom="true"
                                android:layout_alignRight="@+id/imageButton4"
                                android:layout_below="@+id/imageButton4"
                                android:src="@drawable/browsehdpi" />

                        </RelativeLayout>

                    </LinearLayout>

                </LinearLayout>

            </LinearLayout>



            <LinearLayout

                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <EditText
                        android:id="@+id/editText1"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentTop="true"
                        android:layout_marginTop="18dp"
                        android:ems="10"
                        android:hint="Write an annotation..."
                        android:inputType="textMultiLine" />

                    <RadioButton
                        android:id="@+id/radioButton2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:layout_alignParentRight="true"
                        android:layout_marginRight="114dp" />

                    <RadioButton
                        android:id="@+id/radioButton1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:layout_toLeftOf="@+id/radioButton2" />

                    <ImageButton
                        android:id="@+id/imageButton6"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_above="@+id/radioButton2"
                        android:layout_alignParentRight="true"
                        android:layout_below="@+id/editText1"
                        android:src="@drawable/okhdpi" />

                </RelativeLayout>

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>
4

1 回答 1

5

看来您的 b 为空。确保以下语句中的按钮 ID 正确。

 Button b = (Button) findViewById(R.id.button1);
于 2012-08-30T16:37:51.283 回答