实例化新活动可使应用程序崩溃

时间:2013-02-01 17:26:11

标签: java android android-intent

我是Android应用开发的新手,现在正在尝试编写第一个应用。当我想显示另一个Activity时,应用程序崩溃时会显示以下日志(取自LogCat):

02-01 18:05:50.148: D/HomeScreen(24970): news_button clicked
02-01 18:05:50.203: D/dalvikvm(24970): newInstance failed: p0 i0 [0 a1
02-01 18:05:50.210: D/AndroidRuntime(24970): Shutting down VM
02-01 18:05:50.210: W/dalvikvm(24970): threadid=1: thread exiting with uncaught exception (group=0x40018578)
02-01 18:05:50.218: E/AndroidRuntime(24970): FATAL EXCEPTION: main
02-01 18:05:50.218: E/AndroidRuntime(24970): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{me.the_Seppi.freakhall/me.the_Seppi.freakhall.NewsScreen}: java.lang.InstantiationException: me.the_Seppi.freakhall.NewsScreen
02-01 18:05:50.218: E/AndroidRuntime(24970):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
02-01 18:05:50.218: E/AndroidRuntime(24970):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
02-01 18:05:50.218: E/AndroidRuntime(24970):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-01 18:05:50.218: E/AndroidRuntime(24970):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
02-01 18:05:50.218: E/AndroidRuntime(24970):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-01 18:05:50.218: E/AndroidRuntime(24970):    at android.os.Looper.loop(Looper.java:130)
02-01 18:05:50.218: E/AndroidRuntime(24970):    at android.app.ActivityThread.main(ActivityThread.java:3687)
02-01 18:05:50.218: E/AndroidRuntime(24970):    at java.lang.reflect.Method.invokeNative(Native Method)
02-01 18:05:50.218: E/AndroidRuntime(24970):    at java.lang.reflect.Method.invoke(Method.java:507)
02-01 18:05:50.218: E/AndroidRuntime(24970):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
02-01 18:05:50.218: E/AndroidRuntime(24970):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
02-01 18:05:50.218: E/AndroidRuntime(24970):    at dalvik.system.NativeStart.main(Native Method)
02-01 18:05:50.218: E/AndroidRuntime(24970): Caused by: java.lang.InstantiationException: me.the_Seppi.freakhall.NewsScreen
02-01 18:05:50.218: E/AndroidRuntime(24970):    at java.lang.Class.newInstanceImpl(Native Method)
02-01 18:05:50.218: E/AndroidRuntime(24970):    at java.lang.Class.newInstance(Class.java:1409)
02-01 18:05:50.218: E/AndroidRuntime(24970):    at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-01 18:05:50.218: E/AndroidRuntime(24970):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
02-01 18:05:50.218: E/AndroidRuntime(24970):    ... 11 more

执行的代码是:

//HomeScreen.java
//...
public void gotoNews (View view) {
    Log.d(TAG, "news_button clicked");
    Intent news = new Intent(this, NewsScreen.class);
    startActivity(news);
}



//NewsScreen.java
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_news_screen);
    Log.d("NewsScreen", "created");
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(R.string.update_text).setTitle(R.string.update_title).create().show();
    Log.d("NewsScreen", "Started update");  
}

我在代码中找不到错误,因为日志中没有行号。 请帮帮我。

4 个答案:

答案 0 :(得分:1)

您是否将此NewScreen活动添加到清单文件中?这是崩溃活动中最常见的问题。

答案 1 :(得分:0)

您需要将Activity添加到要重定向到的清单文件中 离。

  <activity android:name="sanatan.engine.MainActivity" />

答案 2 :(得分:0)

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="me.the_Seppi.freakhall"
    android:versionCode="1"
    android:versionName="1.0" android:installLocation="auto">

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="me.the_Seppi.freakhall.HomeScreen"
            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="me.the_Seppi.freakhall.NewsScreen"
            android:label="@string/title_activity_news_screen" >
        </activity>
    </application>

</manifest>

NewsScreen.java:

public abstract class NewsScreen extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_screen);
        Log.d("NewsScreen", "created");
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(R.string.update_text).setTitle(R.string.update_title).create().show();
        Log.d("NewsScreen", "Started update");

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_news_screen, menu);
        return true;
    }

}

我希望这可以帮助您解决这个问题。

答案 3 :(得分:0)

问题解决了:只需重新创建活动NewsScreen及其所有资源。

但代码和清单条目是一样的。 =&GT;出了什么问题?