簡體   English   中英

Android-空指針異常反復崩潰

[英]Android- Null pointer exception repeated crashing

我是這里的初學者,因此解決方案可能微不足道。 我只是在嘗試學習,應用程序因null pointer exception而崩潰,但我看不到可能在哪里。 這是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

繼承人的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>

和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

    }



}

我聽了很多建議,但沒有運氣。

在onCreate()中的setContentView之后移動以下代碼

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

因此,您的課程必須像

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);

            }
    });

您要在版式膨脹之前在版式中搜索ID(因為在類初始化期間完成了方法內部的操作),請將findViewById調用移至onCreate方法:

@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
}

您無法在onCreate()方法之前找到UI元素的ID ...因此必須在onCreate()方法中找到它。

@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);

        }
});

移動這些行:

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

到setContentView()之后

您要讓Activity在加載視圖之前先找到它們

更改

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; = (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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM