我有一个启动对话框的片段。在对话框中有一个文本视图和一个按钮。当您按下对话框内的按钮时,它应从文本字段中获取值并将其发送到异步任务。
当我尝试检索文本字段的值时,我的应用程序强行关闭:
public class ListPage extends Fragment {
String beerID = "";
String userID = "";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//set layout here
View v = inflater.inflate(R.layout.frag_listpage, container, false);
setHasOptionsMenu(true);
getActivity().setTitle("Your Lists");
//get user information
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
String userName = prefs.getString("userName", null);
userID = prefs.getString("userID", null);
beerID = prefs.getString("beerID", null);
//todo: get lists and add checkboxes with onclick
Button bt2 = (Button)v.findViewById(R.id.buttonAddList);
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// Create custom dialog object
final Dialog dialog = new Dialog(getActivity());
// Include dialog.xml file
dialog.setContentView(R.layout.listdialog);
// Set dialog title
dialog.setTitle("Create a List");
// set values for custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.textDialog);
dialog.show();
Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
// if decline button is clicked, close the custom dialog
declineButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//todo: run async task to save text field value as a list
//get value from text field
EditText mEdit = (EditText)v.findViewById(R.id.listName);
String lName = mEdit.getText().toString();
try {
lName = URLEncoder.encode(lName, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//run async task with beerID, userID and lName
//log in url
String url = "myURL;
Log.d("registerURL", url);
//async task for getting json
new AddList(getActivity()).execute(url);
// Close dialog
dialog.dismiss();
}
});
}
});
// Inflate the layout for this fragment
return v;
}
错误是:
03-26 16:06:19.340 7659-7659/com.beerportfolio.beerportfoliopro E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.beerportfolio.beerportfoliopro.ListPage$1$1.onClick(ListPage.java:87)
at android.view.View.performClick(View.java:4280)
at android.view.View$PerformClick.run(View.java:17984)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:5789)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
at dalvik.system.NativeStart.main(Native Method)
第87行是:
String lName = mEdit.getText().toString();
答案 0 :(得分:1)
您以错误的方式找到了EditText
。取代
EditText mEdit = (EditText)v.findViewById(R.id.listName);
与
EditText mEdit = (EditText)dialog.findViewById(R.id.listName);
v
是点击的按钮,因此无法找到内部的EditText
。