【问题标题】:Android Application crash with Application has StoppedAndroid 应用程序崩溃,应用程序已停止
【发布时间】:2012-07-18 02:17:27
【问题描述】:

我的 Android 应用程序编译正常,但崩溃并显示消息应用程序已停止。 我的代码有什么问题?

#GameView.java
public class GameView extends View {
public Bitmap droid; 
public Matrix translate;
public GameView(Context context) {
super(context);
droid = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
}
public void onDraw(Canvas canvas) {  
canvas.drawBitmap(droid, 10, 10, null);
}

#Game.java
public class Game extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout view = (FrameLayout)findViewById(R.id.graphics_holder);
view.addView(new GameView(this));
setContentView(R.layout.game);

LogCat

07-18 04:48:05.517: E/AndroidRuntime(960): FATAL EXCEPTION: main
07-18 04:48:05.517: E/AndroidRuntime(960): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.oop/com.example.oop.Game}: java.lang.NullPointerException
07-18 04:48:05.517: E/AndroidRuntime(960):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
07-18 04:48:05.517: E/AndroidRuntime(960):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
07-18 04:48:05.517: E/AndroidRuntime(960):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
07-18 04:48:05.517: E/AndroidRuntime(960):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
07-18 04:48:05.517: E/AndroidRuntime(960):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 04:48:05.517: E/AndroidRuntime(960):  at android.os.Looper.loop(Looper.java:137)
07-18 04:48:05.517: E/AndroidRuntime(960):  at android.app.ActivityThread.main(ActivityThread.java:4745)
07-18 04:48:05.517: E/AndroidRuntime(960):  at java.lang.reflect.Method.invokeNative(Native Method)
07-18 04:48:05.517: E/AndroidRuntime(960):  at java.lang.reflect.Method.invoke(Method.java:511)
07-18 04:48:05.517: E/AndroidRuntime(960):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-18 04:48:05.517: E/AndroidRuntime(960):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-18 04:48:05.517: E/AndroidRuntime(960):  at dalvik.system.NativeStart.main(Native Method)
07-18 04:48:05.517: E/AndroidRuntime(960): Caused by: java.lang.NullPointerException
07-18 04:48:05.517: E/AndroidRuntime(960):  at com.example.oop.Game.onCreate(Game.java:19)
07-18 04:48:05.517: E/AndroidRuntime(960):  at android.app.Activity.performCreate(Activity.java:5008)
07-18 04:48:05.517: E/AndroidRuntime(960):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-18 04:48:05.517: E/AndroidRuntime(960):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
07-18 04:48:05.517: E/AndroidRuntime(960):  ... 11 more

如果我改成这个就可以了

public class Game extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
//      FrameLayout view = (FrameLayout)findViewById(R.id.graphics_holder);
//      view.addView();
        setContentView(new GameView(this));
}       
}

但我的意图是使用框架来封装我想要显示的内容,因为我的布局还有其他 GUI 对象要显示。

我的布局文件 game.xml

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="@dimen/padding_large" >
    <FrameLayout
        android:id="@+id/graphics_holder"
        android:layout_width="300dp"
        android:layout_height="400dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" >
    </FrameLayout>
    <TextView
        android:id="@+id/bpm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="TextView"
        android:textSize="@dimen/padding_large" />
</RelativeLayout>

【问题讨论】:

  • 您遇到什么错误?...您可以查看日志猫,这是整个代码吗?
  • @user153398,请根据以下建议报告您的调查情况。
  • 天哪,日志内容太多了。在日志中,猫错误以红色文本发布。这就是你要找的。它将给出异常的堆栈跟踪。

标签: android graphics canvas frame


【解决方案1】:

“应用程序已停止”通知仅表示您的代码中有未处理的异常,它必须终止。这可能是除以零、空指针异常或尝试为不在清单中的活动调用意图。

虽然您的错误似乎是基于在调用 setContentView 之前调用 findViewById,但根据您提供的 sn-p,实际上不可能确定到底是什么问题,如果这是唯一的问题,但是 Log Cat(一个非常有用的工具和 ADT 的一部分)提供了导致强制关闭(简称 FC)的错误的完整堆栈跟踪。请查看如何使用 Log Cat,因为您在 SO 上发布的几乎所有问题都会涉及人们询问您的 Log Cat 或堆栈跟踪。

【讨论】:

  • 在旁注中,我想说您的 View 实现似乎......不是构建 Android 布局的标准方式。这可能是问题的根源。
  • 视图看起来不错,我会使用一个活动,但大多数刚开始使用 Android 的 Java 程序员出于某种疯狂的原因认为创建活动会使设备过载:)。他们没有读懂android的基础。
  • 同意,我试图指出这种实施方式可能会带来麻烦。了解系统期望的事情是如何完成的总是很好的。
  • 我明白你现在在说什么,我刚刚注意到 Game 是 GameView 的子类。这简直是​​疯了。但是java程序员倾向于直接去参考指南。并且根本不要阅读开发者指南。
  • 您好,构建布局的标准方式是什么?
【解决方案2】:

setContent 不应该在 onCreate 之后完成吗?当您都 findViewById 时,您的应用程序如何知道要查找的布局。

【讨论】:

  • 或者这可能是@Pyrodante 所说的,AndroidManifest 中缺少 Activity。总而言之,由于您没有从 logcat 粘贴错误,因此您得到了负面标记。
  • 不过很好。 TBH 我没有深入研究它并给出了更基本的答案,我已将您的观察添加到我的答案中并给了您一个支持。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-19
  • 2012-06-04
  • 1970-01-01
  • 1970-01-01
  • 2015-03-25
  • 1970-01-01
  • 2012-10-04
相关资源
最近更新 更多