简体   繁体   English

Android从其余Web服务获取数据

[英]Android Getting data from rest web service

I want the Main Activity gets some data from one rest web service. 我希望主要活动从一个其他Web服务获取一些数据。 For this job I use AsyncTask for the connection with the web service. 为此,我将AsyncTask用于与Web服务的连接。 When in the onCreate method displayed the XML Layout file, that will display the data from the web service, the application works successfully. 当在onCreate方法中显示XML布局文件(该文件将显示来自Web服务的数据)时,该应用程序将成功运行。 However when in the onCreate method displayed a different XML Layout file, from the Layout file that will display the data from the web service, the application doesn't work and logcats displays NullPointerException in the data that were taken from web service. 但是,当onCreate方法中的Layout文件(将显示来自Web服务的数据)显示的XML Layout文件不同时,该应用程序将无法运行,并且logcats在从Web服务获取的数据中显示NullPointerException。

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() );
        }
    }
}

The error located in the row: 该错误位于行中:

testTextView.setText( sightsList.getList().get( 1 ).toString() ); testTextView.setText(sightsList.getList()。get(1).toString());

Logcat displays: 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)

If you load a different xml layout, it won't have a textview named testTextView. 如果加载其他xml布局,则不会有名为testTextView的textview。 That will make findViewById return null. 这将使findViewById返回null。 That will cause a NullPointerException in onPostExecute when you try to call setText on it. 当您尝试在onPostExecute上调用setText时,将导致NullPointerException。

This problem was kind of trivial, but in the future please attach a stack trace with any crashing bugs. 这个问题有点琐碎,但是将来请附加堆栈跟踪以及任何崩溃的错误。

Make sure sightsList is not null if you are sure about Gabe's answer. 如果您确定Gabe的答案,请确保sightsList不为空。 I would suggest you to break the testTextView.setText( sightsList.getList().get( 1 ).toString() ); 我建议你打破testTextView.setText( sightsList.getList().get( 1 ).toString() ); to several pieces to easily find out what is happening there. 分成几部分,以轻松找出那里发生了什么。

List targetList = sightsList.getList();
String data = targetList.get(1).toString(); // this index 1 might return null also
textTextView.setText(data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM