-1

I make my application in Android Studio. I run this application but the Android app stopped. I make a app with an animation. Here is my source code:

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.geven.animation" >

    <application

        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Animation"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.geven.animation.DRAWINGTHEBALL" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

MainActivity:

package com.geven.animation;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;


public class MainActivity extends Activity {
Button button = (Button) findViewById(R.id.button);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, Animation.class));
            }
        });
        setContentView(R.layout.activity_main);
    }


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

ActivityMain.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<Button
    android:layout_width="400dp"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    android:textSize="20dp"
    android:id="@+id/button"
    android:text="Goto animation"/>

</RelativeLayout>

Animation.java:

package com.geven.animation;


import android.app.Activity;
import android.os.Bundle;

public class Animation extends Activity {

    DrawingTheBall v;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        v = new DrawingTheBall(this);
        setContentView(v);
    }


}

DrawingTheBall.java:

package com.geven.animation;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

public class DrawingTheBall extends View {
    Bitmap bbal;
    int x,y;

    public DrawingTheBall(Context context) {
        super(context);
        bbal = BitmapFactory.decodeResource(getResources(), R.drawable.blueball);
        x=0;
        y=0;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Rect ourRect = new Rect();
        ourRect.set(0,0,canvas.getWidth(), canvas.getHeight()/2);

        Paint blue = new Paint();
        blue.setColor(Color.BLUE);
        blue.setStyle(Paint.Style.FILL);
        canvas.drawRect(ourRect,blue);

        if (x< canvas.getWidth()) {
            x += 10;
        }else {x=0;}

        if (y<canvas.getHeight()) {
            y += 10;
        }else {y=0;}
        Paint p = new Paint();
        canvas.drawBitmap(bbal, x, y, p);
        invalidate();
    }
}

Logcat:

02-26 21:55:59.874  17975-17975/com.geven.animation D/AndroidRuntime﹕ Shutting down VM
02-26 21:55:59.874  17975-17975/com.geven.animation W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4174b700)
02-26 21:55:59.879  17975-17975/com.geven.animation E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.geven.animation/com.geven.animation.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2232)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
            at android.app.ActivityThread.access$700(ActivityThread.java:168)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:177)
            at android.app.ActivityThread.main(ActivityThread.java:5493)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at android.app.Activity.findViewById(Activity.java:1914)
            at com.geven.animation.MainActivity.<init>(MainActivity.java:14)
            at java.lang.Class.newInstanceImpl(Native Method)
            at java.lang.Class.newInstance(Class.java:1130)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2223)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
            at android.app.ActivityThread.access$700(ActivityThread.java:168)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:177)
            at android.app.ActivityThread.main(ActivityThread.java:5493)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
            at dalvik.system.NativeStart.main(Native Method)
02-26 22:01:00.199  17975-17975/com.geven.animation I/Process﹕ Sending signal. PID: 17975 SIG: 9
Luc
  • 27
  • 6

1 Answers1

2

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

You need to put that line in your onCreate method.

Basically, at the field level, you can have Button button; and then in onCreate you would initialize the variable thusly: button = (Button) findViewById(R.id.button);. You can't call findViewById() before the view has been inflated.

As a side note, a stacktrace may look intimidating at first, but the easiest way to deal with it, is to search for your package name, in this case "com.geven.animation". The first entry in the stack trace that includes that package name is usually the offending line. at com.geven.animation.MainActivity.<init>(MainActivity.java:14) so line 14 of your MainActivity class.

ajacian81
  • 7,419
  • 9
  • 51
  • 64