我现在正在实现自定义ListView
。当我尝试从TextView
更改CustomAdaper.xml
中的MainActivity
时。它显示NullPointerException。这样做是不可能的吗。请帮我找到它。我很困惑。
以下是我的MainActivity.java代码:
setContentView(R.layout.activity_main);
txt_Position = (TextView)findViewById(R.id.txtposition);//still ok
txt_memberName = (TextView)findViewById(R.id.txtmemberName);// still ok
LayoutParams params = (LayoutParams) layout_Date.getLayoutParams();
LayoutParams params1 = (LayoutParams) layout_List.getLayoutParams();
LayoutParams params2 = (LayoutParams) layout_Rotation.getLayoutParams();
int screenSize = getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK;
switch(screenSize) {
case Configuration.SCREENLAYOUT_SIZE_LARGE:
Toast.makeText(this, "Large", Toast.LENGTH_LONG).show();
params.height = 100;
params1.height = 330 ;
params2.height = 200;
txt_Position.setTextSize((float)18.0); //locate in CustomAdapter.xml and this is the line logCat Error Pointed .
break;
CustomAdapter.Java
public class CustomAdapter extends BaseAdapter {
private Activity activity;
private ArrayList data;
private static LayoutInflater inflater=null;
public Resources res;
ListModel getList;
int size ;
int i=0;
public CustomAdapter(Activity a, ArrayList d,Resources resLocal, int size) {
activity = a;
data=d;
res = resLocal;
this.size = size;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
if(data.size()<=0)
return 1;
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public TextView txt_Name;
public TextView txt_Status;
public TextView txt_Position;
public ImageButton imgbtn_senka;
public ImageButton imgbtn_fuenka;
//public TableRow tblrow_btn;
public RelativeLayout layoutbtn;
public ImageView image;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View vi=convertView;
final ViewHolder holder;
if(convertView==null){
/********** Inflate tabitem.xml file for each row ( Defined below ) ************/
vi = inflater.inflate(R.layout.customadapter, null);
holder=new ViewHolder();
holder.txt_Name = (TextView)vi.findViewById(R.id.txtmemberName);
holder.txt_Status = (TextView)vi.findViewById(R.id.txtmemberStatus);
holder.image = (ImageView)vi.findViewById(R.id.imgPlayer);
holder.imgbtn_fuenka = (ImageButton) vi.findViewById(R.id.btnfusenka);
holder.imgbtn_senka = (ImageButton) vi.findViewById(R.id.btnsenka);
holder.txt_Position = (TextView)vi.findViewById(R.id.txtposition);
holder.layoutbtn = (RelativeLayout)vi.findViewById(R.id.layoutbutton);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
if(data.size()<=0)
{
holder.txt_Name.setText("No Data");
}
else
{
getList=null;
getList = (ListModel) data.get(position);
if(getList.getMemberStatus() == 3){
holder.txt_Status.setVisibility(View.INVISIBLE);
holder.imgbtn_fuenka.setVisibility(View.VISIBLE);
holder.imgbtn_senka.setVisibility(View.VISIBLE);
if(getList.getEntry() == 1){
holder.imgbtn_senka.setImageResource(R.drawable.sankagray);
holder.imgbtn_senka.setEnabled(false);
}
else if(getList.getEntry() == 0){
holder.imgbtn_fuenka.setImageResource(R.drawable.fusankagray);
holder.imgbtn_fuenka.setEnabled(false);
}
holder.imgbtn_senka.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String entry = "1";
CustomListViewAndroidExample sct = (CustomListViewAndroidExample)activity;
sct.updateData(position , entry);
holder.imgbtn_senka.setImageResource(R.drawable.sankagray);
holder.imgbtn_senka.setEnabled(false);
holder.imgbtn_fuenka.setEnabled(true);
holder.imgbtn_fuenka.setImageResource(R.drawable.fusanka);
}
});
holder.imgbtn_fuenka.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String entry = "0";
CustomListViewAndroidExample sct = (CustomListViewAndroidExample)activity;
sct.updateData(position , entry);
holder.imgbtn_fuenka.setImageResource(R.drawable.fusankagray);
holder.imgbtn_fuenka.setEnabled(false);
holder.imgbtn_senka.setEnabled(true);
holder.imgbtn_senka.setImageResource(R.drawable.sanka);
}
});
}
else{
holder.txt_Status.setVisibility(View.VISIBLE);
holder.imgbtn_fuenka.setVisibility(View.INVISIBLE);
holder.imgbtn_senka.setVisibility(View.INVISIBLE);
if(getList.getEntry() == 0){
holder.txt_Status.setText("未確認");
}
else if(getList.getEntry() == 1){
holder.txt_Status.setText("参加");
}
else if(getList.getEntry() == 2){
holder.txt_Status.setText("不参加");
}
else if(getList.getEntry() == 3){
holder.txt_Status.setText("取消");
}
}
holder.txt_Name.setText(getList.getMemberName());
holder.txt_Position.setText(getList.getPosition());
String url="http://10.0.2.2/football365/Photo/"+getList.getImage();
try {
Bitmap bitmap= BitmapFactory.decodeStream((InputStream) new URL(url).getContent());
Bitmap resized = Bitmap.createScaledBitmap(bitmap, 76, 76, false);
holder.image.setImageBitmap(resized);
}
catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// holder.image.setImageResource(res.getIdentifier("http://10.0.2.2/football365/Photo/"+getList.getImage(),null,null));
}
if(size == 1){
holder.txt_Name.setTextSize((float)20.0);
}
return vi;
}
public void changeSize(){
ViewHolder holder = new ViewHolder();
holder.txt_Position.setTextSize((float)18.0);
holder.txt_Name.setTextSize((float)20.0);
}
}
这是CustomAdapter.xml
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="@+id/tblrowtotal"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:background="@drawable/blackrow2"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imglin1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:src="@drawable/liney" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:layout_marginLeft="3dp"
android:id="@+id/imgPlayer"
android:layout_width="60dp"
android:layout_height="70dp"
android:layout_gravity="left|top"
android:layout_marginTop="2dp"
android:scaleType="center"
style="@style/AppBaseTheme"
android:background="@null"
/>
</LinearLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:paddingTop="0dip" >
<TableRow>
<TextView
android:id="@+id/txtmemberName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginTop="15dp"
android:textColor="#FFFFFF"
android:textSize="13sp" />
</TableRow>
<ImageView
android:id="@+id/imgview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/linex" />
<TableRow android:layout_width="wrap_content" >
<ImageView
android:id="@+id/position"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginTop="10dp"
android:src="@drawable/c_position" />
<TextView
android:id="@+id/txtposition"
android:layout_width="45dp"
android:layout_height="30dp"
android:layout_marginLeft="3dp"
android:layout_marginTop="7dp"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<ImageView
android:id="@+id/img2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleType="center"
android:layout_marginLeft="0dp"
android:src="@drawable/liney" />
<RelativeLayout
android:id="@+id/layoutbutton"
android:layout_width="wrap_content"
android:gravity="right"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/txtmemberStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="2dp"
android:gravity="center_horizontal"
android:textColor="#FFFFFF"
android:textSize="16sp" />
<ImageButton
android:id="@+id/btnfusenka"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginRight="3dp"
android:background="@null"
android:focusable="false"
android:src="@drawable/fusanka"/>
<ImageButton
android:id="@+id/btnsenka"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@null"
android:focusable="false"
android:src="@drawable/sanka"
/>
</RelativeLayout>
</TableRow>
<ImageView
android:id="@+id/img3"
android:layout_width="fill_parent"
android:layout_marginLeft="0dp"
android:layout_height="wrap_content"
android:src="@drawable/linex" />
</TableLayout>
</TableRow>
最佳答案
尝试这个:
txt_Position.setTextSize(TypedValue.COMPLEX_UNIT_DIP, Float.parseFloat(18.0));