我想让图标可点击,但应用程序总是崩溃。我关注here但没有成功。迈出第一步是必须的吗?点击图标后,它将转到AlertRadioDialog
功能,这意味着应弹出dialog radio
。怎么做到这一点?
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = this.getArguments();
fk = bundle.getLong("ab");
setHasOptionsMenu(true);
getActivity().getActionBar().setHomeButtonEnabled(true);
View claims = inflater.inflate(R.layout.claims, container, false);
android.support.v7.app.ActionBar myActionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
ImageView imageView = new ImageView(myActionBar.getThemedContext());
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(R.mipmap.create);
android.support.v7.app.ActionBar.LayoutParams layoutParams = new android.support.v7.app.ActionBar.LayoutParams(
android.support.v7.app.ActionBar.LayoutParams.WRAP_CONTENT,
android.support.v7.app.ActionBar.LayoutParams.WRAP_CONTENT, Gravity.RIGHT
| Gravity.CENTER_VERTICAL);
layoutParams.rightMargin = 40;
imageView.setLayoutParams(layoutParams);
myActionBar.setCustomView(imageView);
return claims;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.my_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
//Do what you want with menu - listeners etc.
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
AlertDialogRadio();
return true;
}
return (super.onOptionsItemSelected(item));
}
public void AlertDialogRadio() {
final CharSequence[] ClaimsModel = {"A", "B"};
AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity());
alt_bld.setTitle("Select a Class");
alt_bld.setSingleChoiceItems(ClaimsModel, -1, new DialogInterface
.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0) {
Intent intent = new Intent(getActivity().getApplicationContext(), Project1.class);
startActivityForResult(intent, 0);
} else if (item == 1) {
Intent intent = new Intent(getActivity().getApplicationContext(), Petrol.class);
}
dialog.dismiss();
}
});
AlertDialog alert = alt_bld.create();
alert.show();
}
}
错误
11-15 16:42:25.459 20826-20826/com.example.project.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.project.project, PID: 20826
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setHomeButtonEnabled(boolean)' on a null object reference
at com.example.project.project.Claims1.onCreateView(Claims1.java:34)
at android.app.Fragment.performCreateView(Fragment.java:2220)
答案 0 :(得分:0)
确保您在支持操作栏的清单中有该活动的主题。如果您使用AppCompatActivity,请尝试使用getSupportActionBar()