我有一個操作菜單,我試圖啓動另一個活動,一旦圖標被點擊,但它導致錯誤恐慌。事實上,它似乎是走錯了路。這是我的意思。這裏的代碼是:非常奇怪的班級混亂
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.mapIcon) {
Intent displayTheMap = new Intent(this, TheMap.class);
startActivity(displayTheMap);
return true;
}
return super.onOptionsItemSelected(item);
}
但它返回:
04-18 06:19:16.201: E/AndroidRuntime(2079): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gs.maps/com.gs.maps.TheMap}: java.lang.IllegalArgumentException: No view found for id 0x7f06003f (com.gs.maps:id/container) for fragment PlaceholderFragment{b3d8a760 #0 id=0x7f06003f}
這究竟是爲什麼?它應該找到com.gs.maps.TheMap
而不是com.gs.maps/com.gs.maps.TheMap
爲什麼它得到一個錯誤的道路?我錯過了什麼?我知道這只是一小部分,但我目前無法找到解決方案。
完全logcat的例外:
04-18 06:19:16.201: E/AndroidRuntime(2079): FATAL EXCEPTION: main
04-18 06:19:16.201: E/AndroidRuntime(2079): Process: com.gs.maps, PID: 2079
04-18 06:19:16.201: E/AndroidRuntime(2079): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gs.maps/com.gs.maps.TheMap}: java.lang.IllegalArgumentException: No view found for id 0x7f06003f (com.gs.maps:id/container) for fragment PlaceholderFragment{b3d8a760 #0 id=0x7f06003f}
04-18 06:19:16.201: E/AndroidRuntime(2079): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-18 06:19:16.201: E/AndroidRuntime(2079): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-18 06:19:16.201: E/AndroidRuntime(2079): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-18 06:19:16.201: E/AndroidRuntime(2079): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-18 06:19:16.201: E/AndroidRuntime(2079): at android.os.Handler.dispatchMessage(Handler.java:102)
04-18 06:19:16.201: E/AndroidRuntime(2079): at android.os.Looper.loop(Looper.java:136)
04-18 06:19:16.201: E/AndroidRuntime(2079): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-18 06:19:16.201: E/AndroidRuntime(2079): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 06:19:16.201: E/AndroidRuntime(2079): at java.lang.reflect.Method.invoke(Method.java:515)
04-18 06:19:16.201: E/AndroidRuntime(2079): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-18 06:19:16.201: E/AndroidRuntime(2079): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-18 06:19:16.201: E/AndroidRuntime(2079): at dalvik.system.NativeStart.main(Native Method)
04-18 06:19:16.201: E/AndroidRuntime(2079): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f06003f (com.gs.maps:id/container) for fragment PlaceholderFragment{b3d8a760 #0 id=0x7f06003f}
04-18 06:19:16.201: E/AndroidRuntime(2079): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:930)
04-18 06:19:16.201: E/AndroidRuntime(2079): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
04-18 06:19:16.201: E/AndroidRuntime(2079): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-18 06:19:16.201: E/AndroidRuntime(2079): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
04-18 06:19:16.201: E/AndroidRuntime(2079): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
04-18 06:19:16.201: E/AndroidRuntime(2079): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
04-18 06:19:16.201: E/AndroidRuntime(2079): at android.app.Activity.performStart(Activity.java:5241)
04-18 06:19:16.201: E/AndroidRuntime(2079): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
04-18 06:19:16.201: E/AndroidRuntime(2079): ... 11 more
清單:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gs.maps"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="xxxx" />
<activity
android:name="com.gs.maps.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.gs.maps.TheMap"
android:label="@string/title_activity_the_gay_map" >
</activity>
</application>
</manifest>
編輯
的java的主類:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater actionMenue = getMenuInflater();
actionMenue.inflate(R.menu.main_activity_bar, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.mapIcon) {
Intent displayTheMap = new Intent(this, TheMap.class);
startActivity(displayTheMap);
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
「TheMap」是您的應用程序中的一項活動,如果是,您是否在manifest中聲明瞭它? – amj
您的錯誤顯示您正試圖加載到'container'中的'PlaceholderFragment'丟失。請張貼更多的代碼和完全的logcat錯誤 – GrIsHu
@amj這裏是'<活動 機器人:名字= 「com.gs.maps.TheMap」 機器人:標籤= 「@字符串/ title_activity_the_map」> ' – chility