对于单选按钮执行setVisibility.GONE时的nullpointerexception

时间:2013-06-10 13:27:45

标签: java android nullpointerexception

基本上我有单一活动,在通过URL获取JSON数据后显示有关单个项目的信息。当我尝试nullpointerexception setVisibility.GONE内的单选按钮时,我得到onPostExecute(请查看logcat输出)。 添加if else语句以检查产品属于哪个类别。例如,Pizza可以有多种尺寸,因此我显示4种价格,而尺寸不适用于边线,因此只显示一个价格。所以为了实现这个功能,如果产品类别是“副业”,我试图隐藏其他3个radiobuttons。我希望你明白。

    @Override
    protected void onPostExecute(String file_url) {
        pDialog.dismiss();
        // updating UI from Background Thread 
        runOnUiThread(new Runnable() {
            @Override
    public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */

                if(check<=2)
                {
                    Log.i("Check Value","The check value is" + check);
                    Log.i("Adapter:"," Category Value is less than 2");
                ListAdapter adapter = new SimpleAdapter(
                        SingleItemActivity.this, selquesList,
                        R.layout.single_item_layout, new String[] { TAG_newPID,
                                TAG_NAME, TAG_INFO,TAG_SMALLPRICE,TAG_MEDIUMPRICE,TAG_LARGE,TAG_xLARGE },
                        new int[] { R.id.itemid, R.id.itemname, R.id.iteminfo,R.id.smallprice, R.id.mediumprice, R.id.largeprice,R.id.xlargeprice});
                // updating listview
                setListAdapter(adapter);
                }
                else
                {
                    ListAdapter adapter2 = new SimpleAdapter(
                        SingleItemActivity.this, selquesList,
                        R.layout.single_item_layout, new String[] { TAG_newPID,
                                TAG_NAME, TAG_INFO, TAG_PRICE },
                        new int[] { R.id.itemid, R.id.itemname, R.id.iteminfo, R.id.price});

                    RadioButton mediumRadioButton = (RadioButton) findViewById(R.id.medium);
                    RadioButton largeRadioButton = (RadioButton) findViewById(R.id.large);
                    RadioButton xlargeRadioButton = (RadioButton) findViewById(R.id.xlarge);                         
                    Intent intentradiocheck = getIntent();
                    int radioavailcheck = intentradiocheck.getIntExtra("int_value", 0);
                    Log.i("SingleItemActivity","Value of radioavailcheck: "+radioavailcheck);                        
                    if(radioavailcheck > 2)
                    {
                        Log.i("SingleItemActivity","Inside the if loop because radioavailcheck>2"); 
                        mediumRadioButton.setVisibility(View.GONE);
                        largeRadioButton.setVisibility(View.GONE);
                        xlargeRadioButton.setVisibility(View.GONE);
                    }

                setListAdapter(adapter2);
                }
            }
        });
    }

LogCat输出:

 06-10 18:32:23.885: I/SingleItemActivity(306): Inside oncreate
 06-10 18:32:24.704: D/dalvikvm(306): GC_FOR_MALLOC freed 3410 objects / 387688 bytes in 341ms
 06-10 18:32:25.624: D/All Items:(306): {"success":1,"menu":[{"price":"3.95","itemID":"8","categoryID":"3","itemname":"Beer Nastro","iteminfo":"Beer","small":"0","large":"0","medium":"0","xlarge":"0","discount":"0"}]}
 06-10 18:32:25.634: I/SingleItemActivity(306): Value of radioavailcheck: 3
 06-10 18:32:25.634: I/SingleItemActivity(306): Inside the if loop because radioavailcheck>2
 06-10 18:32:25.634: D/AndroidRuntime(306): Shutting down VM
 06-10 18:32:25.634: W/dalvikvm(306): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
 06-10 18:32:25.644: E/AndroidRuntime(306): FATAL EXCEPTION: main
 06-10 18:32:25.644: E/AndroidRuntime(306): java.lang.NullPointerException
 06-10 18:32:25.644: E/AndroidRuntime(306):     at com.wbs.ginos.SingleItemActivity$LoadAllSelques$1.run(SingleItemActivity.java:243)
 06-10 18:32:25.644: E/AndroidRuntime(306):     at android.app.Activity.runOnUiThread(Activity.java:3707)
 06-10 18:32:25.644: E/AndroidRuntime(306):     at com.wbs.ginos.SingleItemActivity$LoadAllSelques.onPostExecute(SingleItemActivity.java:207)
 06-10 18:32:25.644: E/AndroidRuntime(306):     at com.wbs.ginos.SingleItemActivity$LoadAllSelques.onPostExecute(SingleItemActivity.java:1)
 06-10 18:32:25.644: E/AndroidRuntime(306):     at android.os.AsyncTask.finish(AsyncTask.java:417)
 06-10 18:32:25.644: E/AndroidRuntime(306):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
 06-10 18:32:25.644: E/AndroidRuntime(306):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
 06-10 18:32:25.644: E/AndroidRuntime(306):     at android.os.Handler.dispatchMessage(Handler.java:99)
 06-10 18:32:25.644: E/AndroidRuntime(306):     at android.os.Looper.loop(Looper.java:123)
 06-10 18:32:25.644: E/AndroidRuntime(306):     at android.app.ActivityThread.main(ActivityThread.java:4627)
 06-10 18:32:25.644: E/AndroidRuntime(306):     at java.lang.reflect.Method.invokeNative(Native Method)
 06-10 18:32:25.644: E/AndroidRuntime(306):     at java.lang.reflect.Method.invoke(Method.java:521)
 06-10 18:32:25.644: E/AndroidRuntime(306):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 06-10 18:32:25.644: E/AndroidRuntime(306):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
 06-10 18:32:25.644: E/AndroidRuntime(306):     at dalvik.system.NativeStart.main(Native Method)

`

single_item_layout.xml中的Radiogroup代码

      <RadioGroup
        android:id="@+id/group1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="90"
        android:orientation="vertical" >

        <RadioButton
            android:id="@+id/small"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true" />

        <RadioButton
            android:id="@+id/medium"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioButton
            android:id="@+id/large"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioButton
            android:id="@+id/xlarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RadioGroup>

3 个答案:

答案 0 :(得分:0)

我的猜测是onPostExecute()在调用setContentView()之前完成。既然您似乎在onCreate中启动了AsyncTask,我建议您延迟到调用setContentView()之后吗? 如果这不是原因,那么单选按钮的ID是错误的。

在旁注中,您不需要添加对runOnUiThread()的调用,onPostExecute()将始终在UI线程上运行,如下所述:http://android-developers.blogspot.ca/2009/05/painless-threading.html

答案 1 :(得分:0)

findViewById()将无法在ListView项目中找到单选按钮。这就是getView()的用途。

答案 2 :(得分:0)

您需要更改getView()ViewBinder内的可见性。基本上,以下调用将返回null

RadioButton mediumRadioButton = (RadioButton) findViewById(R.id.medium);
RadioButton largeRadioButton = (RadioButton) findViewById(R.id.large);
RadioButton xlargeRadioButton = (RadioButton) findViewById(R.id.xlarge);

因为您正在主布局中的ListView内搜索对象(ListView之外)