我正在使用带有自定义适配器的列表视图。我得到一个空指针异常,其原因未知。我甚至试图调试它但调试器无法找到异常的位置。这是代码:
if(SplashActivity.s_contactNameArrayListSplash != null && SplashActivity.s_contactNameArrayListSplash.size() != 0)
{
if(m_contactAdapterSetTheRule == null)
{
m_contactAdapterSetTheRule = new ContactAdapterSetRule(ShowTheContacts1.this, R.layout.showthecontacts, SplashActivity.s_contactNameArrayListSplash);
//add the footer before adding the adapter, else the footer will not load!
// View footerView = ((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listfooter, null, false);
View footerView = getLayoutInflater().inflate(R.layout.listfooter, null);
m_lvShowContactsSetTheRule.addFooterView(footerView);
m_lvShowContactsSetTheRule.setAdapter(m_contactAdapterUpdatingRule);
}
m_lvShowContactsSetTheRule.setOnScrollListener(new OnScrollListener(){
//useless here, skip!
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
//what is the bottom item that is visible
int lastInScreen = firstVisibleItem + visibleItemCount;
//is the bottom item visible & not loading more already ? Load more !
if((lastInScreen == totalItemCount) && !(m_loadingMore)){
Thread thread = new Thread(null, loadMoreListItems);
thread.start();
}
}
});
//Load the first 25 items
Thread thread = new Thread(null, loadMoreListItems);
thread.start();
}
else
{
Log.e("Info: ","No any contact present in the add Rule Array list");
Toast.makeText(ShowTheContacts1.this,"No Contact is present in the contact list",Toast.LENGTH_LONG).show();
Intent intent = new Intent(ShowTheContacts1.this,Settings.class);
startActivity(intent);
}
}
catch(Exception e)
{
Log.e("Exception: ",e+" in setTheRuleContactListMaker() of ShowTheContacts1.java");
}
}
//Runnable to load the items
Runnable loadMoreListItems = new Runnable() {
@Override
public void run() {
//Set flag so we cant load new items 2 at the same time
m_loadingMore = true;
//Reset the array that holds the new items
m_myListItems = new ArrayList<String>();
if(SplashActivity.s_contactNameArrayListSplash.size() < m_itemsPerPage)
m_itemsPerPage = SplashActivity.s_contactNameArrayListSplash.size();
//Get 15 new listitems
for (int i = 0; i < m_itemsPerPage; i++) {
//Fill the item with some bogus information
m_myListItems.add(SplashActivity.s_contactNameArrayListSplash.get(i).toString());
}
//Done! now continue on the UI thread
runOnUiThread(returnRes);
}
};
//Since we cant update our UI from a thread this Runnable takes care of that!
Runnable returnRes = new Runnable()
{
@Override
public void run()
{
//Loop thru the new items and add them to the adapter
if(m_myListItems != null && m_myListItems.size() > 0)
{
for(int i=0;i < m_myListItems.size();i++)
{
m_contactAdapterSetTheRule.add(m_myListItems.get(i));
}
}
//Tell to the adapter that changes have been made, this will cause the list to refresh
m_contactAdapterSetTheRule.notifyDataSetChanged();
//Done loading more.
m_loadingMore = false;
}
};
这是错误日志:
12-11 15:12:28.535: E/AndroidRuntime(794): Uncaught handler: thread main exiting due to uncaught exception
12-11 15:12:28.545: E/AndroidRuntime(794): java.lang.NullPointerException
12-11 15:12:28.545: E/AndroidRuntime(794): at android.widget.ListView.setupChild(ListView.java:1693)
12-11 15:12:28.545: E/AndroidRuntime(794): at android.widget.ListView.makeAndAddView(ListView.java:1671)
12-11 15:12:28.545: E/AndroidRuntime(794): at android.widget.ListView.fillDown(ListView.java:637)
12-11 15:12:28.545: E/AndroidRuntime(794): at android.widget.ListView.fillFromTop(ListView.java:694)
12-11 15:12:28.545: E/AndroidRuntime(794): at android.widget.ListView.layoutChildren(ListView.java:1521)
12-11 15:12:28.545: E/AndroidRuntime(794): at android.widget.AbsListView.onLayout(AbsListView.java:1113)
12-11 15:12:28.545: E/AndroidRuntime(794): at android.view.View.layout(View.java:6830)
12-11 15:12:28.545: E/AndroidRuntime(794): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
12-11 15:12:28.545: E/AndroidRuntime(794): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
12-11 15:12:28.545: E/AndroidRuntime(794): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
12-11 15:12:28.545: E/AndroidRuntime(794): at android.view.View.layout(View.java:6830)
12-11 15:12:28.545: E/AndroidRuntime(794): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
12-11 15:12:28.545: E/AndroidRuntime(794): at android.view.View.layout(View.java:6830)
12-11 15:12:28.545: E/AndroidRuntime(794): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
12-11 15:12:28.545: E/AndroidRuntime(794): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
12-11 15:12:28.545: E/AndroidRuntime(794): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
12-11 15:12:28.545: E/AndroidRuntime(794): at android.view.View.layout(View.java:6830)
12-11 15:12:28.545: E/AndroidRuntime(794): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
12-11 15:12:28.545: E/AndroidRuntime(794): at android.view.View.layout(View.java:6830)
12-11 15:12:28.545: E/AndroidRuntime(794): at android.view.ViewRoot.performTraversals(ViewRoot.java:996)
12-11 15:12:28.545: E/AndroidRuntime(794): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
12-11 15:12:28.545: E/AndroidRuntime(794): at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 15:12:28.545: E/AndroidRuntime(794): at android.os.Looper.loop(Looper.java:123)
12-11 15:12:28.545: E/AndroidRuntime(794): at android.app.ActivityThread.main(ActivityThread.java:4363)
12-11 15:12:28.545: E/AndroidRuntime(794): at java.lang.reflect.Method.invokeNative(Native Method)
12-11 15:12:28.545: E/AndroidRuntime(794): at java.lang.reflect.Method.invoke(Method.java:521)
12-11 15:12:28.545: E/AndroidRuntime(794): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-11 15:12:28.545: E/AndroidRuntime(794): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
在这两行之后,调试器正在转向异常。
Thread thread = new Thread(null, loadMoreListItems);
thread.start();
我已经搜索了很多工作,但我没有得到任何线索。请帮助我。谢谢。
答案 0 :(得分:1)
尝试使用:
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
//Set flag so we cant load new items 2 at the same time
m_loadingMore = true;
//Reset the array that holds the new items
m_myListItems = new ArrayList<String>();
if(SplashActivity.s_contactNameArrayListSplash.size() < m_itemsPerPage)
m_itemsPerPage = SplashActivity.s_contactNameArrayListSplash.size();
//Get 15 new listitems
for (int i = 0; i < m_itemsPerPage; i++) {
//Fill the item with some bogus information
m_myListItems.add(SplashActivity.s_contactNameArrayListSplash.get(i).toString());
}
//Done! now continue on the UI thread
runOnUiThread(returnRes);
}
} );
thread.start();
而不是:
Thread thread = new Thread(null, loadMoreListItems);
thread.start();