我试图在Class
充气时从Dialog Fragment
中提取值,但在Class
有值时返回null。这对我来说很奇怪,也许我错过了什么。这是代码。
对话片段
public class Dialog_Fragment extends DialogFragment {
TextView store, phone, address, city, zip, state;
String mStore, mPhone, mAddress, mCity, mZip, mState;
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder storeD = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
if (BasicInfo_Fragment.store_model != null) {
mStore = BasicInfo_Fragment.store_model.getStoreNum();
mPhone = BasicInfo_Fragment.store_model.getPhoneNum();
mAddress = BasicInfo_Fragment.store_model.getAddress();
mCity = BasicInfo_Fragment.store_model.getCity();
mZip = BasicInfo_Fragment.store_model.getZip();
mState = BasicInfo_Fragment.store_model.getState();
store = (TextView) getActivity().findViewById(R.id.store_d);
address = (TextView) getActivity().findViewById(R.id.address_d);
city = (TextView) getActivity().findViewById(R.id.city_d);
zip = (TextView) getActivity().findViewById(R.id.zip_d);
state = (TextView) getActivity().findViewById(R.id.state_d);
Log.v("RESPONSE", BasicInfo_Fragment.store_model.getStoreNum());
store.setText("Store #: " + mStore);
phone.setText("Phone #: " + mPhone);
address.setText("Address: " + mAddress);
city.setText("City: " + mCity);
zip.setText("Zip: " + mZip);
state.setText("State: " + mState);
}
storeD.setView(inflater.inflate(R.layout.fragment_dialog, null))
.setMessage(R.string.store_question)
.setPositiveButton(R.string.yes,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.replace(R.id.content_fragment_container,
new Welcome_Fragment());
ft.commit();
}
})
.setNegativeButton(R.string.no,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.replace(R.id.content_fragment_container,
new BasicInfo_Fragment());
ft.commit();
}
});
// Create the Dialog object and return it
return storeD.create();
}
}
logcat的
06-18 19:20:57.740: E/json data(25865): json result {"store_info":[{"store_num":"00607","address":"790 W BROADWAY RD","city":"TEMPE","state":"AZ","zip":"85282"}],"success":1}
06-18 19:20:57.740: V/RESPONSE(25865): Success!
06-18 19:20:57.760: V/RESPONSE(25865): 00607
06-18 19:20:57.760: D/AndroidRuntime(25865): Shutting down VM
06-18 19:20:57.760: W/dalvikvm(25865): threadid=1: thread exiting with uncaught exception (group=0x41093930)
06-18 19:20:57.770: E/AndroidRuntime(25865): FATAL EXCEPTION: main
06-18 19:20:57.770: E/AndroidRuntime(25865): java.lang.NullPointerException
06-18 19:20:57.770: E/AndroidRuntime(25865): at com.fa.Dialog_Fragment.onCreateDialog(Dialog_Fragment.java:41)
06-18 19:20:57.770: E/AndroidRuntime(25865): at android.app.DialogFragment.getLayoutInflater(DialogFragment.java:398)
06-18 19:20:57.770: E/AndroidRuntime(25865): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
06-18 19:20:57.770: E/AndroidRuntime(25865): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
06-18 19:20:57.770: E/AndroidRuntime(25865): at android.app.BackStackRecord.run(BackStackRecord.java:682)
06-18 19:20:57.770: E/AndroidRuntime(25865): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
06-18 19:20:57.770: E/AndroidRuntime(25865): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
06-18 19:20:57.770: E/AndroidRuntime(25865): at android.os.Handler.handleCallback(Handler.java:725)
06-18 19:20:57.770: E/AndroidRuntime(25865): at android.os.Handler.dispatchMessage(Handler.java:92)
06-18 19:20:57.770: E/AndroidRuntime(25865): at android.os.Looper.loop(Looper.java:137)
06-18 19:20:57.770: E/AndroidRuntime(25865): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-18 19:20:57.770: E/AndroidRuntime(25865): at java.lang.reflect.Method.invokeNative(Native Method)
06-18 19:20:57.770: E/AndroidRuntime(25865): at java.lang.reflect.Method.invoke(Method.java:511)
06-18 19:20:57.770: E/AndroidRuntime(25865): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-18 19:20:57.770: E/AndroidRuntime(25865): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-18 19:20:57.770: E/AndroidRuntime(25865): at dalvik.system.NativeStart.main(Native Method)
这是Dialog_Fragment.java
store.setText("Store #: " + mStore);
您从我的Log.v
消息中可以看到的不是null
。它返回“00607”,正确的值。为什么我的应用程序崩溃?
以下是此对话框的XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="@+id/store_d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/address_d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/city_d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/state_d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/zip_d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
答案 0 :(得分:2)
在不知道getActivity()
实际返回的内容的情况下,我只能假设getActivity()
视图不包含R.id.store_d field
。如果,或许你的意思是从R.layout.fragment_dialog
视图中获取该值,那么您需要在此之前移动该视图的膨胀,并使用该视图的参考来查找其他视图。
View v = inflater.inflate(R.layout.fragment_dialog, null);
store = (TextView) v.findViewById(R.id.store_d);
...
storeD.setView(v);