为什么当新活动开始时我的应用程序崩溃?

时间:2019-06-30 12:07:23

标签: java android xml

我创建了一个动画活动,但是当应用启动时,启动屏幕后立即崩溃。我创建了更多活动,但它们运行得很好,但是此活动未运行。

这是错误日志:

  E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.aj.myapplication, PID: 9549
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aj.myapplication/com.aj.myapplication.Animactivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3303)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3411)
        at android.app.ActivityThread.-wrap12(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994)
        at android.os.Handler.dispatchMessage(Handler.java:108)
        at android.os.Looper.loop(Looper.java:166)
        at android.app.ActivityThread.main(ActivityThread.java:7529)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.aj.myapplication.Animactivity.onCreate(Animactivity.java:27)
        at android.app.Activity.performCreate(Activity.java:7383)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3256)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3411) 
        at android.app.ActivityThread.-wrap12(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994) 
        at android.os.Handler.dispatchMessage(Handler.java:108) 
        at android.os.Looper.loop(Looper.java:166) 
        at android.app.ActivityThread.main(ActivityThread.java:7529) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921) 
I/Process: Sending signal. PID: 9549 SIG: 9
Application terminated.


XML代码:

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".Animactivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fade_in_btn"
        android:text="Fade In"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello!"
        android:id="@+id/animview"
        android:layout_marginStart="10dp"
        android:layout_marginTop="7dp"
        android:textSize="28sp"/>



</LinearLayout>

Java代码:

 package com.aj.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

public class Animactivity extends AppCompatActivity {

    TextView txtview;
    Button animbtn;
    Animation fadein;

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

        txtview=(TextView) findViewById(R.id.animview);
        animbtn=(Button) findViewById(R.id.animbtn);

        fadein= AnimationUtils.loadAnimation(Animactivity.this,R.anim.fadein);

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

                txtview.setVisibility(View.VISIBLE);
                txtview.startAnimation(fadein);
            }
        });

    }
}


启动画面Java代码:

   package com.aj.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class splash extends AppCompatActivity {

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

        Thread td=new Thread(){

            @Override
            public void run() {
                try {
                    sleep(3000);
                } catch (Exception ex){
                    ex.printStackTrace();
                } finally {
                    Intent it=new Intent(splash.this,Animactivity.class);
                    startActivity(it);
                }
            }
        }; td.start();


    }


}




当我将活动更改为另一个活动时,包含该活动的应用程序运行完美。

4 个答案:

答案 0 :(得分:1)

这个异常很明显。

在活动中,您有此行

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

但是xml布局中没有名为animbtn的ID。这就是为什么您将获得nullPointerException的原因。

答案 1 :(得分:1)

按钮在XML文件中的ID和您在活动中传递给findViewById()方法的ID不匹配。因此,该按钮为空,并且在调用按钮的方法时将得到NullPointerException

答案 2 :(得分:1)

public class Animactivity extends AppCompatActivity {

    TextView txtview;
    Button animbtn;
    Animation fadein;

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

        txtview=(TextView) findViewById(R.id.animview);
        animbtn=(Button) findViewById(R.id.fade_in_btn); //<<< This line of the ID is wrong

        fadein= AnimationUtils.loadAnimation(Animactivity.this,R.anim.fadein);

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

                txtview.setVisibility(View.VISIBLE);
                txtview.startAnimation(fadein);
            }
        });

    }
}

答案 3 :(得分:1)

替换

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

animbtn=(Button) findViewById(R.id.fade_in_btn);

没有名为id的{​​{1}}。

Read more about NullPointerExceptions