问题描述
当我试图在打一个按钮,在片段中提取从AutoCompleteTextView文本抛出NullPointerException。这是我的code ...
公共类TreeMenuFragment扩展SherlockFragment {
受保护的视图中查看;
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
鉴于= inflater.inflate(R.layout.treemenu,集装箱,假);
//自动完成
AutoCompleteTextView AUTOVIEW =(AutoCompleteTextView)view.findViewById(R.id.edit_message);
的String [] itemOptions = getResources()getStringArray(R.array.edit_message)。
ArrayAdapter<字符串> theAdapter =
新的ArrayAdapter<字符串>(getActivity(),android.R.layout.simple_list_item_1,itemOptions);
autoView.setAdapter(theAdapter);
((BaseAdapter)autoView.getAdapter())notifyDataSetChanged()。
((ViewGroup中)autoView.getParent())removeView(AUTOVIEW)。
container.addView(AUTOVIEW);
按钮B =(按钮)view.findViewById(R.id.post_blacklist_button);
b.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
AutoCompleteTextView EDITTEXT =(AutoCompleteTextView)view.findViewById(R.id.edit_message);
。字符串blackListItem = editText.getText()的toString();
editText.setText();
。MainActivity.getInstance()postBlackListItem(blackListItem);
}
});
返回查看;
}
}
相关的XML是在这里:
< AutoCompleteTextView
机器人:ID =@ + ID / edit_message
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:提示=@字符串/ my_hint/>
<按钮
机器人:ID =@ + ID / post_blacklist_button
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:提示=@字符串/ update_button/>
*的更新:的*当我试图从V改变AutoCompleteTextViev投地查看,我的日志打印如下:
致命异常:主要
显示java.lang.NullPointerException
在com.example.privacyapp.TreeMenuFragment $ 1.onClick(TreeMenuFragment.java:34)
在android.view.View.performClick(View.java:2532)
在android.view.View $ PerformClick.run(View.java:9293)
在android.os.Handler.handleCallback(Handler.java:587)
在android.os.Handler.dispatchMessage(Handler.java:92)
在android.os.Looper.loop(Looper.java:150)
在android.app.ActivityThread.main(ActivityThread.java:4263)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:507)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
在dalvik.system.NativeStart.main(本机方法)
修改
AutoCompleteTextView EDITTEXT =(AutoCompleteTextView)v.findViewById(R.id.edit_message);
到
AutoCompleteTextView EDITTEXT =(AutoCompleteTextView)view.findViewById(R.id.edit_message);
下面 v
是按钮
,你点击,我假设视图
是膨胀的XML这是在那里你会找到 AutoCompleteTextView
。因此,这意味着你的 EDITTEXT
是空
,并抛出一个 NPE
当您尝试调用它的功能就像
editText.getText()的toString();
如果不能解决这个问题,那么请张贴的logcat,但该行仍需要修改
声明 AUTOVIEW
作为成员变量,那么你只需要一次初始化。然后使用该实例的 AutoCompleteTextView
里面的的onClick
来获取文本
A NullPointerException is thrown when I attempt to extract the text from an AutoCompleteTextView upon hitting a button in a Fragment. This is my code...
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;
}
}
The relevant xml is here:
<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" />
*Update:*When I attempted to change the AutoCompleteTextViev cast from v to View, my log prints the following:
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)
Change
AutoCompleteTextView editText=(AutoCompleteTextView) v.findViewById(R.id.edit_message);
to
AutoCompleteTextView editText=(AutoCompleteTextView) view.findViewById(R.id.edit_message);
Here v
is the Button
which you clicked and I'm assuming that view
is the inflated xml which is where you will find the AutoCompleteTextView
. So this means that your editText
is null
and throws a NPE
when you try to call a function on it like
editText.getText().toString();
If this doesn't fix the problem then please post logcat but this line will still need to be changed
Declare autoView
as a member variable then you only have to initialize it once. Then use that instance of your AutoCompleteTextView
inside your onClick
to get the text
这篇关于AutoCompleteTextView空指针在按钮的OnClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!