2013-05-12 81 views
8

我顯示的對話框有兩個Spinners,對於其中一個我想要設置選定項目(要選擇的項目是使用setArguments()/ getArguments()從主活動傳遞的)DialogFragment中的findViewById --NullPointerException

的問題是我不能得到微調,而不是我在

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

的DialogFragment代碼得到一個NullPointerException:

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

按照要求,佈局的對話框(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) 
+0

發佈您定義的'ssd_weeksSelectSpinner'的XML – m0skit0 2013-05-12 11:51:39

+0

編輯原始帖子以包含所需的代碼。 – Rogy 2013-05-12 11:55:18

+0

我假設這是'activity_schedule_select_dialog.xml'文件,我說得對嗎?並且請張貼整個堆棧跟蹤。 – m0skit0 2013-05-12 11:56:49

回答

7

找到了解決辦法,而不是試圖發現在public Dialog onCreateDialog(Bundle savedInstanceState)

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

改變要

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

,並在末尾添加

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

節省我的時間,謝謝! – li2 2015-10-30 09:09:39

1

它是由這個原因引起:

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

它給你一個NullPointerException,因爲微調,它沒有被選中,它是空的,當acti vity開始......你需要把這個放在onItemSelected的監聽器上。 http://developer.android.com/guide/topics/ui/controls/spinner.html#SelectListener

編輯:

public class SpinnerActivity extends Activity implements OnItemSelectedListener { 
    ... 

    public void onItemSelected(AdapterView<?> parent, View view, 
      int pos, long id) { 
     // An item was selected. You can retrieve the selected item using 
     // parent.getItemAtPosition(pos) 
    } 

    public void onNothingSelected(AdapterView<?> parent) { 
     // Another interface callback 
    } 
} 

有關更多信息,請參見本還需要導入「R」的java文件。我沒有看到它在進口清單:

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; 

編輯2:要添加一個偵聽微調做到這一點:

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

spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 
     public void onItemSelected(AdapterView<?> arg0, View v, int position, long id) 
     { 
      // int position is the element you pressed 
     } 
    }); 
+0

是的,在我的第一篇文章中,我寫了''Spinner spinner =(Spinner)view.findViewById(R.id.ssd_weeksSelectSpinner);''得到了NullPointerException。我沒有看到我將如何實現監聽器,因爲我仍然需要使用findViewById()來獲取視圖,並且當我嘗試使用所述方法獲取視圖時,我得到NullPointerException。我試圖爲微調器設置選定的項目,以便用戶不會看到微調器中的第一項(以及微調器使用的數組),用戶會看到(例如)微調器中的第4項。我導入了R java文件,仍然是同樣的問題。 – Rogy 2013-05-12 12:15:45

+0

我更新了答案。 – 2013-05-12 12:22:55

2

首先將Spinner對象提取到類的成員。

public class DFrag extends DialogFragment 
{ 
    private Spinner mSpinner; 
    ... 

然後從onCreateDialog()函數

public Dialog onCreateDialog(Bundle savedInstanceState) 
{ 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());  
    LayoutInflater inflater = getActivity().getLayoutInflater(); 
    View view = inflater.inflate(R.layout.activity_schedule_select_dialog, null); 
    // Assign spinner 
    mSpinner = (Spinner) view.findViewById(R.id.ssd_weeksSelectSpinner); 
    builder.setView(view); 
    // Set positive and negative buttons here 
    ... 
} 

分配您的微調現在把你的微調值上onCreateView()函數

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
    mSpinner.setSelection(getArguments().getInt("SelectWeek")); 
    ... 
} 

乾杯!

+0

這是一個真正的技巧:膨脹到視圖變量,所以從它調用findViewById。謝謝! – 2016-05-21 22:52:14

相關問題