0

When i want to run app in my device i get this error in 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>

activity_login.xml

<RelativeLayout 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:background="@drawable/backgroudlogin"
tools:context=".Login">

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="232dp"
    android:background="@drawable/ic_email_button"
    android:hint="     Email"/>

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="159dp"
    android:background="@drawable/ic_email_button"
    android:hint="     password"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="70dp"
    android:background="@drawable/ic_sign_in"
    android:text="Sign IN" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="20dp"
    android:text="create an account"
    android:textSize="20dp"
    android:textColor="#FFFFFF"/>

Login.xml

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

public class Login extends AppCompatActivity {

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

i know, there is a lot of question in the forum with the same subject but I can not find the solution, so please do not mark the question as duplicated

1 Answers1

0

You should do this way -

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);
    }
}
Raj
  • 2,997
  • 2
  • 12
  • 30