基本上我有 singleitemactivity,其中在通过 URL 获取 JSON 数据后显示有关单个项目的信息。当我nullpointerexception
尝试setVisibility.GONE
在onPostExecute
. 添加 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>