动画java.lang.NullPointerException错误

动画java.lang.NullPointerException错误,java,android,nullpointerexception,Java,Android,Nullpointerexception,当我想在我的设备上运行应用程序时,我在logcat中得到了这个错误 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.allo/com.example.android.allo.WelcomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.wi

当我想在我的设备上运行应用程序时,我在logcat中得到了这个错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.allo/com.example.android.allo.WelcomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setAnimation(android.view.animation.Animation)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setAnimation(android.view.animation.Animation)' on a null object reference
    at com.example.android.allo.WelcomeActivity.onCreate(WelcomeActivity.java:24)
    at android.app.Activity.performCreate(Activity.java:6975)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
    at android.os.Handler.dispatchMessage(Handler.java:105) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6541) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
WelcomActivty.java:

import android.app.Activity;
import android.content.Intent;
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.LinearLayout;
public class WelcomeActivity extends AppCompatActivity {
LinearLayout l1,l2;
Button btnsub;
Animation uptodown,downtoup;
LinearLayout linearLayout=(LinearLayout) findViewById(R.id.l1);
LinearLayout linearLayout1=(LinearLayout) findViewById(R.id.l2);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.welcome);
    java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
    btnsub = (Button)findViewById(R.id.buttoncom);
    uptodown = AnimationUtils.loadAnimation(this,R.anim.uptodown);
    downtoup = AnimationUtils.loadAnimation(this,R.anim.downtoup);
    l1.setAnimation(uptodown);
    l2.setAnimation(downtoup);
}
protected void onStart(){
    super.onStart();
    linearLayout.startAnimation(uptodown);
    linearLayout1.startAnimation(downtoup);
}
public void com(View view){
    Intent intent = new Intent(this, Login.class);
    Button buttoncom = (Button) findViewById(R.id.buttoncom);
    startActivity(intent);
}
}
welcome.xml:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.allo.WelcomeActivity"
android:background="@drawable/background"
android:orientation="vertical">
<LinearLayout
    android:id="@+id/l1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"

    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="120dp"
        android:text="@string/bienvenue"
        android:textColor="@color/lightorange"
        android:textSize="60sp"
        android:textStyle="bold" />
    <TextView
        android:textColor="@color/lightorangedark"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/dans_quoi"
        android:textAlignment="center"
        android:textSize="20sp"
        />

</LinearLayout>

<LinearLayout
    android:id="@+id/l2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="115dp"
    android:background="@drawable/spaceullustration"
    android:orientation="vertical">

    <Button
        android:id="@+id/buttoncom"
        android:layout_width="145dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/buttonstyle"
        android:text="@string/button_commencer" />
</LinearLayout>

我知道,论坛上有很多问题都是关于同一主题的,但我找不到解决方案,所以请不要将问题标记为重复的

你应该这样做-

public class WelcomeActivity extends AppCompatActivity {
    Button btnsub;
    Animation uptodown,downtoup;
    LinearLayout linearLayout;
    LinearLayout linearLayout1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);
        java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());

        linearLayout=(LinearLayout) findViewById(R.id.l1);
        linearLayout1=(LinearLayout) findViewById(R.id.l2);

        btnsub = (Button)findViewById(R.id.buttoncom);
        uptodown = AnimationUtils.loadAnimation(this,R.anim.uptodown);
        downtoup = AnimationUtils.loadAnimation(this,R.anim.downtoup);

        linearLayout.setAnimation(uptodown);
        linearLayout1.setAnimation(downtoup);
    }
}

该代码与堆栈跟踪不匹配。您发布的内容应该在运行
onCreate()
之前崩溃。@MikeM。在
onCreate
之前调用
findViewById
会引发异常吗?@Mike是的,你不能在字段初始值设定项中调用
findViewById()
。@Mike。非常感谢你,这解决了我的问题,但你为什么否决了我的问题?
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
}
}
public class WelcomeActivity extends AppCompatActivity {
    Button btnsub;
    Animation uptodown,downtoup;
    LinearLayout linearLayout;
    LinearLayout linearLayout1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);
        java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());

        linearLayout=(LinearLayout) findViewById(R.id.l1);
        linearLayout1=(LinearLayout) findViewById(R.id.l2);

        btnsub = (Button)findViewById(R.id.buttoncom);
        uptodown = AnimationUtils.loadAnimation(this,R.anim.uptodown);
        downtoup = AnimationUtils.loadAnimation(this,R.anim.downtoup);

        linearLayout.setAnimation(uptodown);
        linearLayout1.setAnimation(downtoup);
    }
}