gpt4 book ai didi

java - 异步任务使我的应用程序崩溃

转载 作者:行者123 更新时间:2023-12-01 17:51:34 25 4
gpt4 key购买 nike

首先我看到两个问题 first second

但它们不同,我使用异步任务与服务器端通信,除了两种情况外它工作正常第一个,如果服务器因任何原因停止而导致我的应用程序崩溃第二种情况是当我连接到网络但该接入点上没有互联网时我的问题是我该如何处理此错误以避免崩溃?这是我的代码

    public class news_tab extends Fragment{
ListView newsLIST;
String result_news,result_fav;
String [] Hot_news,newsName1,newsText1 ,agentName;
int [] Hot_news_id,agent_id;
int [] fav_id;
private Context context;
ArrayList<String> newsName,newsText,agentNamelist;
public news_tab()
{

}
public news_tab (Context context)
{

this.context=context;
newsName=new ArrayList<String>();
newsText=new ArrayList<String>();
agentNamelist=new ArrayList<String>();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.news, container, false);
rootView.setBackground(Drawable.createFromPath("@drawable/back"));
newsLIST=(ListView)rootView.findViewById(R.id.newsLV);
getFavouriteNews();
news();


return rootView;
}
public void getFavouriteNews() {
new AsyncTask<Void, Void, Void>() {


@Override
protected Void doInBackground(Void... voids) {
try {
if(isNetworkAvailable()) {
HttpRequestSender http = new HttpRequestSender((Activity) context);
result_fav = http.SEND_POST_get_news_fav("**********/dal/login.asmx/getdata_newsfav");
}
else
{
result_fav="";
Toast.makeText(context,"Check the internet Connection",Toast.LENGTH_SHORT).show();
}
// result = http.SEND_Check("******/Login?email="
// +username.getText().toString()+"&password="+password1.getText().toString());

Log.d("String", result_fav);


} catch (Exception e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPreExecute() {



super.onPreExecute();
}

@Override
protected void onPostExecute(Void aVoid) {

if(result_fav.contains("text_news")) {

// x.cancel();
// Toast.makeText(context, "Welcome Favourite", Toast.LENGTH_SHORT).show();
// JSONObject jb = null;
try {
JSONArray JA = new JSONArray(result_fav);
ArrayList <String> newsName , newsText,agentNamelist,urlList;

fav_id= new int [JA.length()];


for (int i = 0; i < JA.length(); i++) {
JSONObject JO = (JSONObject) JA.get(i);

fav_id[i]= (int) JO.get("news_id");


}

} catch (JSONException e) {
e.printStackTrace();
}

}
else
{
// Toast.makeText(context,"No News",Toast.LENGTH_SHORT).show();

}
super.onPostExecute(aVoid);

}
}.execute();

}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}

最后我得到了这个错误

03-29 10:15:45.500 24585-24585/com.Offferly.ahmad.hakimi E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.Offferly.ahmad.hakimi, PID: 24585
java.lang.NullPointerException
at com.Offferly.ahmad.hakimi.tab.news_tab$2.onPostExecute(news_tab.java:217)
at com.Offferly.ahmad.hakimi.tab.news_tab$2.onPostExecute(news_tab.java:179)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5692)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
03-29 10:15:45.540 2952-20003/? E/WakeLock: release without a matched acquire!

最佳答案

您无法在 doInBackground() 方法中显示 Toast。如果您想执行此操作,请使用下面的代码。

runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(<your class name>.this, "Cool Ha?", Toast.LENGTH_SHORT).show();
}});

试试这个。

关于java - 异步任务使我的应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49551562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com