这是我的AsyncTask类,我在其中返回一些文本,然后想在onPostExecute方法中使用但是我得到null值,我不知道为什么我得到null-Value
class NetworkOperation extends AsyncTask<String,int[],String>
{
static String line;
MainActivity mainActivity;
@Override
protected String doInBackground(String... params) {
Log.e("Async Method",params[0]+","+params[1]);
try
{
Log.e("ASYNC try started",params[1]+"");
String urlParameters = params[1].toString();
URL url = new URL(params[0].toString());
line="some text"; //in place of this some logic is there
Log.e("OUTPUT OF ASYNc",line);
}
catch (Exception e)
{
}
Log.e("OUTPUT OF ASYNc",line);//line has data which i want
return line; //return value to use in onPostExecute() method
}
@Override
protected void onPostExecute(String result) {
if(result==null) //here i am getting null value
{
Log.e("result is null","result is null");
}
else
{
Log.e("value of result",result);
mainActivity.setJsonData(result);
}
}
}
LogCat错误在下面给出
12-31 00:47:45.999: E/AndroidRuntime(2765): FATAL EXCEPTION: AsyncTask #1
12-31 00:47:45.999: E/AndroidRuntime(2765): Process: com.tatya, PID: 2765
12-31 00:47:45.999: E/AndroidRuntime(2765): java.lang.RuntimeException: An error occured while executing doInBackground()
12-31 00:47:45.999: E/AndroidRuntime(2765): at android.os.AsyncTask$3.done(AsyncTask.java:300)
12-31 00:47:45.999: E/AndroidRuntime(2765): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
12-31 00:47:45.999: E/AndroidRuntime(2765): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
12-31 00:47:45.999: E/AndroidRuntime(2765): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
12-31 00:47:45.999: E/AndroidRuntime(2765): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-31 00:47:45.999: E/AndroidRuntime(2765): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-31 00:47:45.999: E/AndroidRuntime(2765): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-31 00:47:45.999: E/AndroidRuntime(2765): at java.lang.Thread.run(Thread.java:841)
12-31 00:47:45.999: E/AndroidRuntime(2765): Caused by: java.lang.NullPointerException: println needs a message
12-31 00:47:45.999: E/AndroidRuntime(2765): at android.util.Log.println_native(Native Method)
12-31 00:47:45.999: E/AndroidRuntime(2765): at android.util.Log.e(Log.java:232)
12-31 00:47:45.999: E/AndroidRuntime(2765): at com.tatya.NetworkOperation.doInBackground(MainActivity.java:149)
12-31 00:47:45.999: E/AndroidRuntime(2765): at com.tatya.NetworkOperation.doInBackground(MainActivity.java:1)
12-31 00:47:45.999: E/AndroidRuntime(2765): at android.os.AsyncTask$2.call(AsyncTask.java:288)
12-31 00:47:45.999: E/AndroidRuntime(2765): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
答案 0 :(得分:3)
试试这种方式
if(TextUtils.isEmpty(result))
而不是
if(result==null)