我有这个自定义适配器,我不知道为什么如果视图在布局中膨胀会引发异常。
然后我使用适配器而不是一个新类来显示listview。什么是null?
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.save.coinch.R;
import com.save.coinch.SQLiteDB;
import com.save.coinch.responses.Saving;
import com.save.coinch.responses.Transaction;
import com.save.coinch.responses.User;
import com.save.coinch.utils.CacheUtils;
import com.save.coinch.utils.CommonUtils;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
public class TransactionsAdapter extends BaseAdapter {
private List<Transaction> items;
LayoutInflater li;
public TransactionsAdapter(Context context, List<Transaction> items) {
super();
this.items = items;
this.li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
holder = new ViewHolder();
//LayoutInflater li = LayoutInflater.from(getContext());
v = li.inflate(R.layout.trans_item, parent, false);
null pointer exception here
holder.txtViewAmount = (TextView) convertView.findViewById(R.id.trans_amount);
holder.txtViewDate = (TextView) convertView.findViewById(R.id.trans_date);
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.trans_title);
convertView.setTag(holder);
}else
holder = (ViewHolder) convertView.getTag();
Transaction app = items.get(position);
if (app != null) {
holder.txtViewDate.setText(app.getDate());
holder.txtViewAmount.setText(Float.toString(app.getAmount()));
holder.txtViewTitle.setText(app.getTitle());
}
return v;
}
public static class ViewHolder {
TextView txtViewAmount;
TextView txtViewDate;
TextView txtViewTitle;
}
}
Trans_Item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip">
<!-- ListRow Left side Thumbnail image -->
<TextView
android:id="@+id/trans_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/tit_prin"
android:textAlignment="center"
android:textColor="#ff902020"
android:textSize="10sp"
android:textStyle="bold"
android:typeface="sans"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/trans_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tit_prin"
android:textSize="15sp"
android:textStyle="bold"
android:layout_alignBottom="@+id/trans_amount"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/trans_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tit_prin"
android:textSize="25sp"
android:textStyle="bold"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<!--</LinearLayout>-->
</RelativeLayout>
ShowList.java
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;
import com.save.coinch.adapters.TransactionsAdapter;
import com.save.coinch.interfaces.RequestCallBack;
import com.save.coinch.network.FetchDataListener;
import com.save.coinch.network.FetchDataTask;
import com.save.coinch.network.Request;
import com.save.coinch.responses.Savings;
import com.save.coinch.responses.Transaction;
import com.save.coinch.responses.Transactions;
import com.save.coinch.responses.User;
import com.save.coinch.utils.CacheUtils;
import com.save.coinch.utils.CommonUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
/**
* Created by Joel_2 on 07/05/2015.
*/
public class TransRecord extends ListActivity implements FetchDataListener {
ListView data;
TransactionsAdapter adapter;
JSONArray json;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.trans_data);
initView();
//data = (ListView) findViewById(R.id.trans_all);
// adapter = new TransactionsAdapter(this);
//data.setAdapter(adapter);
}
private void initView() {
// show progress dialog
String url = CommonUtils.SERVICES.FETCH_TRANSACTIONS;
FetchDataTask task = new FetchDataTask(this);
//ArrayList<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
User user = CacheUtils.getUser(this);
task.setParameters(new BasicNameValuePair("user_id", String.valueOf(user.user_id)));
task.execute(url);
}
@Override
public void onFetchComplete(List<Transaction> data) {
// create new adapter
TransactionsAdapter adapter = new TransactionsAdapter(this, data);
// set the adapter to list
setListAdapter(adapter);
}
@Override
public void onFetchFailure(String msg) {
// show failure message
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
请帮助:)
答案 0 :(得分:0)
此区域存在例外
if (v == null) {
holder = new ViewHolder();
//LayoutInflater li = LayoutInflater.from(getContext());
v = li.inflate(R.layout.trans_item, parent, false);
holder.txtViewAmount = (TextView) convertView.findViewById(R.id.trans_amount);
holder.txtViewDate = (TextView) convertView.findViewById(R.id.trans_date);
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.trans_title);
convertView.setTag(holder);
}
您必须使用v
instaed convertView