我现在正在实施自定义ListView
。当我尝试从TextView
更改位于CustomAdaper.xml
的{{1}}时。它显示NullPointerException。是不是可以这样做。请帮我找到它。我很困惑。
以下是我的 MainActivity.java 代码:
MainActivity
CustomAdapter.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.xml
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);
}
答案 0 :(得分:1)
试试这个:
txt_Position.setTextSize(TypedValue.COMPLEX_UNIT_DIP, Float.parseFloat(18.0));
答案 1 :(得分:0)
尝试这个
txt_Position.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);