我有一个ListView
的活动和一个自定义适配器。我确信我的mItems
listview适配器不为null。但是,我一直收到NullPointerException
错误。
这是我的主要活动
public class Profile extends AppCompatActivity {
private static String PageType;
ImageView imageToUpload;
private ImageView mImageView;
private String Enc_Pass="";
private List<ListProfileItem> mItems;
private ListProfileItem item;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
URLService = getString(R.string.URLService);
Enc_Pass = getString(R.string.password_encryption);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
SharedPreferences pref = getSharedPreferences("MyPref", MODE_PRIVATE);
strUserId = pref.getString("userId", null);
strPassword = pref.getString("Password", null);
code_comp = pref.getString("CompanyCode",null);
String Status = "";
final ListView lv = (ListView)findViewById(R.id.lvProfile);
getMedicalBalance();
lv.setAdapter(new ListProfileAdapter(this,mItems));
}
public Profile() {
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
menu.add(Menu.NONE, 0, 0, "Save").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return super.onPrepareOptionsMenu(menu);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
final ListView lv = (ListView)findViewById(R.id.lvProfile);
getMedicalBalance();
lv.setAdapter(new ListProfileAdapter(this,mItems));
}
private void getMedicalBalance(){
try{
String encc = "";
try {
// encrypt for parameter
}catch (GeneralSecurityException e){
//handle error
}
RESTClient client = new RESTClient(URLService+"get? method=methodname&value="
+ encc);
client.Execute(RESTClient.RequestMethod.GET);
String response = client.getResponse();
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(response);
mItems = new ArrayList<ListProfileItem>();
String Status = jsonObject.get("Status").toString();
if (Status == "true") {
JSONParser parser = new JSONParser();
JSONArray structure = (JSONArray) parser.parse(dataku);
for (int i = 0; i < structure.size(); i++) {
JSONObject data = (JSONObject) structure.get(i);
item = new ListProfileItem();
item.claimpostamount = data.get("max_limit_per_year").toString();
item.claimpostname = data.get("claim_post_name").toString();
mItems.add(item);
}
// initialize and set the list adapter
}
}
catch(Exception e){
e.printStackTrace();
}
}
这是XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_profile"
android:layout_width="match_parent"
android:tag="tagprofile"
android:layout_height="match_parent"
android:background="#ffffff">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/imageToUpload"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_menu_camera"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Profile Detail"
android:id="@+id/tvProfileDetail"
android:layout_marginTop="30dp" />
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lvProfile"
android:choiceMode="singleChoice"
android:nestedScrollingEnabled="false"
android:longClickable="true"
android:paddingTop="0dp"
android:layout_marginTop="0dp"
android:headerDividersEnabled="false"
android:divider="@null"
android:dividerHeight="0dp"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
这是我的自定义列表视图
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@color/white">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="0.8"
android:orientation="vertical"
android:layout_height="wrap_content">
<!--All textViews here-->
<TextView
android:layout_width="150dp"
android:paddingTop="15dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Type"
android:textSize="12dp"
android:fontFamily="sans-serif"
android:textColor="@color/black"
android:id="@+id/tvJenisBalance"
android:width="130dp" />
<TextView
android:layout_width="150dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Date"
android:fontFamily="sans-serif"
android:id="@+id/tvAmountBalance"
android:textSize="15dp"
android:width="130dp" />
</LinearLayout>
</RelativeLayout>
这是我的listviewadapter
public class ListProfileAdapter extends ArrayAdapter<ListProfileItem> {
public ListProfileAdapter(Context context, List<ListProfileItem> items)
{
super(context, R.layout.style_fragment_list_profile, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null) {
// inflate the GridView item layout
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.style_fragment_list_profile, parent, false);
// initialize the view holder
viewHolder = new ViewHolder();
viewHolder.tvclaimpostamount = (TextView) convertView.findViewById(R.id.tvAmountBalance);
viewHolder.tvclaimpostname = (TextView) convertView.findViewById(R.id.tvJenisBalance);
convertView.setTag(viewHolder);
} else {
// recycle the already inflated view
viewHolder = (ViewHolder) convertView.getTag();
}
// update the item view
ListProfileItem item = getItem(position);
viewHolder.tvclaimpostname.setText(item.claimpostname);
viewHolder.tvclaimpostamount.setText(item.claimpostamount);
return convertView;
}
private static class ViewHolder {
TextView tvclaimpostname;
TextView tvclaimpostamount;
}
}
任何帮助都会受到赞赏。