findViewById在DialogFragment

findViewById在DialogFragment

本文介绍了findViewById在DialogFragment - NullPointerException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我显示对话框有两个纱厂,并为其中的一个,我想设置所选项目(项目选择从主要活动通过与setArguments()/ getArguments())

问题是我不能得到的微调,而不是我在

得到一个NullPointerException异常

微调微调=(微调)view.findViewById(R.id.ssd_weeksSelectSpinner);

该DialogFragment code:

 进口android.app.Activity;
进口android.app.AlertDialog;
进口android.app.Dialog;
进口android.content.DialogInterface;
进口android.os.Bundle;
进口android.support.v4.app.DialogFragment;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.widget.Spinner;公共类属性同好会扩展DialogFragment
{
    公共对话框onCreateDialog(捆绑savedInstanceState)
    {
    AlertDialog.Builder建设者=新AlertDialog.Builder(getActivity());
    LayoutInflater吹气= getActivity()getLayoutInflater()。    builder.setView(inflater.inflate(R.layout.activity_schedule_select_dialog,NULL))
        .setPositiveButton(R.string.ssd_select_positive,新DialogInterface.OnClickListener()
        {
            公共无效的onClick(DialogInterface对话,诠释其)
            {
                mListener.onDialogPositiveClick(DFrag.this);
            }
        })
        .setNegativeButton(R.string.select_negative,新DialogInterface.OnClickListener()
        {
            @覆盖
            公共无效的onClick(DialogInterface对话,诠释它){
                mListener.onDialogNegativeClick(DFrag.this);
                DFrag.this.getDialog()取消()。
            }
        });        builder.setInverseBackgroundForced(真);        返回builder.create();
    }
    公共无效onActivityCreated(捆绑savedInstanceState)
    {
        查看查看= getView();        微调微调=(微调)view.findViewById(R.id.ssd_weeksSelectSpinner);
        spinner.setSelection(getArguments()调用getInt(SelectWeek)。);        super.onActivityCreated(savedInstanceState);
    }    公共接口DFragListener
    {
        公共无效onDialogPositiveClick(DialogFragment对话);
        公共无效onDialogNegativeClick(DialogFragment对话);
    }    DFragListener mListener;
    @覆盖
    公共无效onAttach(活动活动)
    {
        super.onAttach(活动);
        尝试
        {
            mListener =(DFragListener)活性;
        }
        赶上(抛出ClassCastException E)
        {
            抛出新ClassCastException异常(activity.toString()+监听器没有实现);
        }
    }
}

根据要求,布局的对话框(activity_schedule_select_dialog.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
    机器人:方向=垂直>    <的TextView
        机器人:ID =@ + ID / ssd_classLabel
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=@字符串/ select_class_label
        机器人:layout_marginRight =4dip
        机器人:layout_marginLeft =4dip/>
    <微调
        机器人:ID =@ + ID / ssd_classSelectSpinner
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginBottom =4dip
        机器人:项=@阵列/ urnikClasses/>    <的TextView
        机器人:ID =@ + ID / ssd_weekLabel
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=@字符串/ select_week_label
        机器人:layout_marginRight =4dip
        机器人:layout_marginLeft =4dip/>
    <微调
        机器人:ID =@ + ID / ssd_weeksSelectSpinner
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:项=@阵列/ weeksArray/>    <复选框
        机器人:ID =@ + ID / ssd_DefaultChkbox
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=@字符串/ ssd_DefaultChkBoxTxt/>
< / LinearLayout中>

logcat的:

  05-12 13:16:16.288:E / AndroidRuntime(4310):致命异常:主要
05-12 13:16:16.288:E / AndroidRuntime(4310):显示java.lang.NullPointerException
05-12 13:16:16.288:E / AndroidRuntime(4310):在com.rogy.scks.urnik.DFrag.onActivityCreated(DFrag.java:91)
05-12 13:16:16.288:E / AndroidRuntime(4310):在android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1468)
05-12 13:16:16.288:E / AndroidRuntime(4310):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
05-12 13:16:16.288:E / AndroidRuntime(4310):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
05-12 13:16:16.288:E / AndroidRuntime(4310):在android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-12 13:16:16.288:E / AndroidRuntime(4310):在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
05-12 13:16:16.288:E / AndroidRuntime(4310):在android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:429)
05-12 13:16:16.288:E / AndroidRuntime(4310):在android.os.Handler.handleCallback(Handler.java:725)
05-12 13:16:16.288:E / AndroidRuntime(4310):在android.os.Handler.dispatchMessage(Handler.java:92)
05-12 13:16:16.288:E / AndroidRuntime(4310):在android.os.Looper.loop(Looper.java:137)
05-12 13:16:16.288:E / AndroidRuntime(4310):在android.app.ActivityThread.main(ActivityThread.java:5195)
05-12 13:16:16.288:E / AndroidRuntime(4310):在java.lang.reflect.Method.invokeNative(本机方法)
05-12 13:16:16.288:E / AndroidRuntime(4310):在java.lang.reflect.Method.invoke(Method.java:511)
05-12 13:16:16.288:E / AndroidRuntime(4310):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:795)
05-12 13:16:16.288:E / AndroidRuntime(4310):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
05-12 13:16:16.288:E / AndroidRuntime(4310):在dalvik.system.NativeStart.main(本机方法)


解决方案

找到了解决办法,而不是试图找到onActivityCreated(捆绑savedInstanceState)公共无效

公共对话框onCreateDialog(捆绑savedInstanceState)

我从

改变

  AlertDialog.Builder建设者=新AlertDialog.Builder(getActivity());
    LayoutInflater吹气= getActivity()getLayoutInflater()。    builder.setView(inflater.inflate(R.layout.activity_schedule_select_dialog,NULL))
        .setPositiveButton(R.string.ssd_select_positive,新DialogInterface.OnClickListener()
        {
            公共无效的onClick(DialogInterface对话,诠释其)
            {
                mListener.onDialogPositiveClick(DFrag.this);
            }
        })
        .setNegativeButton(R.string.select_negative,新DialogInterface.OnClickListener()
        {
            @覆盖
            公共无效的onClick(DialogInterface对话,诠释它){
                mListener.onDialogNegativeClick(DFrag.this);
                DFrag.this.getDialog()取消()。
            }
        });

  AlertDialog.Builder建设者=新AlertDialog.Builder(getActivity());
    LayoutInflater吹气= getActivity()getLayoutInflater()。
    查看查看= inflater.inflate(R.layout.activity_schedule_select_dialog,NULL);    builder.setView(视图)
        .setPositiveButton(R.string.ssd_select_positive,新DialogInterface.OnClickListener()
        {
            公共无效的onClick(DialogInterface对话,诠释其)
            {
                mListener.onDialogPositiveClick(DFrag.this);
            }
        })
        .setNegativeButton(R.string.select_negative,新DialogInterface.OnClickListener()
        {
            @覆盖
            公共无效的onClick(DialogInterface对话,诠释它){
                mListener.onDialogNegativeClick(DFrag.this);
                DFrag.this.getDialog()取消()。
            }
        });

和在末尾添加

 微调微调=(微调)view.findViewById(R.id.ssd_weeksSelectSpinner);
spinner.setSelection(getArguments()调用getInt(SelectWeek)。);

The dialog I show has two Spinners, and for one of those I'd like to set the selected item (item to select is passed from the main activity with setArguments()/getArguments())

The problem is I can't get the spinner, instead I get a NullPointerException at

Spinner spinner = (Spinner) view.findViewById(R.id.ssd_weeksSelectSpinner);

The DialogFragment code:

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Spinner;

public class DFrag extends DialogFragment
{
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();

    builder.setView(inflater.inflate(R.layout.activity_schedule_select_dialog, null))
        .setPositiveButton(R.string.ssd_select_positive, new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int which)
            {
                mListener.onDialogPositiveClick(DFrag.this);
            }
        })
        .setNegativeButton(R.string.select_negative, new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mListener.onDialogNegativeClick(DFrag.this);
                DFrag.this.getDialog().cancel();
            }
        });

        builder.setInverseBackgroundForced(true);

        return builder.create();
    }


    public void onActivityCreated(Bundle savedInstanceState)
    {
        View view = getView();

        Spinner spinner = (Spinner) view.findViewById(R.id.ssd_weeksSelectSpinner);
        spinner.setSelection(getArguments().getInt("SelectWeek"));

        super.onActivityCreated(savedInstanceState);
    }

    public interface DFragListener
    {
        public void onDialogPositiveClick(DialogFragment dialog);
        public void onDialogNegativeClick(DialogFragment dialog);
    }

    DFragListener mListener;


    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
        try
        {
            mListener = (DFragListener) activity;
        }
        catch (ClassCastException e)
        {
            throw new ClassCastException(activity.toString() + " Listener not implemented");
        }
    }
}

As requested, layout for the dialog (activity_schedule_select_dialog.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:orientation="vertical" >

    <TextView
        android:id="@+id/ssd_classLabel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/select_class_label"
        android:layout_marginRight="4dip"
        android:layout_marginLeft="4dip"/>
    <Spinner
        android:id="@+id/ssd_classSelectSpinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dip"
        android:entries="@array/urnikClasses"/>

    <TextView
        android:id="@+id/ssd_weekLabel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/select_week_label"
        android:layout_marginRight="4dip"
        android:layout_marginLeft="4dip"/>
    <Spinner
        android:id="@+id/ssd_weeksSelectSpinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:entries="@array/weeksArray"/>

    <CheckBox
        android:id="@+id/ssd_DefaultChkbox"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/ssd_DefaultChkBoxTxt"/>


</LinearLayout>

Logcat:

05-12 13:16:16.288: E/AndroidRuntime(4310): FATAL EXCEPTION: main
05-12 13:16:16.288: E/AndroidRuntime(4310): java.lang.NullPointerException
05-12 13:16:16.288: E/AndroidRuntime(4310):     at com.rogy.scks.urnik.DFrag.onActivityCreated(DFrag.java:91)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1468)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at android.os.Handler.handleCallback(Handler.java:725)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at android.os.Looper.loop(Looper.java:137)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at android.app.ActivityThread.main(ActivityThread.java:5195)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at java.lang.reflect.Method.invokeNative(Native Method)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at java.lang.reflect.Method.invoke(Method.java:511)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at dalvik.system.NativeStart.main(Native Method)
解决方案

Found the solution, instead of trying to find the view in public void onActivityCreated(Bundle savedInstanceState)

In public Dialog onCreateDialog(Bundle savedInstanceState)

I changed from

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();

    builder.setView(inflater.inflate(R.layout.activity_schedule_select_dialog, null))
        .setPositiveButton(R.string.ssd_select_positive, new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int which)
            {
                mListener.onDialogPositiveClick(DFrag.this);
            }
        })
        .setNegativeButton(R.string.select_negative, new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mListener.onDialogNegativeClick(DFrag.this);
                DFrag.this.getDialog().cancel();
            }
        });

To

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.activity_schedule_select_dialog, null);

    builder.setView(view)
        .setPositiveButton(R.string.ssd_select_positive, new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int which)
            {
                mListener.onDialogPositiveClick(DFrag.this);
            }
        })
        .setNegativeButton(R.string.select_negative, new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mListener.onDialogNegativeClick(DFrag.this);
                DFrag.this.getDialog().cancel();
            }
        });

And at the end added

Spinner spinner = (Spinner) view.findViewById(R.id.ssd_weeksSelectSpinner);
spinner.setSelection(getArguments().getInt("SelectWeek"));

这篇关于findViewById在DialogFragment - NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 01:22