我希望主要活動從一個其他Web服務獲取一些數據。對於這項工作,我使用AsyncTask來連接Web服務。在onCreate方法中顯示XML佈局文件時,它將顯示來自Web服務的數據,該應用程序可以成功運行。但是,當onCreate方法顯示不同的XML佈局文件時,將顯示來自Web服務的數據的佈局文件中,應用程序不起作用,並且logcats在從Web服務獲取的數據中顯示NullPointerException。Android從其他Web服務獲取數據
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
new DownloadSights().execute();
}
class DownloadSights extends AsyncTask< Void, Void, Places >
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected ListKozanisPlaces doInBackground(Void... params)
{
try
{
final String url = "http://192.168.1.3:8080/.../getSights";
InputStreamReader reader = new InputStreamReader(new URL(url).openStream());
Places sightsList = new Gson().fromJson(reader, Places.class);
return sightsList;
}
catch (Exception e)
{
Log.e("CityCuide", e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Places sightsList)
{
TextView testTextView = (TextView) findViewById(R.id.testTextView);
testTextView.setText(sightsList.getList().get(1).toString());
}
}
}
位於行中的錯誤:
testTextView.setText(sightsList.getList()得到(1)的ToString());
logcat中顯示:
01-22 01:31:12.211: E/AndroidRuntime(5455): FATAL EXCEPTION: main
01-22 01:31:12.211: E/AndroidRuntime(5455): java.lang.NullPointerException
01-22 01:31:12.211: E/AndroidRuntime(5455): at com.example.androidkozaniwebserviceclient.SplashScreenActivity$DownloadSights.onPostExecute(SplashScreenActivity.java:66)
01-22 01:31:12.211: E/AndroidRuntime(5455): at com.example.androidkozaniwebserviceclient.SplashScreenActivity$DownloadSights.onPostExecute(SplashScreenActivity.java:1)
01-22 01:31:12.211: E/AndroidRuntime(5455): at android.os.AsyncTask.finish(AsyncTask.java:602)
01-22 01:31:12.211: E/AndroidRuntime(5455): at android.os.AsyncTask.access$600(AsyncTask.java:156)
01-22 01:31:12.211: E/AndroidRuntime(5455): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
01-22 01:31:12.211: E/AndroidRuntime(5455): at android.os.Handler.dispatchMessage(Handler.java:99)
01-22 01:31:12.211: E/AndroidRuntime(5455): at android.os.Looper.loop(Looper.java:137)
01-22 01:31:12.211: E/AndroidRuntime(5455): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-22 01:31:12.211: E/AndroidRuntime(5455): at java.lang.reflect.Method.invokeNative(Native Method)
01-22 01:31:12.211: E/AndroidRuntime(5455): at java.lang.reflect.Method.invoke(Method.java:511)
01-22 01:31:12.211: E/AndroidRuntime(5455): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-22 01:31:12.211: E/AndroidRuntime(5455): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-22 01:31:12.211: E/AndroidRuntime(5455): at dalvik.system.NativeStart.main(Native Method)
請發表您的logcat –
編輯我的帖子.. –