您好我正在尝试创建我的第一个片段活动和thnx以获得Natali (Cont from this thread)的帮助。我将一个工作正常的类转换成了一个片段,我已经达到了这一点,但现在我已经拥有了我无法解决的臭名昭着的NPE。
它看起来像我的list-fragment类,但我无法弄明白。我确定这是一个简单的修复..(?),我还用硬编码替换了一些变量,使其更容易运行。
以下是我的LogCat和活动的副本。 Thnx提前帮助你!!
编辑: 正确的日志文件: logcat的:
02-03 10:21:14.410: E/AndroidRuntime(29792): FATAL EXCEPTION: main
02-03 10:21:14.410: E/AndroidRuntime(29792): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.andaero.test/com.andaero.test.fragments.MainActivity}: java.lang.NullPointerException
02-03 10:21:14.410: E/AndroidRuntime(29792): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
02-03 10:21:14.410: E/AndroidRuntime(29792): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
02-03 10:21:14.410: E/AndroidRuntime(29792): at android.app.ActivityThread.access$500(ActivityThread.java:122)
02-03 10:21:14.410: E/AndroidRuntime(29792): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
02-03 10:21:14.410: E/AndroidRuntime(29792): at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 10:21:14.410: E/AndroidRuntime(29792): at android.os.Looper.loop(Looper.java:132)
02-03 10:21:14.410: E/AndroidRuntime(29792): at android.app.ActivityThread.main(ActivityThread.java:4123)
02-03 10:21:14.410: E/AndroidRuntime(29792): at java.lang.reflect.Method.invokeNative(Native Method)
02-03 10:21:14.410: E/AndroidRuntime(29792): at java.lang.reflect.Method.invoke(Method.java:491)
02-03 10:21:14.410: E/AndroidRuntime(29792): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-03 10:21:14.410: E/AndroidRuntime(29792): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-03 10:21:14.410: E/AndroidRuntime(29792): at dalvik.system.NativeStart.main(Native Method)
02-03 10:21:14.410: E/AndroidRuntime(29792): Caused by: java.lang.NullPointerException
02-03 10:21:14.410: E/AndroidRuntime(29792): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:99)
02-03 10:21:14.410: E/AndroidRuntime(29792): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
02-03 10:21:14.410: E/AndroidRuntime(29792): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
02-03 10:21:14.410: E/AndroidRuntime(29792): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
02-03 10:21:14.410: E/AndroidRuntime(29792): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1539)
02-03 10:21:14.410: E/AndroidRuntime(29792): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1508)
02-03 10:21:14.410: E/AndroidRuntime(29792): at com.andaero.test.fragments.ListFragment.loadQuery(ListFragment.java:72)
02-03 10:21:14.410: E/AndroidRuntime(29792): at com.andaero.test.fragments.ListFragment.onActivityCreated(ListFragment.java:60)
02-03 10:21:14.410: E/AndroidRuntime(29792): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:795)
02-03 10:21:14.410: E/AndroidRuntime(29792): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:977)
02-03 10:21:14.410: E/AndroidRuntime(29792): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:960)
02-03 10:21:14.410: E/AndroidRuntime(29792): at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1674)
02-03 10:21:14.410: E/AndroidRuntime(29792): at android.app.Activity.performCreate(Activity.java:4400)
02-03 10:21:14.410: E/AndroidRuntime(29792): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
02-03 10:21:14.410: E/AndroidRuntime(29792): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
02-03 10:21:14.410: E/AndroidRuntime(29792): ... 11 more
MainActivity类:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
The ListFragment Class:
public class ListFragment extends android.app.ListFragment {
boolean mDualPane;
int mCurCheckPosition = 0;
protected TextView activityTitle;
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String extStorageDirectory = Environment.getExternalStorageDirectory()
.toString();
File dbfile = new File(extStorageDirectory + "/myApp/dB/myApp.db");
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
private static final String QUERY_KEY = "SELECT * FROM Regulatory_List";
private static final String QUERY_ORDER = "ASC";
private View layout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
layout = inflater.inflate(R.layout.listview, null);
return layout;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Bundle extras = getActivity().getIntent().getExtras();
String q = null;
if (extras != null) {
q = extras.getString(QUERY_KEY);
}
loadQuery(q);
}
public void loadQuery(String q) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
String qO = getActivity().getIntent().getStringExtra("QUERY_ORDER");
Cursor c = db.rawQuery(q + " ORDER BY `_id` " + qO, null);
setListAdapter(new QueryAdapter(getActivity(), c));
db.close();
} else {
Alerts.sdCardMissing(getActivity());
}
}
public class QueryAdapter extends CursorAdapter {
public QueryAdapter(Context context, Cursor c) {
super(context, c);
LayoutInflater.from(context);
}
@Override
public void bindView(View v, Context context, final Cursor c) {
int tvLabel = c.getColumnIndexOrThrow("label");
String label = c.getString(tvLabel);
final TextView labelTxt = (TextView) v.findViewById(R.id.label);
if (labelTxt != null) {
labelTxt.setText("(" + label + ")");
}
int tvTitle = c.getColumnIndexOrThrow("title");
final String title = c.getString(tvTitle);
TextView titleTxt = (TextView) v.findViewById(R.id.listTitle);
if (titleTxt != null) {
titleTxt.setText(title);
}
int tvDescription = c.getColumnIndexOrThrow("description");
String description = c.getString(tvDescription);
TextView descriptionTxt = (TextView) v.findViewById(R.id.caption);
if (descriptionTxt != null) {
descriptionTxt.setText(description);
}
int tvDate = c.getColumnIndexOrThrow("date");
String date = c.getString(tvDate);
TextView dateTxt = (TextView) v.findViewById(R.id.dateAdded);
if (dateTxt != null) {
dateTxt.setText(date);
}
int tvGoto = c.getColumnIndexOrThrow("gotoURL");
final String gotoURL = c.getString(tvGoto);
TextView gotoTxt = (TextView) v.findViewById(R.id.dummy);
if (gotoTxt != null) {
gotoTxt.setText(gotoURL);
}
gotoTxt.setVisibility(View.GONE);
v.setTag(gotoURL);
final ListView lv = getListView();
lv.setEnabled(true);
lv.setClickable(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int arg2,
long arg3) {
String url = "";
url = (String) v.getTag();
int nI = c.getColumnIndexOrThrow("intent");
String intent = c.getString(nI);
Class<?> myIntent = null;
try {
myIntent = Class.forName("com.andaero.test.fragments"
+ intent);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int tvTitle = c.getColumnIndexOrThrow("title");
String title = c.getString(tvTitle);
int tvLabel = c.getColumnIndexOrThrow("label");
String label = c.getString(tvLabel);
String queryKey = "SELECT * FROM " + label;
c.close();
db.close();
Intent i = new Intent();
i.putExtra("QUERY_KEY", queryKey);
i.putExtra("KEY_URL", url);
i.putExtra("KEY_SUBTITLE", title);
i.putExtra("KEY_LABEL", label);
i.putExtra("KEY_INTENT", intent);
i.putExtra("QUERY_ORDER", "ASC");
i.putExtra("KEY_YPOS", "0.0");
startActivity(i);
}
});
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View v = LayoutInflater.from(context).inflate(
R.layout.list_item, parent, false);
return v;
}
}
}
答案 0 :(得分:0)
Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
表示您在表格中没有名为_id的列。
请参阅此link。
&#34;将数据从Cursor公开给ListView小部件的适配器。游标必须包含名为&#34; _id&#34;的列。或者这个课程不起作用。&#34;
编辑:
看起来查询可能无效。我想你想要的是:
Cursor c = db.rawQuery(QUERY_KEY + " ORDER BY `_id` " + QUERY_ORDER, null);
如果仍有问题,请在日志中添加更多信息:
Log.e("QUERY", q + " ORDER BY `_id` " + qO);
编辑:
你为什么这样做q = extras.getString(QUERY_KEY);
查询是从另一个活动传递的?好像你想要loadQuery(QUERY_KEY);