我尝试使用选项菜单打开首选项屏幕。选项菜单的drawable在右角可见,但是当我点击它时,应用程序崩溃了。
有人可以告诉我为什么吗?
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.item1) {
Intent i = new Intent(this, SettingsActivity.class);
startActivity(i);
Log.d("onOptionsItemSelected","yes");
return true;
}
Log.d("onOptionsItemSelected","no");
return super.onOptionsItemSelected(item);
}
public class SettingsActivity extends AppCompatActivity {
@SuppressLint("ResourceType")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.xml.preferences);
}
public void showDatePicker(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "date picker");
}
}
public class DatePickerFragment extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) {
setHasOptionsMenu(true);
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), dateSetListener, year, month, day);
}
private DatePickerDialog.OnDateSetListener dateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int month, int day) {
Toast.makeText(getActivity(), "selected date is " + view.getYear() +
" / " + (view.getMonth() + 1) +
" / " + view.getDayOfMonth(), Toast.LENGTH_SHORT).show();
}
};
}
```
E / AndroidRuntime:致命异常:主要 过程:com.example.m.myfirstapp,PID:4649 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.myfirstapp / com.example.m.myfirstapp.SettingsActivity}:android.view.InflateException:二进制XML文件行#0:二进制XML文件行#0:错误膨胀类PreferenceScreen 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 在android.app.ActivityThread.-wrap12(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 引起:android.view.InflateException:二进制XML文件行#0:二进制XML文件行#0:错误膨胀类PreferenceScreen 引起:android.view.InflateException:二进制XML文件行#0:错误导致类PreferenceScreen 引起:java.lang.ClassNotFoundException:路径上没有找到类“android.view.PreferenceScreen”:DexPathList [[zip file“/data/app/com.example.myfirstapp-1/base.apk”,zip文件“/data/app/com.example.m.myfirstapp-1/split_lib_dependencies_apk.apk”,zip文件“/data/app/com.example.m.myfirstapp-1/split_lib_slice_0_apk.apk”,zip文件“/ data / app / com.example.m.myfirstapp-1 / split_lib_slice_1_apk.apk“,zip文件”/data/app/com.example.m.myfirstapp-1/split_lib_slice_2_apk.apk“,zip文件”/ data / app / com。 example.m.myfirstapp-1 / split_lib_slice_3_apk.apk“,zip文件”/data/app/com.example.m.myfirstapp-1/split_lib_slice_4_apk.apk“,zip文件”/data/app/com.example.m。 myfirstapp-1 / split_lib_slice_5_apk.apk“,zip文件”/data/app/com.example.m.myfirstapp-1/split_lib_slice_6_apk.apk“,zip文件”/data/app/com.example.m.myfirstapp-1/ split_lib_slice_7_apk.apk“,zip文件”/data/app/com.example.m.myfirstapp-1/split_lib_slice_8_apk.apk“,zip文件”/data/app/com.example.m.myfirstapp-1/split_lib_slice_9_apk.apk “],nativeLibraryDirectories = [/ data / app / com.example.m.myfirstapp-1 / lib / x86,/ system / lib,/ vendor / lib]]