我创建了一个用法适配器来绑定我的项目。但是,我不知道为什么这个错误(尝试在空对象引用上调用虚拟方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)')显示我运行应用程序时。
我的适配器
package com.example.redir.wealthtrack;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import com.example.redir.wealthtrack.model.Receipt;
import com.example.redir.wealthtrack.model.Usage;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
/**
* Created by User on 2/3/2016.
*/
public class UsageAdapter extends ArrayAdapter<Usage> {
private static final String TAG = "Usage" ;
ArrayList<Usage> list;
Context context;
public static final String LOGTAG = "EXPLORECE";
public UsageAdapter(Context context, int resource, ArrayList<Usage> lists){
super(context, resource, lists);
this.context = context;
this.list = lists;
}
public View getView(int position, View convertView, ViewGroup parent) {
Usage myusage = list.get(position);
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.usage_item, null);
return view;
}
}
我的片段
package com.example.redir.wealthtrack;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Toast;
import com.example.redir.wealthtrack.db.ReceiptsDataSource;
import com.example.redir.wealthtrack.db.UsageDataSource;
import com.example.redir.wealthtrack.model.Receipt;
import com.example.redir.wealthtrack.model.Usage;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class UsageFragment extends Fragment {
public UsageDataSource Usagedatasource;
ArrayAdapter<Usage> UsageAdapter;
ListView listView;
List<Usage> usages;
Usage selectedUsage;
public UsageFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showToast(getActivity().getApplicationContext(), "Usage's history");
((MainActivity) getActivity()).setActionBarTitle("View Usage");
Usagedatasource = new UsageDataSource(getActivity().getApplicationContext());
Usagedatasource.open();
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_receipts, container, false);
usages = (List<Usage>) Usagedatasource.getAll();
UsageAdapter = new UsageAdapter(getActivity().getApplicationContext(),0, (ArrayList<Usage>) usages);
listView = (ListView) view.findViewById(R.id.listView3);
listView.setAdapter(UsageAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectedUsage = usages.get(position);
AlertDialog.Builder a_builder = new AlertDialog.Builder(getActivity());
a_builder.setMessage("Receipt/Bill captured on: " + selectedUsage.getUsagedate())
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alert = a_builder.create();
alert.setTitle(Html.fromHtml("<font color='#08ae9e'>Information</font>"));
alert.show();
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
selectedUsage = usages.get(position);
AlertDialog.Builder a_builder = new AlertDialog.Builder(getActivity());
a_builder.setMessage("Are you sure you want to delete?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
deleteItem(selectedUsage);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = a_builder.create();
alert.setTitle(Html.fromHtml("<font color='#08ae9e'>Alert!!</font>"));
alert.show();
return true;
}
});
return view;
}
public static void showToast(Context context, String text) {
Toast.makeText(context, text, Toast.LENGTH_LONG).show();
}
private void deleteItem(Usage selectedUsage) {
Usagedatasource.removeUsage(selectedUsage);
showToast(getActivity().getApplicationContext(), "Usage deleted");
UsageAdapter.notifyDataSetChanged();
UsageAdapter.notifyDataSetInvalidated();
refreshDisplay();
}
private void refreshDisplay() {
usages = Usagedatasource.getAll();
UsageAdapter.clear();
UsageAdapter.addAll(usages);
}
}
我的项目
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView3"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView4"
android:layout_centerVertical="true"
android:layout_alignStart="@+id/textView3" />
</RelativeLayout>
//收据片段
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ReceiptFragment"
android:weightSum="1">
<!-- TODO: Update blank fragment layout -->
<GridView
android:layout_width="wrap_content"
android:layout_height="503dp"
android:id="@+id/gridView"
android:numColumns="auto_fit"
android:horizontalSpacing="5dp"
android:columnWidth="120dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="70dp"
android:padding="8dp"
/>
</LinearLayout>
//我的使用数据库(getALL方法)
public List<Usage> getAll() {
List<Usage> usages = new ArrayList<Usage>();
Cursor cursor = database.query(WealthDBOpenHelper.TABLE_USAGE, allColumns,
null, null, null, null, null);
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
Usage usage = new Usage();
usage.setUsagedate(cursor.getString(cursor.getColumnIndex(WealthDBOpenHelper.COLUMN_USAGE_DATE)));
usage.setAmount(cursor.getDouble(cursor.getColumnIndex(WealthDBOpenHelper.COLUMN_AMOUNT)));
usage.setCategory(cursor.getString(cursor.getColumnIndex(WealthDBOpenHelper.COLUMN_CATEGORY)));
usages.add(usage);
}
}
return usages;
}
答案 0 :(得分:1)
您使用的是GridView
而不是ListView
,您使用的ID也是错误的。
像这样改变
GridView gridView;
gridView = (GridView) view.findViewById(R.id.gridView);
gridView.setAdapter(UsageAdapter);
在xml中更改
tools:context=".UsageFragment"