【问题标题】:App Crash due to start of activity that launches asset activity由于启动资产活动的活动开始导致应用程序崩溃
【发布时间】:2015-12-31 01:18:01
【问题描述】:

我正处于学习阶段,我正在构建一个应用程序来测试系统活动。这是主要活动的代码:

public class MainActivity extends ListActivity {

String tests[] = { "LifeCycleTest", "SingleTouchTest", "MultiTouchTest",
        "KeyTest", "AccelerometerTest", "AssetsTest",
        "ExternalStorageTest", "SoundPoolTest", "MediaPlayerTest",
        "FullScreenTest", "RenderViewTest", "ShapeTest", "BitmapTest",
        "FontTest", "SurfaceViewTest" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<>(this,
            android.R.layout.simple_list_item_1, tests));
}

@Override
protected void onListItemClick(ListView list, View view, int position, long id) {
    super.onListItemClick(list, view, position, id);
    String testName = tests[position];
    try {
        Class c = Class.forName("com.ag.systemtests." + testName);
        Intent intent = new Intent(this, c);
        startActivity(intent);
    } catch (ClassNotFoundException e) {  e.printStackTrace();  }
}
}

当我运行应用程序时,它运行良好,直到我选择 AssestsTest 选项。活动 AssetsTest 的代码是:

import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.widget.TextView;

public class AssetsTest extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        TextView textView = new TextView(this);
        setContentView(textView);

        AssetManager assetManager = getAssets();


        InputStream input;
        try {
            input = assetManager.open("myawesometext.txt");

            int size = input.available();
            byte[] buffer = new byte[size];
            input.read(buffer);
            input.close();

            // byte buffer into a string
            String text = new String(buffer);

            textView.setText(text);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

当我选择此活动时,应用程序崩溃。错误出现在 startActivity(intent) 行。我该怎么办?

logcat 是:

10-03 00:15:59.186  24699-24699/com.androidgames.systemtests I/art﹕ Late-enabling -Xcheck:jni
10-03 00:15:59.232  24699-24710/com.androidgames.systemtests I/art﹕ Debugger is no longer active
10-03 00:15:59.392  24699-24722/com.androidgames.systemtests D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
10-03 00:15:59.402  24699-24699/com.androidgames.systemtests D/Atlas﹕ Validating map...
10-03 00:15:59.444  24699-24722/com.androidgames.systemtests I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build:  ()
    OpenGL ES Shader Compiler Version: E031.25.03.04
    Build Date: 04/06/15 Mon
    Local Branch:
    Remote Branch:
    Local Patches:
    Reconstruct Branch:
10-03 00:15:59.446  24699-24722/com.androidgames.systemtests I/OpenGLRenderer﹕ Initialized EGL, version 1.4
10-03 00:15:59.508  24699-24722/com.androidgames.systemtests D/OpenGLRenderer﹕ Enabling debug mode 0
10-03 00:16:09.783  24699-24699/com.androidgames.systemtests D/AndroidRuntime﹕ Shutting down VM
10-03 00:16:09.801  24699-24699/com.androidgames.systemtests E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.androidgames.systemtests, PID: 24699
    android.content.ActivityNotFoundException: Unable to find explicit activity class {com.androidgames.systemtests/com.androidgames.systemtests.AssetsTest}; have you declared this activity in your AndroidManifest.xml?
            at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1868)
            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1568)
            at android.app.Activity.startActivityForResult(Activity.java:3755)
            at android.app.Activity.startActivityForResult(Activity.java:3716)
            at android.app.Activity.startActivity(Activity.java:4036)
            at android.app.Activity.startActivity(Activity.java:3998)
            at com.androidgames.systemtests.AndroidBasicsStarter.onListItemClick(AndroidBasicsStarter.java:32)
            at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
            at android.widget.AdapterView.performItemClick(AdapterView.java:305)
            at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
            at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053)
            at android.widget.AbsListView$3.run(AbsListView.java:3860)
            at android.os.Handler.handleCallback(Handler.java:746)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5343)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

【问题讨论】:

  • 什么是崩溃。粘贴 logcat。
  • 我已经发布了 logcat。
  • 您是否在 AndroidManifest.xml 中声明了 assetstest 活动?
  • @PräTîkTank 这不起作用。它以红色显示 setContentView(textView) 的 textView,因为 textView 未初始化。如果 AssetsTest 活动作为单独的应用程序运行,则它运行良好。
  • @Tauqir 惊人地被抓住了。就是这样。它没有在清单文件中声明。万分感谢。我已经玩了大约一个小时。谢谢。

标签: android android-intent android-assets


【解决方案1】:

将此&lt;activity android:name=".AssetsTest"&gt;&lt;/activity&gt; 插入您的AndroidManifest.xml

参考访问 have you declared this activity in your AndroidManifest.xml

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多