我对这个问题坐了大约一天,我正在尝试添加一个菜单,我应该可以启动设置我正在尝试创建一个Android应用程序,它应该作为RC汽车的控制器。 我设法创建菜单,但每次我点击设置它都会崩溃。
我有很多其他代码用于蓝牙连接和控制器本身,我不知道它是否相关,所以我没有发布但是问我可以发布它:)
XML /的preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:title="@string/pref_title_rc_settings"
android:key="@string/pref_key_rc_settings">
<CheckBoxPreference
android:key="@string/pref_key_antispin"
android:summary="@string/pref_summary_antispin"
android:title="@string/pref_title_antispin"
android:defaultValue="true"/>
<CheckBoxPreference
android:key="@string/pref_key_speedlimit"
android:summary="@string/pref_summary_speedlimit"
android:title="@string/pref_title_speedlimit"
android:defaultValue="false"/>
<CheckBoxPreference
android:key="@string/pref_key_lysstyring"
android:summary="@string/pref_summary_lysstyring"
android:title="@string/pref_title_lysstyring"
android:defaultValue="true"/>
<CheckBoxPreference
android:key="@string/pref_key_kollisionssikring"
android:summary="@string/pref_summary_kollisionssikring"
android:title="@string/pref_title_kollisionssikring"/>
</PreferenceCategory>
</PreferenceScreen>
Main_Activity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.menu_settings:
Intent options1 = new Intent(this, SettingsFragment.class);
startActivity(options1);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public static class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
菜单/ main.xml中
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_settings"
android:title="@string/menu_settings"
android:orderInCategory="100"
android:showAsAction="never" />
</menu>
值/ strings.xml中
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Bluetooth</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main_">Main_Activity</string>
<string name="title_activity_remote">Remote</string>
<string name="action_settings">Settings</string>
<!-- Settings kategori -->
<string name="pref_key_rc_settings">settings_data</string>
<string name="pref_title_rc_settings">Settings</string>
<!-- Antispin -->
<string name="pref_key_antispin">antispin_data</string>
<string name="pref_summary_antispin">antispin_summary</string>
<string name="pref_title_antispin">Antispin</string>
<!-- Fartbegrænser -->
<string name="pref_key_speedlimit">speedlimit_data</string>
<string name="pref_summary_speedlimit">speedlimit_summary</string>
<string name="pref_title_speedlimit">Speed limiter</string>
<!-- Lysstyring -->
<string name="pref_key_lysstyring">lysstyring_data</string>
<string name="pref_summary_lysstyring">lysstyring_summary</string>
<string name="pref_title_lysstyring">Lightmode</string>
<!-- Kollisionssikring -->
<string name="pref_key_kollisionssikring">kollisionssikring_data</string>
<string name="pref_summary_kollisionssikring">kollisionssikring_summary</string>
<string name="pref_title_kollisionssikring">Collision protection</string>
</resources>
logcat的
04-16 11:24:51.882: D/libEGL(30759): loaded /system/lib/egl/libEGL_mali.so
04-16 11:24:51.882: D/libEGL(30759): loaded /system/lib/egl/libGLESv1_CM_mali.so
04-16 11:24:51.887: D/libEGL(30759): loaded /system/lib/egl/libGLESv2_mali.so
04-16 11:24:51.897: E/(30759): Device driver API match
04-16 11:24:51.897: E/(30759): Device driver API version: 23
04-16 11:24:51.897: E/(30759): User space API version: 23
04-16 11:24:51.897: E/(30759): mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Wed Oct 9 21:05:57 KST 2013
04-16 11:24:51.932: D/OpenGLRenderer(30759): Enabling debug mode 0
04-16 11:24:52.007: I/ActivityManager(30759): Timeline: Activity_idle id: android.os.BinderProxy@41d06e80 time:38223935
04-16 11:24:58.002: I/ActivityManager(30759): Timeline: Activity_launch_request id:com.test.bluetooth time:38229933
04-16 11:24:58.007: D/AndroidRuntime(30759): Shutting down VM
04-16 11:24:58.012: W/dalvikvm(30759): threadid=1: thread exiting with uncaught exception (group=0x41a13c80)
04-16 11:24:58.027: E/AndroidRuntime(30759): FATAL EXCEPTION: main
04-16 11:24:58.027: E/AndroidRuntime(30759): Process: com.test.bluetooth, PID: 30759
04-16 11:24:58.027: E/AndroidRuntime(30759): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.test.bluetooth/com.test.bluetooth.Main_Activity$SettingsFragment}; have you declared this activity in your AndroidManifest.xml?
04-16 11:24:58.027: E/AndroidRuntime(30759): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1636)
04-16 11:24:58.027: E/AndroidRuntime(30759): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
04-16 11:24:58.027: E/AndroidRuntime(30759): at android.app.Activity.startActivityForResult(Activity.java:3432)
04-16 11:24:58.027: E/AndroidRuntime(30759): at android.app.Activity.startActivityForResult(Activity.java:3393)
04-16 11:24:58.027: E/AndroidRuntime(30759): at android.app.Activity.startActivity(Activity.java:3635)
04-16 11:24:58.027: E/AndroidRuntime(30759): at android.app.Activity.startActivity(Activity.java:3603)
04-16 11:24:58.027: E/AndroidRuntime(30759): at com.test.bluetooth.Main_Activity.onOptionsItemSelected(Main_Activity.java:200)
04-16 11:24:58.027: E/AndroidRuntime(30759): at android.app.Activity.onMenuItemSelected(Activity.java:2608)
04-16 11:24:58.027: E/AndroidRuntime(30759): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1065)
04-16 11:24:58.027: E/AndroidRuntime(30759): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
04-16 11:24:58.027: E/AndroidRuntime(30759): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
04-16 11:24:58.027: E/AndroidRuntime(30759): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
04-16 11:24:58.027: E/AndroidRuntime(30759): at com.android.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:177)
04-16 11:24:58.027: E/AndroidRuntime(30759): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
04-16 11:24:58.027: E/AndroidRuntime(30759): at android.widget.AbsListView.performItemClick(AbsListView.java:1137)
04-16 11:24:58.027: E/AndroidRuntime(30759): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3048)
04-16 11:24:58.027: E/AndroidRuntime(30759): at android.widget.AbsListView$4.run(AbsListView.java:3789)
04-16 11:24:58.027: E/AndroidRuntime(30759): at android.os.Handler.handleCallback(Handler.java:733)
04-16 11:24:58.027: E/AndroidRuntime(30759): at android.os.Handler.dispatchMessage(Handler.java:95)
04-16 11:24:58.027: E/AndroidRuntime(30759): at android.os.Looper.loop(Looper.java:136)
04-16 11:24:58.027: E/AndroidRuntime(30759): at android.app.ActivityThread.main(ActivityThread.java:5088)
04-16 11:24:58.027: E/AndroidRuntime(30759): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 11:24:58.027: E/AndroidRuntime(30759): at java.lang.reflect.Method.invoke(Method.java:515)
04-16 11:24:58.027: E/AndroidRuntime(30759): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
04-16 11:24:58.027: E/AndroidRuntime(30759): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-16 11:24:58.027: E/AndroidRuntime(30759): at dalvik.system.NativeStart.main(Native Method)
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.bluetooth"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Main_Activity"
android:label="RC-Remote" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
在您的logcat中:com.test.bluetooth/com.test.bluetooth.Main_Activity.
它应该是com.test.bluetooth.Main_Activity
重命名活动名称,如下所示:
<activity
android:name=".Main_Activity"
...
</activity>
答案 1 :(得分:0)
Intent options1 = new Intent(this, SettingsFragment.class);
startActivity(options1);
在这里,您尝试使用Activity
http://developer.android.com/reference/android/preference/PreferenceFragment.html
Fragment