【问题标题】:Why has my Android app stopped? [duplicate]为什么我的 Android 应用程序停止了? [复制]
【发布时间】:2015-02-26 21:12:15
【问题描述】:

我在 Android Studio 中制作我的应用程序。我运行此应用程序,但 Android 应用程序停止了。我用动画制作了一个应用程序。 这是我的源代码:

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>

主活动:

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>

动画.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

【问题讨论】:

    标签: java android android-studio


    【解决方案1】:

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

    您需要将该行放入您的onCreate 方法中。

    基本上,在字段级别,您可以拥有Button button;,然后在onCreate 中,您将因此初始化变量:button = (Button) findViewById(R.id.button);。在视图膨胀之前,您不能调用findViewById()。

    附带说明一下,堆栈跟踪一开始可能看起来很吓人,但处理它的最简单方法是搜索您的包名称,在本例中为“com.geven.animation”。堆栈跟踪中包含该包名称的第一个条目通常是有问题的行。 at com.geven.animation.MainActivity.&lt;init&gt;(MainActivity.java:14) 所以你的 MainActivity 类的第 14 行。

    【讨论】:

    • Thnx,但它没有工作:(
    • 您可能在其他地方遇到了另一个错误。放置新的堆栈跟踪,以便我们查看它。
    • 不用担心,如果这个答案对您有帮助,您可以投票/接受作为答案(只需点击旁边的复选框)。
    猜你喜欢
    • 1970-01-01
    • 2017-12-04
    • 2021-08-14
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    相关资源
    最近更新 更多