我正在重新发布这个问题,因为即使没有阅读问题,也有一个人标记了我以前的任务副本。
我正在创建ListView的适配器,用于将我的自定义布局加载为列表成员。
布局包含
3 RelativeLayout
后者各包含一个TextView
和一个按钮。
在ListView中最多有2行,简称最多6行RelativeLayout。由于需要是递归的,我使用了TextView,RelativeLayout,Button数组。为了通过findViewById()分配Id,我使用int数组,其中包含在layout中定义的所有id。现在我将小部件分配给他们的Id后,当我调用任何方法时,它会给出空指针异常,这意味着小部件没有分配Id,但我无法理解为什么它没有被分配。
还有一点是ListView在RecyclerView.Adapter中填充。 整个场景是这样的:
i)我有Tabs& amp;寻呼机。因此,它为每个选项卡使用片段。我在这里只讨论第一个标签。
ii)在片段中我有一个RecyclerView,它将加载卡片。
iii)该卡有许多小部件,其中一个是ListView。所以在RecyclerView.Adapter中我将为此List调用ArrayAdapter
我的代码如下:
1)ArrayAdapter(显示错误)
public class ElectionOptionAdapter extends ArrayAdapter<String> {
Activity context;
int noOption;
String[] options, description;
TextView[] optionText, optionDescription;
RelativeLayout[] optionImage;
Button[] optionButton;
int[]
text = new int[]{R.id.ERowOption1,R.id.ERowOption2,R.id.ERowOption3},
des = new int[]{R.id.ERowOption1des,R.id.ERowOption2des,R.id.ERowOption3des},
img = new int[]{R.id.ERowOption1Img,R.id.ERowOption2Img,R.id.ERowOption3Img},
but = new int[]{R.id.ERowOption1button,R.id.ERowOption2button,R.id.ERowOption3button};
public ElectionOptionAdapter(Activity context, int length, String[] options, String[] description) {
super(context, R.layout.election_type_options,options);
this.context = context;
this.options = options;
this.description = description;
noOption=options.length;
optionText = new TextView[noOption];
optionDescription = new TextView[noOption];
optionImage = new RelativeLayout[noOption];
optionButton = new Button[noOption];
}
@Override
public int getCount() {
return 1;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View card = inflater.inflate(R.layout.election_type_options, parent, false);
for (int i = 1+(3*position); i <= Math.min(3+(3*position),options.length); i++) {
optionText[i-1] = (TextView) card.findViewById(text[(i%4)-1]);
optionDescription[i-1] = (TextView) card.findViewById(des[(i%4)-1]);
optionImage[i-1] = (RelativeLayout) card.findViewById(img[(i%4)-1]);
optionButton[i-1] = (Button) card.findViewById(but[(i%4)-1]);
//The error message points here
optionImage[i-1].setVisibility(View.VISIBLE);
optionText[i-1].setText(options[i-1]);
optionDescription[i-1].setText(description[i-1]);
}
optionButton[0].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
visibilityChange(0);
}
});
optionButton[1].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
visibilityChange(1);
}
});
optionButton[2].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
visibilityChange(2);
}
});
optionButton[3].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
visibilityChange(3);
}
});
optionButton[4].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
visibilityChange(4);
}
});
optionButton[5].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
visibilityChange(5);
}
});
return card;
}
private void visibilityChange(int pos){
if(optionDescription[pos].getVisibility()==View.VISIBLE)
optionDescription[pos].setVisibility(View.INVISIBLE);
else
optionDescription[pos].setVisibility(View.VISIBLE);
}
}
2)错误讯息:
java.lang.NullPointerException:
Attempt to invoke virtual method 'void android.widget.RelativeLayout.setVisibility(int)'
on a null object reference at com.package.ElectionOptionAdapter.getView(ElectionOptionAdapter.java:72)
3)布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp">
<RelativeLayout
android:tag="@+id/ERowOption1Img"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="4dp"
android:visibility="invisible">
<TextView
android:tag="@+id/ERowOption1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="25dp"
android:textStyle="bold"
android:text=" A) Gaurav Jindal"
android:layout_alignParentBottom="true" />
<TextView
android:background="#FFFFFF"
android:tag="@+id/ERowOption1des"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"/>
<Button
android:tag="@+id/ERowOption1button"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:background="@drawable/info_election_option"/>
</RelativeLayout>
<RelativeLayout
android:tag="@+id/ERowOption2Img"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="4dp"
android:visibility="invisible">
<TextView
android:tag="@+id/ERowOption2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="25dp"
android:textStyle="bold"
android:text=" A) Gaurav Jindal"
android:layout_alignParentBottom="true" />
<TextView
android:tag="@+id/ERowOption2des"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"/>
<Button
android:tag="@+id/ERowOption2button"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:background="@drawable/info_election_option"/>
</RelativeLayout>
<RelativeLayout
android:tag="@+id/ERowOption3Img"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="4dp"
android:visibility="invisible">
<TextView
android:tag="@+id/ERowOption3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="25dp"
android:textStyle="bold"
android:text=" A) Gaurav Jindal"
android:layout_alignParentBottom="true" />
<TextView
android:tag="@+id/ERowOption3des"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"/>
<Button
android:tag="@+id/ERowOption3button"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:background="@drawable/info_election_option"/>
</RelativeLayout>
</LinearLayout>
4)上面提到的调用ListAdapter的RecyclerAdapter是:
public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.ViewHolder> {
JSONArray jsonArray;
Activity context;
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView userid,question;
ImageView userdp;
ListView optionList;
public ViewHolder(View card) {
super(card);
userid = (TextView) card.findViewById(R.id.useridInCard);
question = (TextView) card.findViewById(R.id.questionInCard);
optionList = (ListView) card.findViewById(R.id.optionInCard);
}
}
FeedAdapter(Activity context,JSONArray jsonArray){
this.context=context;
this.jsonArray = jsonArray;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ques_card,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
try {
JSONObject currentFeed = jsonArray.getJSONObject(position);
holder.userid.setText(currentFeed.getString("_id"));
holder.question.setText(currentFeed.getString("Question"));
int noOptions = currentFeed.getInt("noOption");
String[] option = new String[noOptions];
for(int i=1;i<=noOptions;i++)
option[i-1]=optionName(i)+currentFeed.getString("Option"+i);
String[] description = new String[noOptions];
for(int i=1;i<=noOptions;i++)
description[i-1]=currentFeed.getString("Description"+i);
ElectionOptionAdapter electionAdapter;
if(noOptions<=3)
electionAdapter= new ElectionOptionAdapter(context,1,option,description);
else
electionAdapter= new ElectionOptionAdapter(context,2,option,description);
holder.optionList.setAdapter(electionAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return 1;
}
}
答案 0 :(得分:0)
在Layout's xml
文件中,您应该使用属性
android:id="@+id/foobar"
而不是
android:tag="@+id/foobar"