简体   繁体   English

NPE的片段活动

[英]Fragment Activity with a NPE

Hi I'm trying to create my first fragment activities and thnx for the help from Natali (Cont from this thread) . 嗨,我正在尝试创建我的第一个片段活动和thnx,以获取Natali的帮助(此线程续) I convertied a class that worked fine into a fragmentand I've made it to this point but now I have the infamous NPE that I cant resolve. 我将一个可以正常工作的类转换成一个片段,到现在为止,但是现在有了臭名昭著的NPE,我无法解决。

It looks like its from my list-fragment class but I cant figure it out. 它看起来像来自我的列表片段类,但是我无法弄清楚。 I'm sure its an easy fix..(?) and I also replaced some variables with hard-code to make it a little easier to run. 我确信它很容易修复..(?),我还用硬代码替换了一些变量,使其运行起来更容易一些。

Below is a copy of my LogCat and the activities. 以下是我的LogCat和活动的副本。 Thnx for your help in advance!! 谢谢您的帮助!

EDIT: The correct log file: LogCat: 编辑: 正确的日志文件: 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

The MainActivity Class: 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;
        }
    }

    }
Caused by: java.lang.IllegalArgumentException: column '_id' does not exist

Means you don't have a column named _id in you table. 意味着您的表中没有名为_id的列。

See this link . 看到这个链接

"Adapter that exposes data from a Cursor to a ListView widget. The Cursor must include a column named "_id" or this class will not work." “将数据从游标公开到ListView小部件的适配器。游标必须包含名为“ _id”的列,否则此类将无法工作。”

EDIT: 编辑:

Looks like the query may be invalid. 看起来查询可能无效。 I think what you want is: 我认为您想要的是:

Cursor c = db.rawQuery(QUERY_KEY + " ORDER BY `_id` " + QUERY_ORDER, null);

If you still have issues add more info to log: 如果仍然有问题,请添加更多信息进行记录:

Log.e("QUERY", q + " ORDER BY `_id` " + qO);

EDIT: 编辑:

Why are you doing this q = extras.getString(QUERY_KEY); 您为什么要这样做q = extras.getString(QUERY_KEY); Is the query being pass from another activity? 查询是从另一个活动传递的吗? Seem like you want loadQuery(QUERY_KEY); 好像你想要loadQuery(QUERY_KEY);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM