使用简单的BaseAdapter无法显示ListView

时间:2014-01-02 04:40:05

标签: android listview android-viewpager

我已经尝试了所有选项并且坚持了两天。这是代码。 Plz的帮助。 当我运行代码时,lvContacts处理程序是自我null。即使我解决了这个问题,它也不会显示listview

MainActivity

public class MainActivity extends FragmentActivity {
    Cursor mCursor=null;
    Adapter adContact=null;
    ListView lvContacts;
private static final int TLIST_LOADER = 0x01;
private PAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.i("test", "Yes");
        setContentView(R.layout.view_pager_layout);
        lvContacts=(ListView)findViewById(R.id.lvContacts);
         String[] projection= { ContactsContract.Contacts._ID,
                    ContactsContract.Contacts.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone.NUMBER

            };

            mCursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null, null, null);
            mCursor.moveToFirst();
            int phoneNumberIndex = mCursor
                    .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);
            ArrayList alContacts=new ArrayList();


            do {
                alContacts.add(mCursor.getString(1)+ "  "+ mCursor.getString(2));
            }while(mCursor.moveToNext());
            adContact=new Adapter(alContacts, this);
        initialisePaging();
        Log.i("test", "Yes");
        lvContacts.setAdapter(adContact); 

    }

    private void initialisePaging() {
        // TODO Auto-generated method stub
        List<Fragment> fragment=new Vector<Fragment>();
        fragment.add(Fragment.instantiate(this, FragArrList.class.getName()));
        fragment.add(Fragment.instantiate(this, FragDialog.class.getName()));
        adapter=new PAdapter(this.getSupportFragmentManager(),fragment);    
        ViewPager pager=(ViewPager)findViewById(R.id.viewPager);
        pager.setAdapter(adapter);
        Log.i("test", adapter.getCount()+"");
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

Adapter.java

    public class Adapter extends BaseAdapter {
    Context mCtx=null;
    ArrayList mData=null;

    static int i=0;
    Adapter(ArrayList data, Context ctx) {
        mData=data;
    mCtx=ctx;       
}

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

    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        if(convertView==null) {
            LayoutInflater inflator=((FragmentActivity)mCtx).getLayoutInflater();
            convertView=inflator.inflate(R.layout.row,null);

        }
         TextView tv=(TextView)convertView.findViewById(R.id.textView1);

        String name=mData.get(position).toString();
        tv.setText(name);
        return convertView;

    }

}

logcat的

    01-04 23:32:23.309: E/AndroidRuntime(3534): FATAL EXCEPTION: main
01-04 23:32:23.309: E/AndroidRuntime(3534): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.contatcsnew/com.example.contatcsnew.MainActivity}: java.lang.NullPointerException
01-04 23:32:23.309: E/AndroidRuntime(3534):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2083)
01-04 23:32:23.309: E/AndroidRuntime(3534):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2108)
01-04 23:32:23.309: E/AndroidRuntime(3534):     at android.app.ActivityThread.access$600(ActivityThread.java:138)
01-04 23:32:23.309: E/AndroidRuntime(3534):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1204)
01-04 23:32:23.309: E/AndroidRuntime(3534):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 23:32:23.309: E/AndroidRuntime(3534):     at android.os.Looper.loop(Looper.java:137)
01-04 23:32:23.309: E/AndroidRuntime(3534):     at android.app.ActivityThread.main(ActivityThread.java:4904)
01-04 23:32:23.309: E/AndroidRuntime(3534):     at java.lang.reflect.Method.invokeNative(Native Method)
01-04 23:32:23.309: E/AndroidRuntime(3534):     at java.lang.reflect.Method.invoke(Method.java:511)
01-04 23:32:23.309: E/AndroidRuntime(3534):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
01-04 23:32:23.309: E/AndroidRuntime(3534):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
01-04 23:32:23.309: E/AndroidRuntime(3534):     at dalvik.system.NativeStart.main(Native Method)
01-04 23:32:23.309: E/AndroidRuntime(3534): Caused by: java.lang.NullPointerException
01-04 23:32:23.309: E/AndroidRuntime(3534):     at com.example.contatcsnew.MainActivity.onCreate(MainActivity.java:59)
01-04 23:32:23.309: E/AndroidRuntime(3534):     at android.app.Activity.performCreate(Activity.java:5248)
01-04 23:32:23.309: E/AndroidRuntime(3534):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
01-04 23:32:23.309: E/AndroidRuntime(3534):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2047)
01-04 23:32:23.309: E/AndroidRuntime(3534):     ... 11 more

// view_pager_layout.xml
         

<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

2 个答案:

答案 0 :(得分:0)

您的view_pager_layout.xml没有列表视图,因此findViewById(R.id.lvContacts)返回null。

<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

您必须在其中一个pageritems中拥有此列表。

如果您有views作为寻呼机项目,则需要等待initializeItem被调用才能访问listview。最好在PagerAdaper中获取列表视图的引用。

如果您有片段作为寻呼机项目获取片段中列表的引用并在其中设置适配器。

答案 1 :(得分:0)

查看我的评论。

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

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