guillaumegg10
guillaumegg10

Reputation: 53

Android Studio, animation

I'm a beginner in Android Programmation. I'm just trying to create an application with a button and a textview. When we click the button, animation begins and the texteview moves. But the application lauches good, but when I click the button, i Have this :

01-21 16:36:56.454  12536-12536/com.example.guillaume.testsappliations E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at com.example.guillaume.testsappliations.MainActivity$1.onClick(MainActivity.java:36)
        at android.view.View.performClick(View.java:4162)
        at android.view.View$PerformClick.run(View.java:17082)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4867)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
        at dalvik.system.NativeStart.main(Native Method)

This my code :

public class MainActivity extends ActionBarActivity {
Button b = null;
TextView texte =null;
Animation animation = null;


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

    Button b = (Button) findViewById(R.id.button);
    b.setOnClickListener(bListener);
    Animation animation = AnimationUtils.loadAnimation(this,R.anim.animguigui);
    TextView texte= (TextView) findViewById(R.id.textView);



}
private View.OnClickListener bListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        texte.startAnimation(animation);


    }
};


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

Thanks you very much for helping me Bye

Upvotes: 2

Views: 2915

Answers (2)

hyperCoder
hyperCoder

Reputation: 271

in your onCreate() method use following

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

in your xml file inside button declare the following-

android:id="@+id/button"

That should work. Null pointer exception means youhave not initialized the variable. One other easier thing you could do is not use listener in java and instead use onClick attribute in xml file as follows

android:onClick="buttonPressed"

Then keep your pointer on "buttonPressed"use alt+enter in windows or cmd+return on mac and create a method in your mainActivity called

buttonPressed(View view)

And add all your code that you want to excute on button click in this buttonPressed method. If you use onClick attribute, you woulldn't need to declare and initialize your button in java

Upvotes: 0

Zielony
Zielony

Reputation: 16537

Your class field TextView texte is never initialized to a non-null value. In onCreate You're initializing a local variable (same name, different object)

TextView texte =null;
Animation animation = null;

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

    Button b = (Button) findViewById(R.id.button);
    b.setOnClickListener(bListener);
    Animation animation = AnimationUtils.loadAnimation(this,R.anim.animguigui);
    TextView  // <- delete this part
          texte= (TextView) findViewById(R.id.textView);

Upvotes: 3

Related Questions