单击微调器选择项目时获取空指针异常

时间:2011-08-18 06:07:41

标签: android nullpointerexception spinner

我已经从webservice填充了微调器。数据被添加到适配器中,并且当我点击微调器时显示在微调器中。我需要做的是我想要从微调器中选择的项目然后拆分它并传递支付号码到其他网络服务。

当我在OnItemSelectedListener上添加以下代码时,我得到空指针,如下面的代码所示。

请查看我的代码并告诉我我做错了什么:

我填充微调器的代码:

        results = new JSONArray(new String(buffer));
        if (results != null) {
            Log.v("val", results.getString(0));
            for (int i = 0; i <= results.length() - 1; i++) {
                // val[i] = results.getString(i);
                String val1 = results.getString(i);
                val[i] = val1;
                val1 = "";
                Log.v("Val", "[" + i + "]" + results.getString(i));
            }
        } else {
            Log.v("results", "results are null");
        }
        ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, val);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spn1.setAdapter(adapter);

spinner的OnItemSelectedListener的代码:

public class MyOnItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent, View view, int pos,
            long id) {
        String PayoutNo = spnPayout.getItemAtPosition(pos).toString();
        Log.v("seleced item", PayoutNo);
        Toast.makeText(PayoutDetailsActivity.this, "selected item is"+PayoutNo, Toast.LENGTH_LONG).show();
        Log.v("TAG","inside on click");
        //String PayoutNo = spnPayout.getItemAtPosition(pos).toString();
        Log.v("selected item",PayoutNo);
        String[] strPayoutNo = new String[15];
        strPayoutNo = PayoutNo.split("-");
        Log.v("TAG",strPayoutNo[0]);
        getPayoutDetails(strPayoutNo[0]);//in this function i am calling another webservice
    }
    public void onNothingSelected(AdapterView parent) {
        // Do nothing.
    }
}

记录:

08-18 12:01:21.542: ERROR/AndroidRuntime(543): FATAL EXCEPTION: main
08-18 12:01:21.542: ERROR/AndroidRuntime(543): java.lang.NullPointerException
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:376)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:329)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:325)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.widget.AbsListView.obtainView(AbsListView.java:1294)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.widget.ListView.onMeasure(ListView.java:1109)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.view.View.measure(View.java:8171)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.view.View.measure(View.java:8171)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at com.android.internal.widget.WeightedLinearLayout.onMeasure(WeightedLinearLayout.java:60)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.view.View.measure(View.java:8171)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.view.View.measure(View.java:8171)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.view.View.measure(View.java:8171)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.os.Looper.loop(Looper.java:123)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at java.lang.reflect.Method.invokeNative(Native Method)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at java.lang.reflect.Method.invoke(Method.java:521)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-18 12:01:21.542: ERROR/AndroidRuntime(543):     at dalvik.system.NativeStart.main(Native Method)
08-18 12:01:21.576: WARN/ActivityManager(59):   Force finishing activity com.biz.mlm/.PayoutDetailsActivity
08-18 12:01:22.163: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{450cdbd8 com.biz.mlm/.PayoutDetailsActivity}

1 个答案:

答案 0 :(得分:0)

试试这样。

MyOnItemSelectedListener myOnItemSelectedListener = new MyOnItemSelectedListener();

---------------------

ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, val);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spn1.setAdapter(adapter);
spn1.seOnItemSelectedListener( myOnItemSelectedListener);