1

我有一个微调器,当我通过微调器在数据库中插入值时它工作正常,但是当我从数据库中获取值并将索引传递给微调器时它也可以工作,但是当我单击它来更改值时它会给出错误 Java Null Pointer Exception . 请帮帮我。

array_spinner=new String[4];
        array_spinner[0]= sing.gs.getApplicator1();
        array_spinner[1]= sing.gs.getApplicator2();
        array_spinner[2]= sing.gs.getApplicator3();
        array_spinner[3]= sing.gs.getApplicator4();
        app = (Spinner) findViewById(R.id.spnr_app);

        ArrayAdapter adapter = new ArrayAdapter(this,
        android.R.layout.simple_spinner_item, array_spinner);
        app.setAdapter(adapter);

        //singleton check
         if (sing.savedRecord)
            {
                getSetting();
            }

获取设置方法

private void getSetting()
{
    try
    {

        app.setSelection(Integer.valueOf(sing.gs.getApplicator_name()));
        datePicker.setText(sing.gs.getSpray_date().toString());

    }
    catch (Exception e)
    {
        Log.e("Retrieve Error", e.toString());
        e.printStackTrace();
    }
}

LOGCAT

05-16 17:32:58.511: E/AndroidRuntime(910): FATAL EXCEPTION: main
05-16 17:32:58.511: E/AndroidRuntime(910): java.lang.NullPointerException
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:376)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:332)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:328)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.widget.AbsListView.obtainView(AbsListView.java:1409)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.widget.ListView.onMeasure(ListView.java:1127)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.view.View.measure(View.java:8313)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.view.View.measure(View.java:8313)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
05-16 17:32:58.511: E/AndroidRuntime(910):  at com.android.internal.widget.WeightedLinearLayout.onMeasure(WeightedLinearLayout.java:60)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.view.View.measure(View.java:8313)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.view.View.measure(View.java:8313)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.view.View.measure(View.java:8313)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.os.Looper.loop(Looper.java:123)
05-16 17:32:58.511: E/AndroidRuntime(910):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-16 17:32:58.511: E/AndroidRuntime(910):  at java.lang.reflect.Method.invokeNative(Native Method)
05-16 17:32:58.511: E/AndroidRuntime(910):  at java.lang.reflect.Method.invoke(Method.java:507)
05-16 17:32:58.511: E/AndroidRuntime(910):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-16 17:32:58.511: E/AndroidRuntime(910):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-16 17:32:58.511: E/AndroidRuntime(910):  at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

1

请检查它的外观,您的 sing.gs.getApplicator1(); 之一 值为 null .. 请检查所有 sing.gs.getApplicator ..........

原因:转到第 394 行(您的 sdk ArrayAdepter 的第 328 行)

https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/ArrayAdapter.java

第394章

所以这里的文本可能为空或项目可能为空。

但是您使用的是内置布局,因此文本不应为空,因此项目可以为空...。

于 2012-05-16T13:16:15.457 回答
0

发布您的完整代码和详细说明,这是一个示例微调器,希望对您有所帮助,

spinnerSize = (Spinner) findViewById(R.id.spinnerSize);
    sizeArray = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_dropdown_item, size);
    spinnerSize.setAdapter(sizeArray);
    spinnerSize.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> parent, View v, int pos,
                long id) {
            if (sizeCount != 0) {
                Constants.enhancedImage.setTextSize(size[pos]);
                receiver.setTextSize(Integer.parseInt(size[pos]));
                sender.setTextSize(Integer.parseInt(size[pos]));
                message.setTextSize(Integer.parseInt(size[pos]));
            }
            sizeCount++;
        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    });

和 String size[] 是,

String[] size = { "10", "12", "14", "16", "18", "20", "22"};
于 2012-05-16T12:56:39.713 回答