空指针在对话片段 [英] Null Pointer in Dialog Fragment

查看:140
本文介绍了空指针在对话片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从充气这个时候对话片段拉值,但它是当返回null 的值。这是很奇怪的我,也许我失去了一些东西。这里是code。

对话片段

 公共类Dialog_Fragment扩展DialogFragment {    TextView的专卖店,电话,地址,城市,邮编,状态;
    串mStore,mPhone,mAddress,mCity,mZip,mState;    公共对话框onCreateDialog(捆绑savedInstanceState){        AlertDialog.Builder存储=新AlertDialog.Builder(getActivity());        LayoutInflater吹气= getActivity()getLayoutInflater()。        如果(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();            店内=(TextView中)getActivity()findViewById(R.id.store_d)。
            地址=(TextView中)getActivity()findViewById(R.id.address_d)。
            城市=(TextView中)getActivity()findViewById(R.id.city_d)。
            邮编=(TextView中)getActivity()findViewById(R.id.zip_d)。
            状态=(TextView中)getActivity()findViewById(R.id.state_d)。            Log.v(反应,BasicInfo_Fragment.store_model.getStoreNum());
            store.setText(商店#:+ mStore);
            phone.setText(电话号码+ mPhone);
            address.setText(地址+ mAddress);
            city​​.setText(城市:+ mCity);
            zip.setText(邮编+ mZip);
            state.setText(状态:+ mState);        }
        storeD.setView(inflater.inflate(R.layout.fragment_dialog,NULL))
                .setMessage(R.string.store_question)
                .setPositiveButton(R.string.yes,
                        新DialogInterface.OnClickListener(){
                            公共无效的onClick(DialogInterface对话,诠释的id){
                                FragmentManager FM = getFragmentManager();
                                FragmentTransaction英尺= fm.beginTransaction();
                                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                                ft.replace(R.id.content_fragment_container,
                                        新Welcome_Fragment());
                                ft.commit();
                            }
                        })                .setNegativeButton(R.string.no,
                        新DialogInterface.OnClickListener(){
                            公共无效的onClick(DialogInterface对话,诠释的id){
                                FragmentManager FM = getFragmentManager();
                                FragmentTransaction英尺= fm.beginTransaction();
                                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                                ft.replace(R.id.content_fragment_container,
                                        新BasicInfo_Fragment());
                                ft.commit();
                            }
                        });        //创建Dialog对象,并将其返回
        返回storeD.create();
    }
}

LogCat中

  06-18 19:20:57.740:E / JSON数据(25865):JSON结果{store_info:[{store_num:00607,地址: 790W¯¯BROADWAY RD​​,城市:坦佩,状态:AZ,拉链:85282}],成功:1}
06-18 19:20:57.740:V / RESPONSE(25865):成功!
06-18 19:20:57.760:V / RESPONSE(25865):00607
06-18 19:20:57.760:D / AndroidRuntime(25865):关闭VM
06-18 19:20:57.760:W / dalvikvm(25865):主题ID = 1:螺纹未捕获的异常(组= 0x41093930)退出
06-18 19:20:57.770:E / AndroidRuntime(25865):致命异常:主要
06-18 19:20:57.770:E / AndroidRuntime(25865):显示java.lang.NullPointerException
06-18 19:20:57.770:E / AndroidRuntime(25865):在com.fa.Dialog_Fragment.onCreateDialog(Dialog_Fragment.java:41)
06-18 19:20:57.770:E / AndroidRuntime(25865):在android.app.DialogFragment.getLayoutInflater(DialogFragment.java:398)
06-18 19:20:57.770:E / AndroidRuntime(25865):在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
06-18 19:20:57.770:E / AndroidRuntime(25865):在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
06-18 19:20:57.770:E / AndroidRuntime(25865):在android.app.BackStackRecord.run(BackStackRecord.java:682)
06-18 19:20:57.770:E / AndroidRuntime(25865):在android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
06-18 19:20:57.770:E / AndroidRuntime(25865):在android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:441)
06-18 19:20:57.770:E / AndroidRuntime(25865):在android.os.Handler.handleCallback(Handler.java:725)
06-18 19:20:57.770:E / AndroidRuntime(25865):在android.os.Handler.dispatchMessage(Handler.java:92)
06-18 19:20:57.770:E / AndroidRuntime(25865):在android.os.Looper.loop(Looper.java:137)
06-18 19:20:57.770:E / AndroidRuntime(25865):在android.app.ActivityThread.main(ActivityThread.java:5041)
06-18 19:20:57.770:E / AndroidRuntime(25865):在java.lang.reflect.Method.invokeNative(本机方法)
06-18 19:20:57.770:E / AndroidRuntime(25865):在java.lang.reflect.Method.invoke(Method.java:511)
06-18 19:20:57.770:E / AndroidRuntime(25865):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
06-18 19:20:57.770:E / AndroidRuntime(25865):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-18 19:20:57.770:E / AndroidRuntime(25865):在dalvik.system.NativeStart.main(本机方法)

这是 Dialog_Fragment.java

在code。在第41行

  store.setText(存储#:+ mStore);

正如你可以从我的 Log.v 消息,看看是不是。它返回00607,正确的值。为什么我的应用程序崩溃?

下面是 XML 此对话框

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:layout_marginTop =10dp
    机器人:重力=CENTER_HORIZONTAL
    机器人:方向=垂直>    <的TextView
        机器人:ID =@ + ID / store_d
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>    <的TextView
        机器人:ID =@ + ID / address_d
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>    <的TextView
        机器人:ID =@ + ID / city_d
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>    <的TextView
        机器人:ID =@ + ID / state_d
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>    <的TextView
        机器人:ID =@ + ID / zip_d
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>< / LinearLayout中>


解决方案

不知道什么 getActivity()真的返回,我只能假设 getActivity()视图不包含 R.id.store_d领域。如果,也许你的意思是您可以通过 R.layout.fragment_dialog 认为值,那么你需要在早些时候的移动视图的通货膨胀,以及使用该视图寻找其他意见参考。

 视图V = inflater.inflate(R.layout.fragment_dialog,NULL);
店内=(TextView中)v.findViewById(R.id.store_d);
...
storeD.setView(五);

I am trying to pull values from a Class when inflating this Dialog Fragment but it is returning null when the Class has values. This is very strange to me, maybe I'm missing something. Here is the code.

Dialog Fragment

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)

this is the code at line 41 in Dialog_Fragment.java

store.setText("Store #: " + mStore);

Which as you can see from my Log.v message is not null. It's returning "00607", the correct value. Why is my application crashing?

Here is the XML for this dialog

<?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>

解决方案

Without knowing what getActivity() is actually returning, I can only assume that the getActivity() view doesn't contain the R.id.store_d field. If, perhaps you mean to get that value from the R.layout.fragment_dialog view, then you'll need to move the inflation of that view earlier on, and the use that view's reference for finding the other views.

View v = inflater.inflate(R.layout.fragment_dialog, null);
store = (TextView) v.findViewById(R.id.store_d);
...
storeD.setView(v);

这篇关于空指针在对话片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆