Android应用在startActivity上崩溃了

时间:2014-07-28 15:23:52

标签: java android xml eclipse android-intent

我从互联网上下载了一个数据库,并且我试图在它上面构建我的应用程序。它已经过测试并且有效。当我尝试打开新活动时问题就开始了

我有一个名为" New"在我的主要布局上应该在我的应用程序中打开一个新窗口(现在只打开一个新活动),但它会在通话时立即崩溃。我当前的活动是DatabaseExampleActivity(我的主要活动),我希望新活动为NewAssignmentActivity

以下是相关的代码部分:

//" new"时调用的函数按下按钮

public void newAssignment(View view) {
    Intent i = new Intent(DatabaseExampleActivity.this, NewAssignmentActivity.class);
    startActivity(i);
    }

//清单

<activity
android:name=".DatabaseExampleActivity"
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=".NewAssignmentActivity"
        android:label="@string/title_activity_new_assignment" >
    </activity>

//新活动代码

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class NewAssignmentActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_newassignment);
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

consol writes in red : ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.anotherandroidblog.tools.database/.DatabaseExampleActivity }
ActivityManager: Warning: Activity not started, its current task has been brought to the front

logCat也会产生大量错误:

07-28 18:29:39.792: E/ERROR(5847): java.lang.NullPointerException
07-28 18:29:39.792: W/System.err(5847): java.lang.NullPointerException
07-28 18:29:39.792: W/System.err(5847):     at com.anotherandroidblog.tools.database.DatabaseExampleActivity.addButtonListeners(DatabaseExampleActivity.java:104)
07-28 18:29:39.792: W/System.err(5847):     at com.anotherandroidblog.tools.database.DatabaseExampleActivity.onCreate(DatabaseExampleActivity.java:51)
07-28 18:29:39.792: W/System.err(5847):     at android.app.Activity.performCreate(Activity.java:5206)
07-28 18:29:39.792: W/System.err(5847):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-28 18:29:39.792: W/System.err(5847):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
07-28 18:29:39.792: W/System.err(5847):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
07-28 18:29:39.792: W/System.err(5847):     at android.app.ActivityThread.access$700(ActivityThread.java:140)
07-28 18:29:39.792: W/System.err(5847):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
07-28 18:29:39.792: W/System.err(5847):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-28 18:29:39.792: W/System.err(5847):     at android.os.Looper.loop(Looper.java:137)
07-28 18:29:39.792: W/System.err(5847):     at android.app.ActivityThread.main(ActivityThread.java:4921)
07-28 18:29:39.792: W/System.err(5847):     at java.lang.reflect.Method.invokeNative(Native Method)
07-28 18:29:39.792: W/System.err(5847):     at java.lang.reflect.Method.invoke(Method.java:511)
07-28 18:29:39.792: W/System.err(5847):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
07-28 18:29:39.792: W/System.err(5847):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
07-28 18:29:39.792: W/System.err(5847):     at dalvik.system.NativeStart.main(Native Method)
07-28 18:29:59.301: I/brcm-gr(5847): [gralloc_lock]: new usage 0x933
07-28 18:29:59.301: D/AndroidRuntime(5847): Shutting down VM
07-28 18:29:59.301: W/dalvikvm(5847): threadid=1: thread exiting with uncaught exception (group=0x41bcf2a0)
07-28 18:29:59.311: E/AndroidRuntime(5847): FATAL EXCEPTION: main
07-28 18:29:59.311: E/AndroidRuntime(5847): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.anotherandroidblog.tools.database/com.anotherandroidblog.tools.database.NewAssignmentActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
07-28 18:29:59.311: E/AndroidRuntime(5847):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
07-28 18:29:59.311: E/AndroidRuntime(5847):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
07-28 18:29:59.311: E/AndroidRuntime(5847):     at android.app.ActivityThread.access$700(ActivityThread.java:140)
07-28 18:29:59.311: E/AndroidRuntime(5847):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
07-28 18:29:59.311: E/AndroidRuntime(5847):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-28 18:29:59.311: E/AndroidRuntime(5847):     at android.os.Looper.loop(Looper.java:137)
07-28 18:29:59.311: E/AndroidRuntime(5847):     at android.app.ActivityThread.main(ActivityThread.java:4921)
07-28 18:29:59.311: E/AndroidRuntime(5847):     at java.lang.reflect.Method.invokeNative(Native Method)
07-28 18:29:59.311: E/AndroidRuntime(5847):     at java.lang.reflect.Method.invoke(Method.java:511)
07-28 18:29:59.311: E/AndroidRuntime(5847):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
07-28 18:29:59.311: E/AndroidRuntime(5847):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
07-28 18:29:59.311: E/AndroidRuntime(5847):     at dalvik.system.NativeStart.main(Native Method)
07-28 18:29:59.311: E/AndroidRuntime(5847): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
07-28 18:29:59.311: E/AndroidRuntime(5847):     at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:110)
07-28 18:29:59.311: E/AndroidRuntime(5847):     at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
07-28 18:29:59.311: E/AndroidRuntime(5847):     at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:99)
07-28 18:29:59.311: E/AndroidRuntime(5847):     at com.anotherandroidblog.tools.database.NewAssignmentActivity.onCreate(NewAssignmentActivity.java:12)
07-28 18:29:59.311: E/AndroidRuntime(5847):     at android.app.Activity.performCreate(Activity.java:5206)
07-28 18:29:59.311: E/AndroidRuntime(5847):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-28 18:29:59.311: E/AndroidRuntime(5847):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)

1 个答案:

答案 0 :(得分:2)

查看崩溃日志:

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

NewAssignmentActivity扩展ActionBarActivity,而非Activity,因此您必须使用Theme.AppCompat。将AndroidManifest中的活动或应用程序主题设置为

android:theme="@style/Theme.AppCompat"