1

我试图Class在膨胀这个时从 a 中提取值,Dialog Fragment但是当它有值时它返回 null Class。这对我来说很奇怪,也许我错过了一些东西。这是代码。

对话片段

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();
    }
}

日志猫

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)

这是第 41 行的代码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>
4

1 回答 1

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);
于 2013-06-18T23:40:49.517 回答