无法使用Intent从ListActivity启动新活动

时间:2013-10-10 18:32:41

标签: android android-intent android-activity listactivity

我刚刚开始编写Android应用程序,我一直在编写一本书。该应用程序只是假设从列表活动中运行一堆测试活动家。我测试列表活动,它工作正常。我还尝试在活动列表上测试第一个活动,它只是试图加载它而陷入困境,并且会继续尝试加载它。除了包名之外,我的代码实际上是逐字逐句的。我知道它找到了这个类,它只是活动没有启动而且我不知道为什么。我觉得我只是缺少简单的东西或忽略了一个小错误。这是我到目前为止应用程序的三个文件。我很确定它与意图有关 AndroidBasicStarter.java,但我包括所有内容以防它不是。

编辑:我在加载LifeCycleTest.java时遇到问题。我没有编写任何更多的测试代码,因为第一个测试不起作用,我不知道为什么。

AndroidBasicStarter.java

package com.dom.starter;

import android.os.Bundle;
import android.content.Intent;
import android.app.ListActivity;
import android.view.View; 
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class AndroidBasicStarter extends ListActivity 
{
    String tests[] = { "LifeCycleTest", "SingleTouchTest", "MultiTouchTest", 
    "KeyTest", "AccelerometerTest", "AssestsTest", "ExternalStorageTest",
    "SoundPoolTest", "MediaPlayerTest", "FullScreenTest", "RenderViewTest",
    "ShapeTest", "BitmapTest", "FontTest", "SurfaceTest"};

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String> 
               (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 clazz = Class.forName("com.dom.starter." + testName);
            Intent intent = new Intent(AndroidBasicStarter.this,clazz);
            startActivity(intent);//problem here maybe?
        }//end try

        catch(ClassNotFoundException e)
        {
            e.printStackTrace();
        }//end catch
    }

}

LifeCycleTest.java

package com.dom.starter;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class LifeCycleTest extends Activity
{
    StringBuilder builder = new StringBuilder();
    TextView textView;

    private void log(String text)
    {
        Log.d("LifeCycleTest", text);
        builder.append(text);
        builder.append('\n');
        textView.setText(builder.toString());
    }


    @Override
    public void onCreate(Bundle saveInstanceState)
    {
        super.onSaveInstanceState(saveInstanceState);
    textView = new TextView(this);
    textView.setText(builder.toString());
    setContentView(textView);
    log("Created");
    }


    @Override
    protected void onResume()
    {
        super.onResume();
        log("Resumed");
    }


    @Override
    protected void onPause()
    {
        super.onPause();
        log("Paused");

        if(isFinishing())
            log("Finishing");
    }   
}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dom.starter"
    android:versionCode="1"
    android:versionName="1.0">

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
        android:name="AndroidBasicStarter"
        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="LifeCycleTest"
        android:label="Life Cycle Test" 
        android:configChanges="keyboard|keyboardHidden|orientation"/>
</application>

logcat的

10-10 12:50:39.807: E/AndroidRuntime(31182): FATAL EXCEPTION: main
10-10 12:50:39.807: E/AndroidRuntime(31182): java.lang.RuntimeException: Unable to   start activity ComponentInfo{com.dom.starter/com.dom.starter.LifeCycleTest}: java.lang.NullPointerException
10-10 12:50:39.807: E/AndroidRuntime(31182):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
10-10 12:50:39.807: E/AndroidRuntime(31182):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
10-10 12:50:39.807: E/AndroidRuntime(31182):    at android.app.ActivityThread.access$700(ActivityThread.java:143)
10-10 12:50:39.807: E/AndroidRuntime(31182):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
10-10 12:50:39.807: E/AndroidRuntime(31182):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 12:50:39.807: E/AndroidRuntime(31182):    at android.os.Looper.loop(Looper.java:137)
10-10 12:50:39.807: E/AndroidRuntime(31182):    at android.app.ActivityThread.main(ActivityThread.java:4950)
10-10 12:50:39.807: E/AndroidRuntime(31182):    at java.lang.reflect.Method.invokeNative(Native Method)
10-10 12:50:39.807: E/AndroidRuntime(31182):    at java.lang.reflect.Method.invoke(Method.java:511)
10-10 12:50:39.807: E/AndroidRuntime(31182):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
10-10 12:50:39.807: E/AndroidRuntime(31182):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
10-10 12:50:39.807: E/AndroidRuntime(31182):    at dalvik.system.NativeStart.main(Native Method)
10-10 12:50:39.807: E/AndroidRuntime(31182): Caused by: java.lang.NullPointerException
10-10 12:50:39.807: E/AndroidRuntime(31182):    at android.app.Activity.onSaveInstanceState(Activity.java:1222)
10-10 12:50:39.807: E/AndroidRuntime(31182):    at com.dom.starter.LifeCycleTest.onCreate(LifeCycleTest.java:26)
10-10 12:50:39.807: E/AndroidRuntime(31182):    at android.app.Activity.performCreate(Activity.java:5179)
10-10 12:50:39.807: E/AndroidRuntime(31182):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
10-10 12:50:39.807: E/AndroidRuntime(31182):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
10-10 12:50:39.807: E/AndroidRuntime(31182):    ... 11 more

提前致谢。

2 个答案:

答案 0 :(得分:2)

在LifeCycleTest.java中的onCreate中它应该是super.onCreate(saveInstanceState);而不是super.onSaveInstanceState(saveInstanceState);

答案 1 :(得分:0)

根据您的代码,只有LifeCycleTest可以使用,您需要在Manifest中添加所有类。
更改 setContentView(textView); setContentView(R.layout.life_cycle_test);