Android 将addFooterView添加到活动中的Listview会导致空指针异常
您好,我正在使用ASYNC任务解析json中的数据,并将其列在活动中。但是,如果没有AddFooter视图,它可以正常工作。当我尝试添加它时,它会给我空指针异常Android 将addFooterView添加到活动中的Listview会导致空指针异常,android,android-activity,android-listview,Android,Android Activity,Android Listview,您好,我正在使用ASYNC任务解析json中的数据,并将其列在活动中。但是,如果没有AddFooter视图,它可以正常工作。当我尝试添加它时,它会给我空指针异常 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); new FilesTask(baseurl, true).execute(); footerView = getLayoutInf
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
new FilesTask(baseurl, true).execute();
footerView = getLayoutInflater().inflate(R.layout.listview_footer, null); }
@Override
protected void onPreExecute() {
if (isLoading){
this.cancel(true);
} else {
isLoading = true;
}
if (initialload){
pDialog = (RelativeLayout) ll.findViewById(R.id.progressBarHolder);
if (pDialog.getVisibility() == View.GONE) {
pDialog.setVisibility(View.VISIBLE);
feedListView.setVisibility(View.GONE);
}
if (null != feedList){
feedList.clear();
}
if (null != feedListView){
feedListView.setAdapter(null);
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
feedListView.addFooterView(footerView);
}
} else {
feedListView.addFooterView(footerView);
}
}
@Override
protected void onPostExecute(Void result) {
if (null != feedList) {
//set adapter
updateList(initialload);
if (pDialog.getVisibility() == View.VISIBLE) {
pDialog.setVisibility(View.GONE);
feedListView.setVisibility(View.VISIBLE);
// Helper.revealView(feedListView,ll);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
feedListView.addFooterView(footerView);
}
} else if(footerView.getVisibility()== View.VISIBLE) {
feedListView.addFooterView(footerView);
}
}else{ // else feedList is empty
pDialog.setVisibility(View.GONE);
}
isLoading = false;
}
在这里,我粘贴了onPreExecute和postexecute。如果我在这里做错了什么,请告诉我。错误发生在
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
feedListView.addFooterView(footerView);
}
931-931/com.example E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.search.SearchActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.search.SearchActivity$DownloadFilesTask.onPreExecute(SearchActivity.java:198)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
at android.os.AsyncTask.execute(AsyncTask.java:534)
at com.example.search.SearchActivity.onCreate(SearchActivity.java:105)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
if(Build.VERSION.SDK\u INT
ListView必须在setAdapter之前添加FootView或HeadView,请参阅下面的源代码
/**
* Add a fixed view to appear at the bottom of the list. If addFooterView is called more
* than once, the views will appear in the order they were added. Views added using
* this call can take focus if they want.
* <p>NOTE: Call this before calling setAdapter. This is so ListView can wrap the supplied
* cursor with one that will also account for header and footer views.
*
*
* @param v The view to add.
*/
public void addFooterView(View v) {
addFooterView(v, null, true);
}
/**
*添加固定视图以显示在列表底部。如果addFooterView被称为“更多”
*不止一次,视图将按添加顺序显示。使用添加的视图
*如果他们愿意,这个电话可以引起关注。
*注意:在调用setAdapter之前调用此选项。这样ListView就可以包装提供的
*光标,其中一个光标还将用于页眉和页脚视图。
*
*
*@param v要添加的视图。
*/
公共视图(视图v){
addFooterView(v,null,true);
}
在这里,我必须在addFooterView之前初始化listView
首先初始化
footerView = getLayoutInflater().inflate(R.layout.listview_footer, null);
然后
fileTask(baseurl,true).execute();将执行set adapter的剩余部分。
实际上,这种错误发生在KITKAT下面的SDK中。解决办法是
1.要初始化listview,请单击footerView
2.完成剩下的ASNYC任务您的
页脚视图在哪里
initialized@A.S.public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);footerView=getLayoutInflater().inflate(R.layout.listview_footer,null);}您的footerView可能为null,你能展示更多关于它的实例吗?@Bubu它在SDK KITKAT下面没有页脚的情况下运行良好。上面的SDK Kitkat可以很好地处理页脚。public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);footerView=getLayoutInflater().inflate(R.layout.listview_footer,null);}您能在设置适配器之前添加页脚吗?请发布异常日志。请检查它现在添加了logcat
footerView = getLayoutInflater().inflate(R.layout.listview_footer, null);
new FilesTask(baseurl, true).execute();