从数据库中检索数据的代码在扩展Activity的类中运行良好,但是当我将它放在需要插入列表元素的类中时,应用程序崩溃,此类扩展了Fragment。
package com.autohush.www.dah;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivityFragment2 extends Fragment {
Context ctx = this.getActivity();
private List<TimeList> TDList = new ArrayList<TimeList>();
public MainActivityFragment2() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
populateLocationList();
}
private void populateLocationList() {
DBoperations db = new DBoperations(ctx);
Cursor cr = db.getInformation(db);
cr.moveToFirst();
String TimeH, TimeM;
do {
TimeH = Integer.toString(cr.getInt(1));
TimeM = Integer.toString(cr.getInt(2));
TDList.add((new TimeList(TimeH + ":" + TimeM,"1 hr")));
}while (cr.moveToNext());
}
private class MyAdapter extends ArrayAdapter<TimeList> {
public MyAdapter(Context context, List<TimeList> values) {
super(context, R.layout.time_list_item,values);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
LayoutInflater inflater = LayoutInflater.from(getContext());
if( v == null){
v = inflater.inflate(R.layout.time_list_item, parent, false);
}
TimeList currLoc = TDList.get(position);
TextView theTextView = (TextView) v.findViewById(R.id.time_list_item_textview1);
theTextView.setText(currLoc.getTime());
theTextView = (TextView) v.findViewById(R.id.time_list_item_textview2);
theTextView.setText(currLoc.getDuration());
return v;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.time_list_view, container, false);
ListView list = (ListView) rootView.findViewById(R.id.time_list_view);
ArrayAdapter<TimeList> adapter = new MyAdapter(getActivity(),TDList);
list.setAdapter(adapter);
setRetainInstance(true);
ImageButton ib = (ImageButton) rootView.findViewById(R.id.imageButton1);
ib.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivityFragment2.this.getActivity(),TimePickerPopup.class));
}
});
return rootView;
}
这是我的崩溃日志...我是android新手。请帮忙。
03-10 18:03:04.142 20856-20856/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.autohush.www.dah, PID: 20856
java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
at com.autohush.www.dah.DBoperations.getInformation(DBoperations.java:47)
at com.autohush.www.dah.MainActivityFragment2.populateLocationList(MainActivityFragment2.java:42)
at com.autohush.www.dah.MainActivityFragment2.onCreate(MainActivityFragment2.java:37)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1951)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1029)
at android.support.v4.app.BackStackRecord.ensureFragmentsAreInitialized(BackStackRecord.java:1077)
at android.support.v4.app.BackStackRecord.beginTransition(BackStackRecord.java:1032)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:658)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1545)
at android.view.View.measure(View.java:17442)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:664)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:731)
at android.view.View.measure(View.java:17442)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:17442)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17442)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17442)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17442)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
at android.view.View.measure(View.java:17442)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEve
答案 0 :(得分:0)
我找到了它崩溃的原因。 Context ctx = this.getActivity();不应该在开始,因为它否则返回一个空值。 它应该在onCreate()中。