应用活动崩溃

时间:2014-10-26 19:42:00

标签: java android

这是我的第一篇文章,如果我违反任何规则,请原谅我。

我正在使用ADT编写应用程序,当我通过按下按钮启动我的一项活动时,应用程序会一直崩溃。当我在其中的一个按钮中运行一个漫长而复杂的Java时,该活动才开始给我带来麻烦。在将Netbeans移植到应用程序之前,我测试了Netbeans中的Java,并且它运行良好。当我第一次将代码添加到src文件时,ADT拒绝它,因为我使用了一个字符串开关,它只在java 1.7中兼容(我用1.6编译而我的android设备有4.2.2只支持1.6)。重新编写没有字符串开关的代码后,使用1.6(Android版本4.3.2)编译的代码没有错误,但一旦在设备上运行(Android版4.2.2植根于HyperDrive Rom),运行运行Java的活动导致应用程序崩溃 我应该发布什么代码,以便有人可以查看它并发现任何错误?

编辑:好的伙计们这里是Log Cat:
10-26 19:19:27.223: W/ActivityThread(22541): Application com.example.cube can be debugged on port 8100... 10-26 19:19:27.603: D/libEGL(22541): loaded /system/lib/egl/libEGL_adreno200.so 10-26 19:19:27.613: D/libEGL(22541): loaded /system/lib/egl/libGLESv1_CM_adreno200.so 10-26 19:19:27.643: D/libEGL(22541): loaded /system/lib/egl/libGLESv2_adreno200.so 10-26 19:19:27.653: I/Adreno200-EGL(22541): <qeglDrvAPI_eglInitialize:265>: EGL 1.4 QUALCOMM build: (CL3579225) 10-26 19:19:27.653: I/Adreno200-EGL(22541): Build Date: 04/22/13 Mon 10-26 19:19:27.653: I/Adreno200-EGL(22541): Local Branch: adreno_sba_20130419 10-26 19:19:27.653: I/Adreno200-EGL(22541): Remote Branch: 10-26 19:19:27.653: I/Adreno200-EGL(22541): Local Patches: 10-26 19:19:27.653: I/Adreno200-EGL(22541): Reconstruct Branch: 10-26 19:19:27.963: D/OpenGLRenderer(22541): Enabling debug mode 0 10-26 19:19:29.605: D/GestureDetector(22541): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 4 mFalseSizeCnt:0 10-26 19:19:30.096: E/SpannableStringBuilder(22541): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 10-26 19:19:30.096: E/SpannableStringBuilder(22541): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 10-26 19:19:30.956: D/GestureDetector(22541): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 6 mFalseSizeCnt:0 10-26 19:19:31.407: E/SpannableStringBuilder(22541): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 10-26 19:19:31.407: E/SpannableStringBuilder(22541): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 10-26 19:19:33.369: D/GestureDetector(22541): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 5 mFalseSizeCnt:0 10-26 19:19:34.250: E/SpannableStringBuilder(22541): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 10-26 19:19:34.250: E/SpannableStringBuilder(22541): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 10-26 19:19:35.391: D/GestureDetector(22541): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 8 mFalseSizeCnt:0 10-26 19:19:35.571: D/AndroidRuntime(22541): Shutting down VM 10-26 19:19:35.571: W/dalvikvm(22541): threadid=1: thread exiting with uncaught exception (group=0x41b906c0) 10-26 19:19:35.591: E/AndroidRuntime(22541): FATAL EXCEPTION: main 10-26 19:19:35.591: E/AndroidRuntime(22541): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.cube/com.example.cube.AlgorithmConverter}: java.lang.NullPointerException 10-26 19:19:35.591: E/AndroidRuntime(22541): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173) 10-26 19:19:35.591: E/AndroidRuntime(22541): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297) 10-26 19:19:35.591: E/AndroidRuntime(22541): at android.app.ActivityThread.access$700(ActivityThread.java:152) 10-26 19:19:35.591: E/AndroidRuntime(22541): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282) 10-26 19:19:35.591: E/AndroidRuntime(22541): at android.os.Handler.dispatchMessage(Handler.java:99) 10-26 19:19:35.591: E/AndroidRuntime(22541): at android.os.Looper.loop(Looper.java:137) 10-26 19:19:35.591: E/AndroidRuntime(22541): at android.app.ActivityThread.main(ActivityThread.java:5328) 10-26 19:19:35.591: E/AndroidRuntime(22541): at java.lang.reflect.Method.invokeNative(Native Method) 10-26 19:19:35.591: E/AndroidRuntime(22541): at java.lang.reflect.Method.invoke(Method.java:511) 10-26 19:19:35.591: E/AndroidRuntime(22541): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 10-26 19:19:35.591: E/AndroidRuntime(22541): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 10-26 19:19:35.591: E/AndroidRuntime(22541): at dalvik.system.NativeStart.main(Native Method) 10-26 19:19:35.591: E/AndroidRuntime(22541): Caused by: java.lang.NullPointerException 10-26 19:19:35.591: E/AndroidRuntime(22541): at android.app.Activity.findViewById(Activity.java:1886) 10-26 19:19:35.591: E/AndroidRuntime(22541): at com.example.cube.AlgorithmConverter.<init>(AlgorithmConverter.java:15) 10-26 19:19:35.591: E/AndroidRuntime(22541): at java.lang.Class.newInstanceImpl(Native Method) 10-26 19:19:35.591: E/AndroidRuntime(22541): at java.lang.Class.newInstance(Class.java:1319) 10-26 19:19:35.591: E/AndroidRuntime(22541): at android.app.Instrumentation.newActivity(Instrumentation.java:1071) 10-26 19:19:35.591: E/AndroidRuntime(22541): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2164) 10-26 19:19:35.591: E/AndroidRuntime(22541): ... 11 more
我确信XML是合理的,但是java很冗长,即使有了评论也很难理解,所以我对发布它犹豫不决。
请帮忙!
非常感谢!!

1 个答案:

答案 0 :(得分:0)

This提供了一个如何让按钮开始新活动的好例子。

Mkyong的帖子提供的关键信息:

在您希望按钮放置以下内容的活动中,或者如果您已经有一个按钮给它一个id,或者您已经完成了下一步的所有操作。

<Button
        android:id="@+id/button1" <!-- Id of your button -->
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Click me to another screen" />

您的主要活动(或按钮所在的活动应包含以下内容:

package com.mkyong.android; // Name of package, change if necessary

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

public class AppActivity extends Activity {

    Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        addListenerOnButton();
    }

    public void addListenerOnButton() {

        final Context context = this;

        button = (Button) findViewById(R.id.button1); // ID of your button, change if necessary

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(context, App2Activity.class); // Name of second activity's class, change if necessary
                            startActivity(intent);   

            }

        });

    }

这就是它,只要你为第二个活动或XML设置了一个类,那么你就不需要再进一步了。如果您没有第二个活动的课程,请创建一个并放在以下代码中:

package com.mkyong.android; //name of package, change if necessary 

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

public class App2Activity extends Activity {

    Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2); // Name of second XML, change if necessary
    }

}

由于您不需要复杂的Java来执行此操作,因此使用上述更简单的替代方法会更好并且可以最大限度地减少问题。