导航抽屉的列表视图,给出空指针错误

时间:2014-01-26 08:41:02

标签: android android-layout nullpointerexception actionbarsherlock navigation-drawer

我收到空指针异常错误,我无法弄清楚错误在哪里。任何人都可以指出吗?该布局具有视图寻呼机,导航抽屉和操作栏sherlock中的选项卡。一切都工作,直到我改为使用自定义布局抽屉从库存android布局(仅包含textview的简单列表)。

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userpage);

    //Getting reference to the DrawerLayout
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    mDrawerList = (ListView) findViewById(R.id.drawer_list);
    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

    sqlHandler_nav_drawer = new SqlHandler_nav_drawer(getBaseContext());
    contactList = new ArrayList<ContactListItems>(); 
    contactList.clear(); 

    Calendar c = Calendar.getInstance();
    SimpleDateFormat dateformat = new SimpleDateFormat("dd-MM-yyyy");
    SimpleDateFormat timeformat = new SimpleDateFormat("HH:mm:ss");
    String date = dateformat.format(c.getTime());
    String time = timeformat.format(c.getTime());

    Log.d("MSG", "HERE HERE");
    String s = "Notification";
    String query2 = "INSERT INTO tbl_nickname(time,msg,date) values ('"+ time + "','" + s + "','" + date + "')"; 
    sqlHandler_nav_drawer.executeQuery(query2);
    sqlHandler_nav_drawer.sqlDatabase.close();

    String query = "SELECT * FROM tbl_nickname"; 
    Cursor c1 = sqlHandler_nav_drawer.selectQuery(query); 

    if (c1 != null && c1.getCount() != 0) { 
        if (c1.moveToFirst()) { 
            do { 
                ContactListItems contactListItems = new ContactListItems(); 

                contactListItems.setmsg(c1.getString(c1.getColumnIndex("msg"))); 

                contactList.add(contactListItems); 

            } while (c1.moveToNext()); 
        }  
    }   
        c1.close(); 
        contactListAdapter_nav_drawer = new ContactListAdapter_nav_drawer(getBaseContext(), contactList); 
        mDrawerList.setAdapter(contactListAdapter_nav_drawer);

我的适配器:

public class ContactListAdapter_nav_drawer extends BaseAdapter { 

Context context; 
ArrayList<ContactListItems> contactList; 

public ContactListAdapter_nav_drawer(Context context, ArrayList<ContactListItems> list) { 
this.context = context; 
contactList = list; 
} 

@Override
public int getCount() { 
return contactList.size(); 
} 

@Override
public Object getItem(int position) { 
return contactList.get(position); 
} 

@Override
public long getItemId(int position) { 
return position; 
} 

@Override
public View getView(int position, View convertView, ViewGroup arg2) { 

ContactListItems contactListItems = contactList.get(position); 

if (convertView == null) { 
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    convertView = inflater.inflate(R.layout.listview_nav_drawer_inflate, null); 
} 

//TextView tv_time = (TextView) convertView.findViewById(R.id.tv_time); 
//tv_time.setText(contactListItems.gettime()); 

TextView tv_msg = (TextView) convertView.findViewById(R.id.msg_nav_drawer); 
tv_msg.setText(contactListItems.getmsg()); 

//TextView tv_date = (TextView) convertView.findViewById(R.id.tv_date); 
//tv_date.setText(contactListItems.getdate()); 
return convertView; 

} 

}

listview_nav_drawer_inflate.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/msg_nav_drawer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

</LinearLayout>

logcat的:

01-26 16:31:05.208: E/AndroidRuntime(21731): FATAL EXCEPTION: main
01-26 16:31:05.208: E/AndroidRuntime(21731): java.lang.NullPointerException
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:392)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.widget.AbsListView.obtainView(AbsListView.java:2040)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.widget.ListView.makeAndAddView(ListView.java:1772)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.widget.ListView.fillDown(ListView.java:672)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.widget.ListView.fillFromTop(ListView.java:732)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.widget.ListView.layoutChildren(ListView.java:1625)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.widget.AbsListView.onLayout(AbsListView.java:1870)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.view.View.layout(View.java:11418)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:930)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.view.View.layout(View.java:11418)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.widget.FrameLayout.onLayout(FrameLayout.java:443)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.view.View.layout(View.java:11418)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1589)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.view.View.layout(View.java:11418)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:690)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.view.View.layout(View.java:11418)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.widget.FrameLayout.onLayout(FrameLayout.java:443)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.view.View.layout(View.java:11418)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1652)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1510)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1415)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.view.View.layout(View.java:11418)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.widget.FrameLayout.onLayout(FrameLayout.java:443)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.view.View.layout(View.java:11418)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.os.Looper.loop(Looper.java:137)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at android.app.ActivityThread.main(ActivityThread.java:4441)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at java.lang.reflect.Method.invokeNative(Native Method)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at java.lang.reflect.Method.invoke(Method.java:511)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-26 16:31:05.208: E/AndroidRuntime(21731):    at dalvik.system.NativeStart.main(Native Method)
01-26 16:31:06.988: I/Process(21731): Sending signal. PID: 21731 SIG: 9

2 个答案:

答案 0 :(得分:0)

您应该考虑使用ViewHolder。试试下面的

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

@Override
public View getView(int position, View convertView, ViewGroup arg2) { 

ContactListItems contactListItems = contactList.get(position); 
ViewHolder holder;
if (convertView == null) { 
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    convertView = inflater.inflate(R.layout.listview_nav_drawer_inflate,arg2,false); 
    holder= new ViewHolder();
    holder.tv_msg = (TextView) convertView.findViewById(R.id.msg_nav_drawer); 
    convertView.setTag(holder);  
}else{
     holder= (ViewHolder)convertView.getTag(); 
}  
tv_msg.setText(contactListItems.getmsg()); 
return convertView; 

} 
static class ViewHolder
{
     TextView tv_msg;
}

答案 1 :(得分:0)

当你的数据来自sqlite db时,不要使用BaseAdapter,而是使用SimpleCursorAdapter