好的,我尝试在stackoverflow线程上大量搜索此错误,但无济于事。请帮助我,我真的很沮丧。
这是我的Menu.java类的代码,其中我有3个容器,如数字选择器,自动完成文本视图和微调器。我使用相同的findviewById()来设置所有三个容器,只有微调器抛出Null Pointer Exception。所有三个容器都在同一视图中。
public class Menu extends AppCompatActivity {
AutoCompleteTextView autoCompleteTextView;
ArrayList<String> ekdum_final_order_names = new ArrayList<>();
ArrayAdapter<String> adapter_menu;
ArrayAdapter<String> adapter ;
Spinner spinner ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.menu_full);
//item_names = getResources().getStringArray(R.array.menu_items);
adapter_menu = new ArrayAdapter<>(this, R.layout.item_layout,R.id.txt,stringArray);
ArrayAdapter<String> adapter_menu = new ArrayAdapter<>(this,android.R.layout.simple_expandable_list_item_1,stringArray);
autoCompleteTextView.setAdapter(adapter_menu);
spinner = (Spinner)findViewById(R.id.dekhane_ke_liye_spinner);
adapter = new ArrayAdapter<>(this,android.R.layout.simple_expandable_list_item_1,ekdum_final_order_names);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
//number picker
nopicker = (NumberPicker)findViewById(R.id.numberPicker);
if (nopicker != null) {
nopicker.setMaxValue(10);
nopicker.setMinValue(1);
nopicker.setWrapSelectorWheel(false);
}
onClickListenerButton();
}
public void onStart(){
super.onStart();
RequestQueue requestQueue= Volley.newRequestQueue(this);
// Create json array request
JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(Request.Method.POST,"http://170.241.241.198/volley.php",new Response.Listener<JSONArray>(){
public void onResponse(JSONArray jsonArray){
for(int i=0;i<jsonArray.length();i++){
try {
JSONObject jsonObject=jsonArray.getJSONObject(i);
stringArray.add(jsonObject.getString("items"));
stringArray_id.add(jsonObject.getString("id"));
stringArray_price.add(jsonObject.getString("Price"));
} catch (JSONException e) {
e.printStackTrace();
}
}
adapter_menu.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.e("Error", "Unable to parse json array");
}
});
requestQueue.add(jsonArrayRequest);
}
}
这是activity_menu.xml文件的代码:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:isScrollContainer="false">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#ffffff">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/menu_full"
android:hint="@string/select_item"
/>
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/numberPicker"
android:layout_alignParentEnd="true"
android:layout_below="@+id/menu_full" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ordered_items"
android:id="@+id/some_text_view"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:textSize="20dp" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/dekhane_ke_liye_spinner"
android:layout_below="@+id/some_text_view"
android:layout_alignParentStart="true"
android:spinnerMode="dropdown" />
</RelativeLayout>
</ScrollView>
这是logcat:
07-13 14:57:05.589 21680-21680/com.example.gandhjee.pantry_order E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.gandhjee.pantry_order, PID: 21680
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gandhjee.pantry_order/com.example.gandhjee.pantry_order.Menu}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
at com.example.gandhjee.pantry_order.Menu.onCreate(Menu.java:69)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我不知道为什么我仍然会收到此错误。任何帮助将不胜感激。
P.S。 - 这个问题不是What is a NullPointerException, and how do I fix it?的重复,因为我已经检查了这个内部的线程没有解决我的问题。我解决了使用这个线程在数字选择器中得到的NullPointer异常,但我无法摆脱我在旋转器上得到的NullPointer异常。因此,请不要低调投票或至少在投票前提供解决方案。
答案 0 :(得分:0)
问题不在微调器中,而是在适配器微调器
中检查代码中的这两行
adapter = new ArrayAdapter<>(this,android.R.layout.simple_expandable_list_item_1,ekdum_final_order_names);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
答案 1 :(得分:0)
取消注释此行,您将空数组传递给适配器,这就是获取错误的原因。
item_names = getResources().getStringArray(R.array.menu_items);
并将此数组传递给适配器
在你的情况下,你正在传递stringArray
stringArray
声明并初始化?
//stringArray= getResources().getStringArray(R.array.menu_items);
adapter_menu = new ArrayAdapter<>(this, R.layout.item_layout,R.id.txt,stringArray);
答案 2 :(得分:0)
您的数据在ArrayAdapter中不可用,这就是为什么它会显示NullPointer Exception。 试试这个。
ArrayList<String> countries = new ArrayList<String>();
countries.add("India");
countries.add("Canada");
countries.add("U.S");
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, countries);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
我希望它会对你有所帮助。