Android的ListView中的NullPointerException [英] Android ListView NullPointerException
本文介绍了Android的ListView中的NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有填充的ListView
的问题。 logcat的节目 NullPointerExcepton
。我也贴code和下面的logcat。
在code很着急做,我只是个初学者。所以,请指出任何错误或建议。
包com.rj.unical;进口java.io.IOException异常;
进口java.net.MalformedURLException;
进口的java.net.URL;进口org.jsoup.Jsoup;
进口org.jsoup.nodes.Document;
进口org.jsoup.nodes.Element;
进口org.jsoup.select.Elements;进口android.app.Activity;
进口android.app.ListActivity;
进口android.content.Context;
进口android.content.Intent;
进口android.net.ConnectivityManager;
进口android.net.NetworkInfo;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.ArrayAdapter;
进口android.widget.ListView;
进口android.widget.TextView;
进口android.widget.Toast;
进口android.widget.AdapterView.OnItemClickListener;公共类UnicalNotificationsActivity扩展ListActivity { 字符串notificationlinks [] =新的String [100];
字符串notificationnames [] =新的String [100];
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
//创建一个列表视图
新DownloadNotificationTask()执行(); }
/////////////////////////
私有类DownloadNotificationTask扩展的AsyncTask<弦乐,太虚,字符串> { @覆盖
在preExecute保护无效(){
Toast.makeText(UnicalNotificationsActivity.this,加载,Toast.LENGTH_LONG);
}
@覆盖
保护无效onPostExecute(字符串结果){
ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(UnicalNotificationsActivity.this,R.layout.exams,notificationnames);
setListAdapter(适配器);
ListView控件LV = getListView(); //设置项点击监听器
lv.setTextFilterEnabled(真);
lv.setOnItemClickListener(新OnItemClickListener(){ @覆盖
公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,长ARG3){
//当点击一个项目,发出相应的链接getResults类
意图I =新意图(getApplicationContext(),getResults.class);
i.putExtra(examlink,notificationlinks [(int)的ARG3]);
startActivity(ⅰ); } }); }
@覆盖
保护字符串doInBackground(字符串...为arg0){
URL UNI = NULL;
尝试{
UNI =新URL(\"http://universityofcalicut.info/index.php?option=com_content&task=view&id=744&Itemid=324\");
}赶上(MalformedURLException的E){ e.printStackTrace();
} 字符串URL = uni.toString();
文档DOC = NULL; 尝试{
DOC = Jsoup.connect(URL)获得();
}赶上(IOException异常五){
// TODO自动生成catch块
e.printStackTrace();
}
元素链接= doc.select(一个[HREF]);
INT指数= -1;
INT I = 0;
//迭代的每一个环节,只有选择那些含202.88.252.6字符串
对于(元素链接:链接){
指数= -1;
指数= link.attr(ABS:HREF)的indexOf(PDF)。 如果(指数= - 1安培;&安培; link.attr(!!!ABS:HREF)= NULL和放大器;&安培; link.text()= NULL和放大器;&安培; link.text()=&放大器;&安培; I< 100)
{
//保存的链接和相应的名称
notificationlinks [I] = link.attr(ABS:HREF);
notificationnames [I] = link.text(); Log.d(PDF FOUND,notificationlinks [I]);
}
}
返回null;
}} ///////////////
}
这里是logcat的
11月3日至6日:11:43.116:D / PDF FOUND(2626):http://universityofcalicut.info/notifications/CCSSUGIII120112.pdf
11月3日至6日:11:43.126:D / PDF FOUND(2626):http://universityofcalicut.info/notifications/NotfnIIIBAMS12112.pdf
11月3日至6日:11:43.137:D / PDF FOUND(2626):http://universityofcalicut.info/notifications/NotifnMomoeoThesis12112.pdf
11月3日至6日:11:43.166:D / PDF FOUND(2626):http://universityofcalicut.info/notifications/NotifnPGCCSS1stFolkloreExamchange10112.pdf
11月3日至6日:11:43.176:D / PDF FOUND(2626):http://universityofcalicut.info/notifications/NotiBA_POT_%20PvtRegn2012.pdf
11月3日至6日:11:43.196:D / PDF FOUND(2626):http://universityofcalicut.info/notifications/MEDexamchange020112.pdf
11月3日至6日:11:43.216:D / PDF FOUND(2626):http://universityofcalicut.info/notifications/afsaladib$p$pfinal2012.pdf
11月3日至6日:11:43.236:D / AndroidRuntime(2626):关闭VM
11月3日至6日:11:43.236:W / dalvikvm(2626):主题ID = 1:螺纹未捕获的异常退出(组= 0x4001d800)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):致命异常:主要
11月3日至6日:11:43.256:E / AndroidRuntime(2626):显示java.lang.NullPointerException
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.widget.AbsListView.obtainView(AbsListView.java:1315)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.widget.ListView.makeAndAddView(ListView.java:1727)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.widget.ListView.fillDown(ListView.java:652)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.widget.ListView.fillFromTop(ListView.java:709)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.widget.ListView.layoutChildren(ListView.java:1580)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.widget.AbsListView.onLayout(AbsListView.java:1147)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.view.View.layout(View.java:7035)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.widget.FrameLayout.onLayout(FrameLayout.java:333)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.view.View.layout(View.java:7035)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.widget.FrameLayout.onLayout(FrameLayout.java:333)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.view.View.layout(View.java:7035)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.widget.FrameLayout.onLayout(FrameLayout.java:333)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.view.View.layout(View.java:7035)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.view.View.layout(View.java:7035)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.widget.FrameLayout.onLayout(FrameLayout.java:333)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.view.View.layout(View.java:7035)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.widget.FrameLayout.onLayout(FrameLayout.java:333)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.view.View.layout(View.java:7035)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.widget.FrameLayout.onLayout(FrameLayout.java:333)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.view.View.layout(View.java:7035)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.os.Handler.dispatchMessage(Handler.java:99)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.os.Looper.loop(Looper.java:123)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在android.app.ActivityThread.main(ActivityThread.java:4627)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在java.lang.reflect.Method.invokeNative(本机方法)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在java.lang.reflect.Method.invoke(Method.java:521)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11月3日至6日:11:43.256:E / AndroidRuntime(2626):在dalvik.system.NativeStart.main(本机方法)
解决方案
在这里,我已经修改了一些快速code ..
有,如果你试图加载系统可能产生错误包含一些后台线程视图
公共类UnicalNotificationsActivity扩展ListActivity {
字符串notificationlinks [] =新的String [100];
字符串notificationnames [] =新的String [100];
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); //创建一个列表视图
ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(UnicalNotificationsActivity.this,R.layout.exams,notificationnames);
setListAdapter(适配器);
ListView控件LV = getListView(); //设置项点击监听器
lv.setTextFilterEnabled(真);
lv.setOnItemClickListener(新OnItemClickListener(){ @覆盖
公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,长ARG3){
//当点击一个项目,发出相应的链接getResults类
意图I =新意图(getApplicationContext(),getResults.class);
i.putExtra(examlink,notificationlinks [(int)的ARG3]);
startActivity(ⅰ); } }); 新DownloadNotificationTask()执行();
}
/////////////////////////
私有类DownloadNotificationTask扩展的AsyncTask<弦乐,太虚,字符串> {
@覆盖
在preExecute保护无效(){
Toast.makeText(UnicalNotificationsActivity.this,加载,Toast.LENGTH_LONG);
}
@覆盖
保护无效onPostExecute(字符串结果){
}
@覆盖
保护字符串doInBackground(字符串...为arg0){
URL UNI = NULL;
尝试{
UNI =新URL(\"http://universityofcalicut.info/index.php?option=com_content&task=view&id=744&Itemid=324\");
}赶上(MalformedURLException的E){
e.printStackTrace();
} 字符串URL = uni.toString();
文档DOC = NULL; 尝试{
DOC = Jsoup.connect(URL)获得();
}赶上(IOException异常五){
// TODO自动生成catch块
e.printStackTrace();
}
元素链接= doc.select(一个[HREF]);
INT指数= -1;
INT I = 0; //迭代的每一个环节,只有选择那些含202.88.252.6字符串
对于(元素链接:链接){
指数= -1;
指数= link.attr(ABS:HREF)的indexOf(PDF)。 如果(指数= - 1安培;&安培; link.attr(!!!ABS:HREF)= NULL和放大器;&安培; link.text()= NULL和放大器;&安培; link.text()=&放大器;&安培; I< 100)
{
//保存的链接和相应的名称
notificationlinks [I] = link.attr(ABS:HREF);
notificationnames [I] = link.text(); Log.d(PDF FOUND,notificationlinks [I]);
}
}
返回null;
}
}
///////////////
}
I am having problems populating the ListView
. Logcat Shows NullPointerExcepton
. I have also posted the code and logcat below.
The code was did in a hurry and I am just a beginner. So please point out any mistakes or suggestions.
package com.rj.unical;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class UnicalNotificationsActivity extends ListActivity {
String notificationlinks[] = new String[100];
String notificationnames[] = new String[100];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Create a list view
new DownloadNotificationTask().execute();
}
/////////////////////////
private class DownloadNotificationTask extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute(){
Toast.makeText(UnicalNotificationsActivity.this ,"Loading", Toast.LENGTH_LONG);
}
@Override
protected void onPostExecute(String result) {
ArrayAdapter<String> adapter=new ArrayAdapter<String>(UnicalNotificationsActivity.this,R.layout.exams,notificationnames);
setListAdapter(adapter);
ListView lv = getListView();
//Set item click listener
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
//When an item is clicked, send the corresponding link to getResults class
Intent i = new Intent(getApplicationContext(), getResults.class);
i.putExtra("examlink", notificationlinks[(int)arg3]);
startActivity(i);
}
});
}
@Override
protected String doInBackground(String... arg0) {
URL uni = null;
try {
uni = new URL("http://universityofcalicut.info/index.php?option=com_content&task=view&id=744&Itemid=324");
} catch (MalformedURLException e) {
e.printStackTrace();
}
String url=uni.toString();
Document doc = null;
try {
doc = Jsoup.connect(url).get();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Elements links = doc.select("a[href]");
int index=-1;
int i=0;
//Iterate every link and select only those with the string containing 202.88.252.6
for (Element link : links) {
index=-1;
index=link.attr("abs:href").indexOf(".pdf");
if(index!=-1 && link.attr("abs:href")!=null && link.text()!=null && link.text()!=" " && i<100)
{
//Store the link and corresponding names
notificationlinks[i]=link.attr("abs:href");
notificationnames[i]=link.text();
Log.d("PDF FOUND", notificationlinks[i]);
}
}
return null;
}
}
///////////////
}
here is the logcat
03-06 11:11:43.116: D/PDF FOUND(2626): http://universityofcalicut.info/notifications/CCSSUGIII120112.pdf
03-06 11:11:43.126: D/PDF FOUND(2626): http://universityofcalicut.info/notifications/NotfnIIIBAMS12112.pdf
03-06 11:11:43.137: D/PDF FOUND(2626): http://universityofcalicut.info/notifications/NotifnMomoeoThesis12112.pdf
03-06 11:11:43.166: D/PDF FOUND(2626): http://universityofcalicut.info/notifications/NotifnPGCCSS1stFolkloreExamchange10112.pdf
03-06 11:11:43.176: D/PDF FOUND(2626): http://universityofcalicut.info/notifications/NotiBA_POT_%20PvtRegn2012.pdf
03-06 11:11:43.196: D/PDF FOUND(2626): http://universityofcalicut.info/notifications/MEDexamchange020112.pdf
03-06 11:11:43.216: D/PDF FOUND(2626): http://universityofcalicut.info/notifications/afsaladibprefinal2012.pdf
03-06 11:11:43.236: D/AndroidRuntime(2626): Shutting down VM
03-06 11:11:43.236: W/dalvikvm(2626): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-06 11:11:43.256: E/AndroidRuntime(2626): FATAL EXCEPTION: main
03-06 11:11:43.256: E/AndroidRuntime(2626): java.lang.NullPointerException
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.widget.ListView.makeAndAddView(ListView.java:1727)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.widget.ListView.fillDown(ListView.java:652)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.widget.ListView.fillFromTop(ListView.java:709)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.widget.ListView.layoutChildren(ListView.java:1580)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.widget.AbsListView.onLayout(AbsListView.java:1147)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.view.View.layout(View.java:7035)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.view.View.layout(View.java:7035)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.view.View.layout(View.java:7035)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.view.View.layout(View.java:7035)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.view.View.layout(View.java:7035)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.view.View.layout(View.java:7035)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.view.View.layout(View.java:7035)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.view.View.layout(View.java:7035)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.os.Looper.loop(Looper.java:123)
03-06 11:11:43.256: E/AndroidRuntime(2626): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-06 11:11:43.256: E/AndroidRuntime(2626): at java.lang.reflect.Method.invokeNative(Native Method)
03-06 11:11:43.256: E/AndroidRuntime(2626): at java.lang.reflect.Method.invoke(Method.java:521)
03-06 11:11:43.256: E/AndroidRuntime(2626): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-06 11:11:43.256: E/AndroidRuntime(2626): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-06 11:11:43.256: E/AndroidRuntime(2626): at dalvik.system.NativeStart.main(Native Method)
解决方案
Here i have modified some quick code ..
there it may generate an error if you tried to load the system contain View in some background thread
public class UnicalNotificationsActivity extends ListActivity {
String notificationlinks[] = new String[100];
String notificationnames[] = new String[100];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Create a list view
ArrayAdapter<String> adapter=new ArrayAdapter<String>(UnicalNotificationsActivity.this,R.layout.exams,notificationnames);
setListAdapter(adapter);
ListView lv = getListView();
//Set item click listener
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
//When an item is clicked, send the corresponding link to getResults class
Intent i = new Intent(getApplicationContext(), getResults.class);
i.putExtra("examlink", notificationlinks[(int)arg3]);
startActivity(i);
}
});
new DownloadNotificationTask().execute();
}
/////////////////////////
private class DownloadNotificationTask extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute(){
Toast.makeText(UnicalNotificationsActivity.this ,"Loading", Toast.LENGTH_LONG);
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected String doInBackground(String... arg0) {
URL uni = null;
try {
uni = new URL("http://universityofcalicut.info/index.php?option=com_content&task=view&id=744&Itemid=324");
} catch (MalformedURLException e) {
e.printStackTrace();
}
String url=uni.toString();
Document doc = null;
try {
doc = Jsoup.connect(url).get();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Elements links = doc.select("a[href]");
int index=-1;
int i=0;
//Iterate every link and select only those with the string containing 202.88.252.6
for (Element link : links) {
index=-1;
index=link.attr("abs:href").indexOf(".pdf");
if(index!=-1 && link.attr("abs:href")!=null && link.text()!=null && link.text()!=" " && i<100)
{
//Store the link and corresponding names
notificationlinks[i]=link.attr("abs:href");
notificationnames[i]=link.text();
Log.d("PDF FOUND", notificationlinks[i]);
}
}
return null;
}
}
///////////////
}
这篇关于Android的ListView中的NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文