我刚刚开始编写 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
}
}
生命周期测试.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>
日志猫
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
提前致谢。