0

Fragment의 단추를 눌렀을 때 AutoCompleteTextView에서 텍스트를 추출하려고하면 NullPointerException이 throw됩니다.AutoCompleteTextView Button의 OnClickListener에있는 NullPointer

<AutoCompleteTextView 
    android:id="@+id/edit_message" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="@string/my_hint" /> 

<Button 
    android:id="@+id/post_blacklist_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:hint="@string/update_button" /> 

* 업데이트 : 나는 V에서 AutoCompleteTextViev 캐스트를 변경하려고 할 때 *이보기에 이것은 내 코드 ...

public class TreeMenuFragment extends SherlockFragment{ 
protected View view; 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
    view = inflater.inflate(R.layout.treemenu, container, false); 
    //Auto-Complete 
    AutoCompleteTextView autoView = (AutoCompleteTextView) view.findViewById(R.id.edit_message); 
    String[] itemOptions = getResources().getStringArray(R.array.edit_message); 

    ArrayAdapter<String> theAdapter = 
      new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, itemOptions); 
    autoView.setAdapter(theAdapter); 
    ((BaseAdapter) autoView.getAdapter()).notifyDataSetChanged(); 
    ((ViewGroup) autoView.getParent()).removeView(autoView); 

    container.addView(autoView); 

    Button b = (Button) view.findViewById(R.id.post_blacklist_button); 
    b.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      AutoCompleteTextView editText=(AutoCompleteTextView) view.findViewById(R.id.edit_message); 
      String blackListItem = editText.getText().toString(); 

      editText.setText(""); 
      MainActivity.getInstance().postBlackListItem(blackListItem); 

     } 
    }); 
    return view; 
} 

}

관련 XML은 여기입니다 , 내 기록은 다음을 인쇄합니다 :

FATAL EXCEPTION: main 
java.lang.NullPointerException 
at com.example.privacyapp.TreeMenuFragment$1.onClick(TreeMenuFragment.java:34) 
at android.view.View.performClick(View.java:2532) 
at android.view.View$PerformClick.run(View.java:9293) 
at android.os.Handler.handleCallback(Handler.java:587) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:150) 
at android.app.ActivityThread.main(ActivityThread.java:4263) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:507) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
at dalvik.system.NativeStart.main(Native Method) 
+0

'TreeMenuFragment'의 34 번째 줄에는 무엇이 있습니까? – codeMagic

+0

문자열 blackListItem = editText.getText(). toString(); – NumenorForLife

+0

업데이트 된 답변을보십시오 – codeMagic

답변

1

변경

AutoCompleteTextView editText=(AutoCompleteTextView) v.findViewById(R.id.edit_message); 

여기 v

AutoCompleteTextView editText=(AutoCompleteTextView) view.findViewById(R.id.edit_message); 

로 클릭 한 Button 내가 view 당신이 AutoCompleteTextView를 찾을 곳이다 팽창 XML이라고 가정하고있다. 그래서 당신의 editTextnull 것을 의미하고이 문제가 해결되지 않으면 다음 로그 캣을 게시하시기 바랍니다

 editText.getText().toString(); 

처럼에 함수를 호출하려고 할 때 NPE을 발생하지만,이 라인은 여전히 ​​변경해야합니다

회원 변수로 autoView을 선언하면 한 번만 초기화하면됩니다. 그런 다음 onClick 안에 AutoCompleteTextView의 해당 인스턴스를 사용하여 텍스트를 가져옵니다.

+0

안녕하세요 CodeMagic, 의견을 보내 주셔서 감사합니다. 다음 단계에서 내 업데이트를 확인하십시오. – NumenorForLife

+0

나는 당신의 업데이 트에 내 코멘트를 참조하십시오 :) – codeMagic