运行startActivity(intent)时新意图崩溃;

时间:2014-01-15 07:11:10

标签: android android-intent android-activity

我试图在选项菜单中调用main中的另一个活动。当我从自己的选项菜单中调用主活动时,它将正常工作,但调用另一个意图会产生异常,应用程序将崩溃。我使用了try和catch,发现它到达这一行时会崩溃:

startActivity(intent);

下面是我的主要java文件和应用程序清单文件。

public class MainActivity extends Activity {

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

    ListView listView = (ListView) findViewById(R.id.times_list);
    TimeTrackerAdapter timeTrackerAdapter = new TimeTrackerAdapter();
    listView.setAdapter(timeTrackerAdapter);
}

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

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item){
    if (item.getItemId() == R.id.add_time_menu_item) {
        Intent intent = new Intent(this, AddTimeActivity.class);
        try {
        startActivity(intent);
        }catch(Exception e){};
        //return true;
    }
    return super.onOptionsItemSelected(item);
}

public class TimeTrackerAdapter extends BaseAdapter {

    private ArrayList<TimeRecord> times = new ArrayList<TimeRecord>();

    public TimeTrackerAdapter() {
        times.add(new TimeRecord("38:23", "Feeling good!"));
        times.add(new TimeRecord("49:01", "Tired. Needed more caffeine"));
        times.add(new TimeRecord("26:21", "I’m rocking it!"));
        times.add(new TimeRecord("29:42",
                "Lost some time on the hills, but pretty good."));
    }

    @Override
    public int getCount() {
        return times.size();
    }

    @Override
    public Object getItem(int index) {
        return times.get(index);
    }

    @Override
    public long getItemId(int index) {
        return index;
    }

    @Override
    public View getView(int index, View view, ViewGroup parent) {
        if (view == null) {
            LayoutInflater inflater = LayoutInflater.from(parent
                    .getContext());
            view = inflater.inflate(R.layout.time_list_item, parent, false);
        }
        TimeRecord time = times.get(index);

        TextView timeTextView = (TextView) view
                .findViewById(R.id.time_view);
        timeTextView.setText(time.getTime());

        TextView notesTextView = (TextView) view
                .findViewById(R.id.notes_view);
        notesTextView.setText(time.getNotes());

        return view;
    }
}

public class TimeRecord {
    private String time;
    private String notes;

    public TimeRecord(String time, String notes) {
        this.time = time;
        this.notes = notes;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getNotes() {
        return notes;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }
}

public class AddTimeActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_time);
    }

    public boolean onCreateOptionsMenu(Menu m) {
        super.onCreateOptionsMenu(m);
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.time_list_menu, m);
        return true;
    }
}

}

和manirfest:

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

<uses-sdk
    android:minSdkVersion="3"
    android:targetSdkVersion="18" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.test02.MainActivity"
        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="com.example.test02.AddTimeActivity" >
    </activity>
</application>

</manifest>

logcat的:

01-15 02:20:01.164: W/dalvikvm(3018): threadid=1: thread exiting with uncaught exception (group=0x41465700)
01-15 02:20:01.414: E/AndroidRuntime(3018): FATAL EXCEPTION: main
01-15 02:20:01.414: E/AndroidRuntime(3018): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.test02/com.example.test02.MainActivity$AddTimeActivity}; have you declared this activity in your AndroidManifest.xml?
01-15 02:20:01.414: E/AndroidRuntime(3018): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
01-15 02:20:01.414: E/AndroidRuntime(3018): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
01-15 02:20:01.414: E/AndroidRuntime(3018): at android.app.Activity.startActivityForResult(Activity.java:3390)
01-15 02:20:01.414: E/AndroidRuntime(3018): at android.app.Activity.startActivityForResult(Activity.java:3351)
01-15 02:20:01.414: E/AndroidRuntime(3018): at android.app.Activity.startActivity(Activity.java:3587)
01-15 02:20:01.414: E/AndroidRuntime(3018): at android.app.Activity.startActivity(Activity.java:3555)
01-15 02:20:01.414: E/AndroidRuntime(3018): at com.example.test02.MainActivity.onMenuItemSelected(MainActivity.java:44)
01-15 02:20:01.414: E/AndroidRuntime(3018): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java??:986)
01-15 02:20:01.414: E/AndroidRuntime(3018): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.??java:735)
01-15 02:20:01.414: E/AndroidRuntime(3018): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
01-15 02:20:01.414: E/AndroidRuntime(3018): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:87??4)
01-15 02:20:01.414: E/AndroidRuntime(3018): at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.j??ava:166)
01-15 02:20:01.414: E/AndroidRuntime(3018): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
01-15 02:20:01.414: E/AndroidRuntime(3018): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
01-15 02:20:01.414: E/AndroidRuntime(3018): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2788)
01-15 02:20:01.414: E/AndroidRuntime(3018): at android.widget.AbsListView$1.run(AbsListView.java:3463)
01-15 02:20:01.414: E/AndroidRuntime(3018): at android.os.Handler.handleCallback(Handler.java:730)
01-15 02:20:01.414: E/AndroidRuntime(3018): at android.os.Handler.dispatchMessage(Handler.java:92)
01-15 02:20:01.414: E/AndroidRuntime(3018): at android.os.Looper.loop(Looper.java:137)
01-15 02:20:01.414: E/AndroidRuntime(3018): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-15 02:20:01.414: E/AndroidRuntime(3018): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 02:20:01.414: E/AndroidRuntime(3018): at java.lang.reflect.Method.invoke(Method.java:525)
01-15 02:20:01.414: E/AndroidRuntime(3018): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-15 02:20:01.414: E/AndroidRuntime(3018): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 

4 个答案:

答案 0 :(得分:3)

将其添加到AndroidManifest.xml

<activity
   android:name="com.example.test02.AddTimeActivity"
   android:label="@string/app_name">
 <intent-filter>
   <action android:name="android.intent.action.AddTimeActivity" />
   <category android:name="android.intent.category.DEFAULT" />
 </intent-filter>
</activity>

答案 1 :(得分:1)

您是不是将活动放在同一个文件中?

如果您真的想在另一个活动中将活动用作内部类,则需要将其放入Android清单中:

   <activity android:name="com.example.test02.MainActivity$AddTimeActivity" >

我不推荐这个。将内部类拆分为两个不同的类文件。

在包/文件夹中:com.example.test02您需要将其分开并生成两个文件AddTimeActivity.classMainActivity.class,然后您无需更改清单中的任何内容。

您是否尝试确保包名称匹配?您可以在评论中发布您的导入/包名称以进一步澄清吗?

答案 2 :(得分:0)

在此处,您可以在MainActivity类中创建AddTimeActivity类。

注意:在不需要的时候在一个类中写入多个嵌套类不是一个好的编码标准。

按照以下步骤操作:

1st:只需从MainActivity中剪切AddTimeActivity并创建一个新的。

第二:在你的表现中,只需将其用作:

<activity android:name=".AddTimeActivity" />

并检查..

答案 3 :(得分:0)

如果您想在应用中使用显式意图,或者想要使用以下代码启动其他活动:

Intent intent_name = new Intent(MainActivity.this,ActivityName.class);

startActivity(intent_name);

如果您的应用崩溃了,那么您应该编辑AndroidManifest.xml文件并定义您要开始的活动。

例如:

  • 如果您要启动的活动名称为MyActivity且您的包名称为com.mycomp.myapp,那么您应该添加以下定义:

    <activity android:name="com.mycomp.myapp.MyActivity" />