Java 执行setVisibility.Goe时为单选按钮执行nullpointerexection
基本上,我有singleitemactivity,其中通过URL获取JSON数据后,将显示关于单个项目的信息。当我尝试Java 执行setVisibility.Goe时为单选按钮执行nullpointerexection,java,android,nullpointerexception,Java,Android,Nullpointerexception,基本上,我有singleitemactivity,其中通过URL获取JSON数据后,将显示关于单个项目的信息。当我尝试setVisibility时,我得到一个nullpointerexception(请查看logcat输出)。对于onPostExecute中的单选按钮,消失了。 添加if-else语句是为了检查产品属于哪个类别。例如,比萨饼可以有多种尺寸,所以我显示了4种价格,而尺寸不适用于副业,所以它只显示一种价格。因此,为了实现这一功能,如果产品类别为“副业”,我尝试隐藏其他3个单选按钮。我
setVisibility时,我得到一个nullpointerexception
(请查看logcat输出)。对于onPostExecute
中的单选按钮,消失了。
添加if-else语句是为了检查产品属于哪个类别。例如,比萨饼可以有多种尺寸,所以我显示了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);
}
}
});
}
`
单个_item_layout.xml中的射线组代码
<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>
我的猜测是onPostExecute()在调用setContentView()之前完成。由于您似乎是在onCreate内启动AsyncTask,我建议您延迟到调用setContentView()之后再执行吗?
如果这不是原因,那么单选按钮的ID是错误的
另一方面,您不需要添加对runOnUiThread()
的调用,onPostExecute()将始终在UI线程上运行,如下所述:findViewById()
将不会在ListView项中找到单选按钮。这就是getView()
的作用。您需要在getView()或ViewBinder中更改可见性。基本上,以下调用将返回null
:
RadioButton mediumRadioButton = (RadioButton) findViewById(R.id.medium);
RadioButton largeRadioButton = (RadioButton) findViewById(R.id.large);
RadioButton xlargeRadioButton = (RadioButton) findViewById(R.id.xlarge);
由于您正在主布局的列表视图
内(在列表视图
外)搜索对象很明显,其中一个按钮为空。所以请检查您的id…您没有设置ContentView。请检查所有三个的id。@neoh是对的。无论如何,请检查com.wbs.ginos.SingleItemActivity$LoadAllSelques$1.run(SingleItemActivity.java:243)这一行的确切内容是什么?为什么要使用runOnUiThread
?onPostExecute已经在UI线程中。谢谢,这可能就是原因。我已经编辑了我的答案,因此您可以检查ID是否正确。你能告诉我该如何处理你的建议解决方案吗?谢谢。那么,我应该如何继续使用getView()?您需要像示例中那样重写SimpleAdapter的getView()方法,并检查设置视图可见性所需的条件(对于每个单选按钮),每次适配器在ListView行中创建视图时都会调用getView(),如果您重写它,您可以根据数据更改ListView中每一行的外观,这非常感谢。我会试试看。
RadioButton mediumRadioButton = (RadioButton) findViewById(R.id.medium);
RadioButton largeRadioButton = (RadioButton) findViewById(R.id.large);
RadioButton xlargeRadioButton = (RadioButton) findViewById(R.id.xlarge);