我有FragmentActivity
使用SherlockActionBar
和支持库v4。
主布局中有ListFragment
。点击列表项会打开一个新的ListFragment
,可以部分滑入视图。如果您长按某些列表项,则会显示一个上下文菜单,其中包含两个选项。第一个选项会显示dialog
EditText
框,Button
'在尝试打开时会崩溃。
如果我删除EditText
行和Button
行(除非您使用findViewById
声明这些行),系统会弹出一个空的dialog
。但由于某种原因,它不喜欢对话框的内容(按钮/框)。任何人都可以看看LogCat,看看为什么? null
行是setText
行。如果我将其评论出来,则按钮的onClickListner
会变为null
。
以下是该代码和logcat的代码。
public boolean onContextItemSelected(android.view.MenuItem item) {
if (item.getGroupId() == 2) {
switch (item.getItemId()) {
case 1:
case 1:
if (Rateit.isUserLoggedIn == true) {
final Dialog dialog = new Dialog(getActivity());
dialog.setTitle("Edit Review");
dialog.show();
final EditText etEdit = (EditText) dialog
.findViewById(R.id.etEditReview);
etEdit.setText(reviewWords);
Button bInsert = (Button) dialog.findViewById(R.id.bInsert);
bInsert.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
clickedReview = etEdit.getText().toString();
if (clickedReview.equals("")) {
Toast.makeText(getActivity(),
"Please add something first.",
Toast.LENGTH_SHORT).show();
} else {
newReview = etEdit.getText().toString();
new EditCommentTask().execute();
InputMethodManager imm = (InputMethodManager) getActivity()
.getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(
etEdit.getWindowToken(), 0);
dialog.dismiss();
}
}
});
}
return true;
logcat的:
03-06 10:29:08.527: E/AndroidRuntime(10438): FATAL EXCEPTION: main
03-06 10:29:08.527: E/AndroidRuntime(10438): java.lang.NullPointerException
03-06 10:29:08.527: E/AndroidRuntime(10438): at com.---.---.RateReviewFragment.onContextItemSelected(RateReviewFragment.java:901)
03-06 10:29:08.527: E/AndroidRuntime(10438): at android.support.v4.app.FragmentManagerImpl.dispatchContextItemSelected(FragmentManager.java:1933)
03-06 10:29:08.527: E/AndroidRuntime(10438): at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:360)
03-06 10:29:08.527: E/AndroidRuntime(10438): at com.actionbarsherlock.app.SherlockFragmentActivity.onMenuItemSelected(SherlockFragmentActivity.java:211)
03-06 10:29:08.527: E/AndroidRuntime(10438): at com.android.internal.policy.impl.PhoneWindow$DialogMenuCallback.onMenuItemSelected(PhoneWindow.java:3633)
03-06 10:29:08.527: E/AndroidRuntime(10438): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
03-06 10:29:08.527: E/AndroidRuntime(10438): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
03-06 10:29:08.527: E/AndroidRuntime(10438): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
03-06 10:29:08.527: E/AndroidRuntime(10438): at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:167)
03-06 10:29:08.527: E/AndroidRuntime(10438): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:941)
03-06 10:29:08.527: E/AndroidRuntime(10438): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
03-06 10:29:08.527: E/AndroidRuntime(10438): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
03-06 10:29:08.527: E/AndroidRuntime(10438): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
03-06 10:29:08.527: E/AndroidRuntime(10438): at android.widget.AbsListView$1.run(AbsListView.java:3423)
03-06 10:29:08.527: E/AndroidRuntime(10438): at android.os.Handler.handleCallback(Handler.java:725)
03-06 10:29:08.527: E/AndroidRuntime(10438): at android.os.Handler.dispatchMessage(Handler.java:92)
03-06 10:29:08.527: E/AndroidRuntime(10438): at android.os.Looper.loop(Looper.java:137)
03-06 10:29:08.527: E/AndroidRuntime(10438): at android.app.ActivityThread.main(ActivityThread.java:5226)
03-06 10:29:08.527: E/AndroidRuntime(10438): at java.lang.reflect.Method.invokeNative(Native Method)
03-06 10:29:08.527: E/AndroidRuntime(10438): at java.lang.reflect.Method.invoke(Method.java:511)
03-06 10:29:08.527: E/AndroidRuntime(10438): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
03-06 10:29:08.527: E/AndroidRuntime(10438): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
03-06 10:29:08.527: E/AndroidRuntime(10438): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
看起来好像你没有夸大对话框的XML, 我想你有一个用于该对话框的XML,所以你应该这样做:
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.instructions, (ViewGroup) activity.findViewById(R.id.layout_root));
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
Button bInsert = (Button) layout.findViewById(R.id.bInsert);
alertDialog = builder.create();
alertDialog.show();
你要把
放在哪里 mContext=this;
onCreate()中的(如果你在你的活动中)
答案 1 :(得分:1)
查看您的代码,似乎您可能忘记为新创建的Dialog
(使用setContentView(the_layout_file)
)设置布局(包含您的小部件)。如果您不这样做,那么在对话框中搜索并尝试使用这些小部件将抛出NullPointerException
。