0

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

    @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>
4

3 回答 3

0

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

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

于 2013-06-10T13:33:25.013 回答
0

findViewById()不会在您的 ListView 项目中找到单选按钮。这getView()就是为了。

于 2013-06-10T13:34:54.300 回答
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)中搜索对象

于 2013-06-10T13:35:27.810 回答