为什么我的Andr​​oid应用程序停止了? [英] Why has my Android app stopped?

查看:209
本文介绍了为什么我的Andr​​oid应用程序停止了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把我在Android的Studio应用程序。我运行这个应用程序,但Android应用程序停止。我做与动画的应用程序。
下面是我的源$ C ​​$ C:

AndroidManifest:

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.geven.animation>    <应用        机器人:allowBackup =真
        机器人:图标=@的mipmap / ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme>
        <活动
            机器人:名字=。MainActivity
            机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
        <活动
            机器人:名字=动漫
            机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=com.geven.animation.DRAWINGTHEBALL/>                <类机器人:名字=android.intent.category.DEFAULT/>
            &所述; /意图滤光器>
        < /活性GT;
    < /用途>< /清单>

MainActivity:

 包com.geven.animation;进口android.app.Activity;
进口android.content.Intent;
进口android.support.v7.app.ActionBarActivity;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.widget.Button;
公共类MainActivity延伸活动{
Button按钮=(按钮)findViewById(R.id.button);
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        button.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(查看视图){
                startActivity(新意图(MainActivity.this,Animation.class));
            }
        });
        的setContentView(R.layout.activity_main);
    }
    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.menu_main,菜单);
        返回true;
    }    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        //处理动作栏项目点击这里。操作栏会
        //自动处理上点击主页/向上按钮,只要
        //你在AndroidManifest.xml中指定一个父活动。
        INT ID = item.getItemId();        // noinspection SimplifiableIfStatement
        如果(ID == R.id.action_settings){
            返回true;
        }        返回super.onOptionsItemSelected(项目);
    }
}

ActivityMain.xml:

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s的android:layout_width =match_parent
    机器人:layout_height =match_parent机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    机器人:paddingBottom会=@扪/ activity_vertical_margin工具:上下文=MainActivity。><按钮
    机器人:layout_width =400dp
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_centerHorizo​​ntal =真
    机器人:比重=中心
    机器人:TEXTSIZE =20dp
    机器人:ID =@ + ID /按钮
    机器人:文字=转到动漫/>< / RelativeLayout的>

Animation.java:

 包com.geven.animation;
进口android.app.Activity;
进口android.os.Bundle;公共类动画延伸活动{    DrawingTheBall伏;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        V =新DrawingTheBall(本);
        的setContentView(五);
    }
}

DrawingTheBall.java:

 包com.geven.animation;
进口android.content.Context;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.graphics.Paint;
进口android.graphics.Rect;
进口android.view.View;公共类DrawingTheBall扩展视图{
    位图bbal;
    INT X,Y;    公共DrawingTheBall(上下文的背景下){
        超级(上下文);
        bbal = BitmapFactory.de codeResource(getResources(),R.drawable.blueball);
        X = 0;
        Y = 0;
    }    @覆盖
    保护无效的onDraw(帆布油画){
        super.onDraw(画布);        矩形ourRect =新的矩形();
        ourRect.set(0,0,canvas.getWidth(),canvas.getHeight()/ 2);        漆蓝=新的油漆();
        blue.setColor(Color.BLUE);
        blue.setStyle(Paint.Style.FILL);
        canvas.drawRect(ourRect,蓝);        如果(X LT; canvas.getWidth()){
            X + = 10;
        }否则{X = 0;}        如果(γ&下; canvas.getHeight()){
            Y + = 10;
        }其他{Y = 0;}
        涂料P =新的油漆();
        canvas.drawBitmap(bbal,X,Y,P);
        无效();
    }
}

logcat的:

  02-26 21:55:59.874 17975-17975 / com.geven.animation D / AndroidRuntime:关闭VM
02-26 21:55:59.874 17975-17975 / com.geven.animation W / dalvikvm:主题ID = 1:螺纹未捕获的异常退出(组= 0x4174b700)
02-26 21:55:59.879 17975-17975 / com.geven.animation E / AndroidRuntime:致命异常:主要
    了java.lang.RuntimeException:无法实例活动ComponentInfo {com.geven.animation / com.geven.animation.MainActivity}:显示java.lang.NullPointerException
            在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2232)
            在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
            在android.app.ActivityThread.access $ 700(ActivityThread.java:168)
            在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1329)
            在android.os.Handler.dispatchMessage(Handler.java:99)
            在android.os.Looper.loop(Looper.java:177)
            在android.app.ActivityThread.main(ActivityThread.java:5493)
            在java.lang.reflect.Method.invokeNative(本机方法)
            在java.lang.reflect.Method.invoke(Method.java:525)
            在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1225)
            在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
            在dalvik.system.NativeStart.main(本机方法)
     显示java.lang.NullPointerException:产生的原因
            在android.app.Activity.findViewById(Activity.java:1914)
            在com.geven.animation.MainActivity<&初始化GT;(MainActivity.java:14)
            在java.lang.Class.newInstanceImpl(本机方法)
            在java.lang.Class.newInstance(Class.java:1130)
            在android.app.Instrumentation.newActivity(Instrumentation.java:1078)
            在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2223)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
在android.app.ActivityThread.access $ 700(ActivityThread.java:168)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1329)
在android.os.Handler.dispatchMessage(Handler.java:99)
在android.os.Looper.loop(Looper.java:177)
在android.app.ActivityThread.main(ActivityThread.java:5493)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:525)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1225)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
在dalvik.system.NativeStart.main(本机方法)
02-26 22:01:00.199 17975-17975 / com.geven.animation I /工艺:发送信号。 PID:17975 SIG:9


解决方案

按钮按钮=(按钮)findViewById(R.id.button);

您需要把该行你的的onCreate 方法。

基本上,在外地一级,你可以有按钮按钮; ,然后在的onCreate 你会初始化正是如此变量:按钮=(按钮)findViewById(R.id.button); 。你不能叫 findViewById()前视图已经被夸大了。

作为一个方面说明,一个堆栈跟踪可以看第一吓人,但对付它最简单的方法,就是寻找你的包名,在这种情况下,com.geven.animation。在包括包名堆栈跟踪的第一项通常是有问题的行。 INIT> LT在com.geven.animation.MainActivity&(MainActivity.java:14)让14行的 MainActivity 类。

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

解决方案

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.

这篇关于为什么我的Andr​​oid应用程序停止了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆