我使用ListFragment
在片段中加载listview。但运行应用程序后我的应用程序崩溃。我检查了logcat
,但我没有发现问题是什么。这是logcat
:
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: FATAL EXCEPTION: main
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: Process: whc.ir.news, PID: 1545
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: java.lang.NullPointerException
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.widget.AbsListView.obtainView(AbsListView.java:2265)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.widget.ListView.makeAndAddView(ListView.java:1790)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.widget.ListView.fillDown(ListView.java:691)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.widget.ListView.fillFromTop(ListView.java:752)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.widget.ListView.layoutChildren(ListView.java:1630)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.widget.AbsListView.onLayout(AbsListView.java:2091)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.View.layout(View.java:14817)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:4631)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.View.layout(View.java:14817)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:4631)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1627)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.View.layout(View.java:14817)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:4631)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1037)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:747)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1156)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:760)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.View.layout(View.java:14817)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:4631)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.View.layout(View.java:14817)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:4631)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1043)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.View.layout(View.java:14817)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:4631)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.View.layout(View.java:14817)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:4631)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.View.layout(View.java:14817)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:4631)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.View.layout(View.java:14817)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:4631)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.View.layout(View.java:14817)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:4631)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.View.layout(View.java:14817)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:4631)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:574)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:544)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:733)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
12-28 00:42:55.170 1545-1545/whc.ir.news E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5017)
这是我的代码:
package whc.ir.news.activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import whc.ir.news.R;
import whc.ir.news.adapter.Adapter_lst_List_news;
import whc.ir.news.classes.MyApplication;
import whc.ir.news.classes.Utility;
/**
* Created by marzieh on 25/12/2015.
*/
public class TCenterFragment2 extends ListFragment implements AdapterView.OnItemClickListener ,AbsListView.OnScrollListener {
boolean is_null_result2 = false;
private int cnt_load = 10;
private Adapter_lst_List_news adp;
private ProgressBar progressBar;
private String id_tree_news = "8611";
private String Node_id = "394";
private String res1, line, res2, line2;
private InputStream is, is2;
private Bundle b, send;
private ListView lst_List_news;
View myview;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myview = inflater.inflate(R.layout.fragment_t_center, container, false);
progressBar = (ProgressBar) myview.findViewById(R.id.progressBar1);
progressBar.setVisibility(View.GONE);
lst_List_news= (ListView) myview.findViewById(android.R.id.list);
final File file = getContext().getFileStreamPath(id_tree_news + ".txt");
AlertForNoConnection();
CheckForFileExist(file);
FillAdapter();
return myview;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Planets, android.R.layout.simple_list_item_1);
setListAdapter(adp);
getListView().setOnItemClickListener(this);
getListView().setOnScrollListener(this);
}
private void AlertForNoConnection() {
// Alert Dialog if No Internet
if (!Utility.isOnline(getActivity().getApplicationContext())) {
try {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
alertDialog.setMessage(getString(R.string.dlg_internet));
alertDialog.setPositiveButton(getString(R.string.approve),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
});
alertDialog.show();
} catch (Exception e) {
Log.e("fail intenet", "fail in is online check");
e.printStackTrace();
}
}
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.build();
StrictMode.setThreadPolicy(policy);
}
}
private String CheckForFileExist(File file)
{
/***
* Check For File exist
***/
if (file.exists()) {
//Log.e("file exist", "file exist");
try {
FileInputStream fileIn = MyApplication.getContext().openFileInput(id_tree_news + ".txt");
InputStreamReader inputReader = new InputStreamReader(fileIn);
char[] inputBuffer = new char[100];
String s = "";
int charRead;
while ((charRead = inputReader.read(inputBuffer)) > 0) {
String readstring = String.copyValueOf(inputBuffer, 0,
charRead);
s += readstring;
}
res1 = s;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
Log.e("Fail Read File", "Fail");
e.printStackTrace();
}
} else {
//setTitle("not exist");
Log.e("File Notexist", "File Not exist");
try {
HttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
Uri.parse(getString(R.string.site_address))
+ "get_list/" + Node_id + "/"
+ id_tree_news + "/0/10");
// HttpPost httppost = new
// HttpPost(Uri.parse(getString(R.string.site_address))+"get_list/"+b.get("id_node")+"/"+b.get("id_tree_news")+"/0/00");
List<NameValuePair> param = new ArrayList<NameValuePair>();
param.add(new BasicNameValuePair("username", "mobile"));
param.add(new BasicNameValuePair("password", "M0b!1e.whc"));
httppost.setEntity(new UrlEncodedFormEntity(param));
HttpResponse response = client.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
//Log.e("Pass", "connection successfully");
} catch (Exception e) {
Log.e("Fail 1", e.toString());
e.printStackTrace();
LayoutInflater inflater = getActivity().getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) myview.findViewById(R.id.toast_layout_root));
TextView text = (TextView) layout.findViewById(R.id.toas_text);
text.setText(getResources().getString(R.string.connection_problem));
Typeface typeface = Typeface.createFromAsset(getActivity().getAssets(), "IranSansB.ttf");
text.setTypeface(typeface);
Toast toast = new Toast(getActivity().getApplicationContext());
toast.setGravity(Gravity.BOTTOM, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();
getActivity().finish();
}
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
res1 = sb.toString();
//Log.e("Pass 2", "Pass 2 successfuly");
} catch (Exception e) {
Log.e("Fail 2", e.toString());
e.printStackTrace();
}
}
/***
* end Check
***/
return res1;
}
private void FillAdapter()
{
try {
JSONArray JA = new JSONArray(res1);
if (JA.length() > 0) {
JSONObject json = new JSONObject();
ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < JA.length(); i++) {
json = JA.getJSONObject(i);
HashMap<String, String> data_item = new HashMap<String, String>();
data_item.put("id_list_news", json.getString("id"));
data_item.put("date_list_news",
json.getString("date_publish"));
data_item.put("title_list_news", json.getString("title"));
data_item.put("img_url_list_news", json.getString("img"));
data_item.put("sum_list_news", json.getString("summary"));
data.add(data_item);
}
// adp = new Adapter_lst_List_news(getActivity(), data, Integer.parseInt(b.get("id_node").toString()));
adp = new Adapter_lst_List_news(getActivity(), data, Integer.parseInt(Node_id));
}
Log.e("Pass 3", "succesfully pass 3 _list_news");
} catch (Exception e) {
Log.e("Fail 3", "Fail 3 !!!! _listNews");
e.printStackTrace();
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
send = new Bundle();
send.putString("id_node", b.getString("id_node"));
send.putString("id_list_news",
adp.getItemId(position) + "");
Intent i = new Intent(getActivity(),
Content_News_Activity.class);
i.putExtras(send);
startActivity(i);
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (lst_List_news.getLastVisiblePosition() == lst_List_news
.getAdapter().getCount() - 1
&& lst_List_news.getChildAt(
lst_List_news.getChildCount() - 1)
.getBottom() <= lst_List_news
.getHeight()) {
new new_load_async().execute("");
}
}
class new_load_async extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
if (is_null_result2 == false)
progressBar.setVisibility(View.VISIBLE);
else
progressBar.setVisibility(View.GONE);
}
@Override
protected String doInBackground(String... arg0) {
try {
Log.e("P d =>Visible", "Visisble");
cnt_load = cnt_load + 10;
HttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
Uri.parse(getString(R.string.site_address))
+ "get_list/" + b.get("id_node") + "/"
+ b.get("id_tree_news") + "/" + cnt_load
+ "/10");
List<NameValuePair> param = new ArrayList<NameValuePair>();
param.add(new BasicNameValuePair("username", "mobile"));
param.add(new BasicNameValuePair("password", "M0b!1e.whc"));
httppost.setEntity(new UrlEncodedFormEntity(param));
HttpResponse response = client.execute(httppost);
HttpEntity entity = response.getEntity();
is2 = entity.getContent();
// pDialog.dismiss();
Log.e("Pass", "connection successfully _New Async Load");
} catch (Exception e) {
Log.e("Fail 1 _New Async Load", e.toString());
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is2, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
while ((line2 = reader.readLine()) != null) {
sb.append(line2 + "\n");
}
res2 = sb.toString();
getActivity().runOnUiThread(new Runnable() {
public void run() {
progressBar.setVisibility(View.INVISIBLE);
}
});
if (res2 == null) {
is_null_result2 = true;
Log.e("result 2", "result 2 is null ====" + res2);
} else {
is_null_result2 = false;
//Log.e("result 2", "result 2 is not null=======>>" + result2);
}
return res2;
} catch (Exception e) {
Log.e("Fail 2 _New Async Load", e.toString());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String myresult) {
set_adapter_asycn();
}
}
public void set_adapter_asycn() {
try {
JSONArray JA = new JSONArray(res2);
JSONObject json = new JSONObject();
if (JA.length() > 0) {
for (int i = 0; i < JA.length(); i++) {
json = JA.getJSONObject(i);
HashMap<String, String> data_item = new HashMap<String, String>();
data_item.put("id_list_news", json.getString("id"));
data_item.put("date_list_news",
json.getString("date_publish"));
data_item.put("title_list_news", json.getString("title"));
data_item.put("img_url_list_news", json.getString("img"));
data_item.put("sum_list_news", json.getString("summary"));
adp.data.add(data_item);
adp.notifyDataSetChanged();
}
Log.e("Pass3 _New Load", "pass 3 _New Load");
}
} catch (Exception e) {
Log.v("new_load_fail 3", e.toString() + "New Load_Fail 3");
e.printStackTrace();
}
}
}
我的片段xml代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F3EFF3"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<TextView
android:id="@+id/tv_List_news"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tv_List_news"
android:divider="@android:color/transparent"
android:dividerHeight="12dp" >
</ListView>
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
答案 0 :(得分:0)
在onActivityCreated()回调中获取ListView的有效引用。
移到onActivityCreated()
内的代码行下面lst_List_news= (ListView) myview.findViewById(android.R.id.list);
Coz onCreateView创建并返回与片段关联的视图层次结构。
更改以下行 -
getListView().setOnItemClickListener(this);
getListView().setOnScrollListener(this);
到 -
lst_List_news.setOnItemClickListener(this);
lst_List_news.setOnScrollListener(this);
答案 1 :(得分:0)
从onActivityCreated
方向
onCreateView
移至FillAdapter()
以下
setListAdapter(adp);
getListView().setOnItemClickListener(this);
getListView().setOnScrollListener(this);