Sherlock FragmentActivity选项卡无法将数据填充到Listview中

时间:2013-07-22 13:37:48

标签: android sqlite listview android-fragments actionbarsherlock

我正在学习关于Sherlock ActionBar的开发,我使用Sherlock ActionBar创建了一个示例程序,用于将日期从数据库填充到列表视图。

因为我在一个活动中创建了两个选项卡,我用于输入详细信息的第一个选项卡(fragment1)工作正常,但需要将数据填充到列表视图中的第二个选项卡(fragment2)不起作用细

请查看我的代码:

Fragment2.java

public class Fragment2 extends SherlockFragment implements TabListener {
     private Fragment mFragment;

     DBAdapter db;
     Cursor c;
     ListView list;
     MyAdapter myAdapter;
     View view ;

     Context context;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        //getActivity().setContentView(R.layout.details_listview);

        db = new DBAdapter(getActivity());
        db.openDatabase();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
         view = inflater.inflate(R.layout.details_listview, null);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

         list = (ListView)view.findViewById(R.id.listView1);
          registerForContextMenu(list);
         updateListView();
    }

    private void updateListView() {
        // TODO Auto-generated method stub
        c = db.getAllRecords();
        myAdapter = new MyAdapter();
        list.setAdapter(myAdapter);
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

        mFragment = new Fragment2();        
          ft.add(android.R.id.content, mFragment);
            ft.attach(mFragment);
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        ft.remove(mFragment);
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    class MyAdapter extends BaseAdapter{

        Context context;

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return c.getCount();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public android.view.View getView(int position, android.view.View conview,
                ViewGroup arg2) {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            conview = inflater.inflate(R.layout.customllistview, null);

            TextView t1 = (TextView)conview.findViewById(R.id.idtextView);
            TextView t2 = (TextView)conview.findViewById(R.id.nametextView);
            TextView t3 = (TextView)conview.findViewById(R.id.locationtextView);

            c.moveToPosition(position);

            String id = c.getString(0);
            String name = c.getString(1);
            String location = c.getString(2);

            t1.setText("row id: "+id);
            t2.setText("name :"+name);
            t3.setText("location :"+location);


            return conview;
        }

    }

}

数据库数据库适配器: DBAdapter.java

public class DBAdapter {

    DBHelper dbHelper;
    Context context;
    SQLiteDatabase db;
    public static final int DATABASE_VERSION = 1;

    String DATABASE_NAME = "sampleDB";
    String TABLE_NAME = "sample";

    String COL_ROWID = "rowid";
    String COL_NAME ="name";
    String COL_LOCATION = "location";

    String CREATE_TABLE = "create table sample(rowid integer primary key autoincrement,name text not null,location text not null) ";

    public DBAdapter(Context c) {
        // TODO Auto-generated constructor stub
        this.context = c;
        dbHelper = new DBHelper(context);
    }

    DBAdapter openDatabase() {
        db = dbHelper.getWritableDatabase();
        return this;

    }

    void close() {
        dbHelper.close();
    }

    long insertRecord(String name, String location) {
        ContentValues con = new ContentValues();
        con.put(COL_NAME, name);
        con.put(COL_LOCATION, location);

        return db.insert(TABLE_NAME, null, con);

    }

    Cursor getAllRecords(){
        String[] columns = {COL_ROWID,COL_NAME,COL_LOCATION};
        return db.query(TABLE_NAME, columns, null, null, null, null, null);

    }

    class DBHelper extends SQLiteOpenHelper {

        public DBHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL(CREATE_TABLE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub

        }

    }


}

最后这是我的logcat输出:

07-22 18:54:16.684: E/AndroidRuntime(1204): FATAL EXCEPTION: main
07-22 18:54:16.684: E/AndroidRuntime(1204): java.lang.NullPointerException
07-22 18:54:16.684: E/AndroidRuntime(1204):     at com.example.sherlocklistview.Fragment2$MyAdapter.getView(Fragment2.java:111)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.widget.AbsListView.obtainView(AbsListView.java:1430)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.widget.ListView.onMeasure(ListView.java:1127)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.view.View.measure(View.java:8313)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:701)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:311)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.view.View.measure(View.java:8313)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.view.View.measure(View.java:8313)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.view.View.measure(View.java:8313)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.view.View.measure(View.java:8313)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.view.View.measure(View.java:8313)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.view.View.measure(View.java:8313)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.os.Looper.loop(Looper.java:123)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at android.app.ActivityThread.main(ActivityThread.java:3683)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at java.lang.reflect.Method.invokeNative(Native Method)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at java.lang.reflect.Method.invoke(Method.java:507)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-22 18:54:16.684: E/AndroidRuntime(1204):     at dalvik.system.NativeStart.main(Native Method)

请为我的问题提供一些解决方案

1 个答案:

答案 0 :(得分:0)

请提供有关哪条线正好是文件Fragment2.java中的111行的信息(考虑到导入行号)。
检查Fragment2.java中MyAdapter中的上下文变量,因为它看起来没有在任何地方初始化。