简体   繁体   中英

Android- Null pointer exception repeated crashing

I am beginner here so solution maybe trivial. I'm just trying to learn and the app is crashing with a null pointer exception but I can't see where that could be. Here's the logcat:

01-31 05:36:30.434: D/AndroidRuntime(1824): Shutting down VM
01-31 05:36:30.434: W/dalvikvm(1824): threadid=1: thread exiting with uncaught exception (group=0xb3aceb90)
01-31 05:36:30.454: E/AndroidRuntime(1824): FATAL EXCEPTION: main
01-31 05:36:30.454: E/AndroidRuntime(1824): Process: com.thecloset, PID: 1824
01-31 05:36:30.454: E/AndroidRuntime(1824): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.thecloset/com.thecloset.MainActivity}: java.lang.NullPointerException
01-31 05:36:30.454: E/AndroidRuntime(1824):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2102)
01-31 05:36:30.454: E/AndroidRuntime(1824):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
01-31 05:36:30.454: E/AndroidRuntime(1824):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
01-31 05:36:30.454: E/AndroidRuntime(1824):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
01-31 05:36:30.454: E/AndroidRuntime(1824):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-31 05:36:30.454: E/AndroidRuntime(1824):     at android.os.Looper.loop(Looper.java:137)
01-31 05:36:30.454: E/AndroidRuntime(1824):     at android.app.ActivityThread.main(ActivityThread.java:4998)
01-31 05:36:30.454: E/AndroidRuntime(1824):     at java.lang.reflect.Method.invokeNative(Native Method)
01-31 05:36:30.454: E/AndroidRuntime(1824):     at java.lang.reflect.Method.invoke(Method.java:515)
01-31 05:36:30.454: E/AndroidRuntime(1824):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-31 05:36:30.454: E/AndroidRuntime(1824):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-31 05:36:30.454: E/AndroidRuntime(1824):     at dalvik.system.NativeStart.main(Native Method)
01-31 05:36:30.454: E/AndroidRuntime(1824): HERE!!!!!!!     Caused by: java.lang.NullPointerException  HERE!!!!!
01-31 05:36:30.454: E/AndroidRuntime(1824):     at android.app.Activity.findViewById(Activity.java:1883)
01-31 05:36:30.454: E/AndroidRuntime(1824):     at com.thecloset.MainActivity.<init>(MainActivity.java:16)
01-31 05:36:30.454: E/AndroidRuntime(1824):     at java.lang.Class.newInstanceImpl(Native Method)
01-31 05:36:30.454: E/AndroidRuntime(1824):     at java.lang.Class.newInstance(Class.java:1208)
01-31 05:36:30.454: E/AndroidRuntime(1824):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
01-31 05:36:30.454: E/AndroidRuntime(1824):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
01-31 05:36:30.454: E/AndroidRuntime(1824):     ... 11 more

Heres the xml:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="-5dp"
        android:layout_y="65dp"
        android:rotation="90"
        android:text="secondimage" />



    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="53dp"
        android:layout_y="0dp"
        android:src="@drawable/im1" />

</AbsoluteLayout>

And the Java:

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements View.OnClickListener {

    Button button = (Button) findViewById(R.id.button1);
    ImageView img = (ImageView) findViewById(R.id.image);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.thecloset);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                img.setImageResource(R.drawable.image2);

                }
        });


    }
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    }



}

I followed lots of advice but no luck.

move following code after setContentView in onCreate()

 Button button = (Button) findViewById(R.id.button1);
 ImageView img = (ImageView) findViewById(R.id.image);

so , your class must be like,

public class MainActivity extends Activity implements View.OnClickListener {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.thecloset);

    Button button = (Button) findViewById(R.id.button1);
    ImageView img = (ImageView) findViewById(R.id.image);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            img.setImageResource(R.drawable.image2);

            }
    });

You are searching for an id in the layout before the layout is inflated (as what is not inside a method is done during class initialization), move the findViewById calls to your onCreate method :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.thecloset);
    Button button = (Button) findViewById(R.id.button1);
    ImageView img = (ImageView) findViewById(R.id.image);
    // the rest of your code
}

you can't find id of UI element before onCreate() method...So you must be find it on onCreate() method.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thecloset);

Button button = (Button) findViewById(R.id.button1);
ImageView img = (ImageView) findViewById(R.id.image);

button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        img.setImageResource(R.drawable.image2);

        }
});

Move these lines:

Button button = (Button) findViewById(R.id.button1);
ImageView img = (ImageView) findViewById(R.id.image);

to after your setContentView()

You're asking the Activity to find the views before it's loaded them

Change

Button button = (Button) findViewById(R.id.button1);
ImageView img = (ImageView) findViewById(R.id.image);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.thecloset);

To

Button button; = (Button) findViewById(R.id.button1);
ImageView img; = (ImageView) findViewById(R.id.image);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.thecloset);
    button = (Button) findViewById(R.id.button1);
    img = (ImageView) findViewById(R.id.image);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM