-1
我有我想要显示ListView
NullPointerException异常错误
这里是我的代码的一部分
public class ExpensesDaily extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
lvExpenses = (ListView) getActivity().findViewById(android.R.id.list);
Cursor expensesListCursor;
DbControl dbc = new DbControl(getActivity());
try {
dbc.open();
expensesListCursor = dbc.listExpenses();
String[] from = new String[] {"description", "cost" };
ListAdapter adapter = new SimpleCursorAdapter(getActivity(), R.layout.list_expenses, expensesListCursor, from, new int[] { R.id.tvDescription, R.id.tvCost },0);
lvExpenses.setAdapter(adapter);
}catch (SQLException e){
}
dbc.close();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.expenses_daily, container, false);
return rootView;
}
}
** DbControl类**片段
public class DbControl {
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
public DbControl(Context context) {
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public Cursor listExpenses(){
Cursor cursor;
cursor = database.query(MySQLiteHelper.TABLE_EXPENSES, new String[] {"id _id", "description, cost"}, "forDate='" + date + "'", null, null, null, "id asc");
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
expense_daily.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ll_expensesDaily"
android:baselineAligned="false">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
</LinearLayout>
list_expenses.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="7dip">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:id="@+id/tvDescription" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:id="@+id/tvCost" />
</LinearLayout>
</LinearLayout>
但每次我加载片段,将显示这个错误
FATAL EXCEPTION: main java.lang.NullPointerException at com.code.imin.mymoneymanaged.ExpensesDaily.onCreate(ExpensesDaily.java:60)
它指向线
lvExpenses.setAdapter(adapter) in class ExpensesDaily.
你要调用onCreateView()您的浏览声明(使用rootView.findViewById(...))或rootView保存为一个类范围的变量和onActivityCreated声明它们( )。 – wblaschko 2014-10-10 17:18:54
'getActivity()。findViewById(android.R.id.list)'可以在'onActivityCreated'上安全地调用,并且它会返回视图。无需将“rootView”保存为字段。 – 2014-10-10 17:20:37
@PedroOliveira不知道为什么,但似乎我需要使用rootView.findViewById(...),并不能只使用getActivity()。findViewById(),因为这会使我的应用程序崩溃与空指针 – imin 2014-10-13 18:52:03