API不起作用,progressBar继续旋转

时间:2016-07-16 15:11:12

标签: java android api android-asynctask

我是初学者,我正在尝试学习如何在Android上使用API​​。我正在做比特币价格应用程序来学习API。但我有一些问题。我在mainactivity.xml中创建了一个进度条和一个价格TextView。默认情况下,我将progressbar设置为visible而textview不可见。我认为非常简单,我创建了一个新的AsyncTask类,在后台完成后,我将设置进度条不可见,并在onPostExecute()中显示textview以显示价格。但它没有用。进度条一直在旋转,它没有显示价格,我不明白它为什么不起作用。

The API i am using

MainActivity

import android.os.AsyncTask;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.util.Log;

import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;


import org.json.JSONObject;
import org.json.JSONTokener;

import java.net.URL;
import java.net.HttpURLConnection;
import java.io.InputStreamReader;
import java.io.BufferedReader;


public class MainActivity extends AppCompatActivity {


        public static String line;
        public static ProgressBar progressBar;

        static String API_URL="https://api.coindesk.com/v1/bpi/currentprice/try.json";

        static TextView priceTextView;
        static TextView timeTextView;

        static int priceInt;




            @Override
            protected void onCreate( Bundle savedInstanceState) {

                super.onCreate(savedInstanceState);


                setContentView(R.layout.activity_main);



                priceTextView=(TextView)findViewById(R.id.priceText);
                timeTextView=(TextView)findViewById(R.id.timeText);

                progressBar=(ProgressBar)findViewById(R.id.progressBar);


                retrievePriceTask retrievePriceTask=new retrievePriceTask();
                retrievePriceTask.execute();



            }




        }



   class retrievePriceTask extends AsyncTask <Void,Void,String>{




       @Override
       protected void onPreExecute() {
           MainActivity.progressBar.setVisibility(View.VISIBLE);
           MainActivity.priceTextView.setVisibility(View.INVISIBLE);


       }

       @Override
       protected String doInBackground(Void... urls) {

           try {
               URL url = new URL(MainActivity.API_URL);
               HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
               try {
                   BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                   StringBuilder stringBuilder = new StringBuilder();

                   while ((MainActivity.line = bufferedReader.readLine()) != null) {
                       stringBuilder.append(MainActivity.line).append("\n");
                   }
                   bufferedReader.close();
                   return stringBuilder.toString();


               }
               finally{
                   urlConnection.disconnect();
               }
           }
           catch(Exception e) {
               Log.e("ERROR", e.getMessage(), e);
               return null;
           }
       }





       @Override
       protected void onPostExecute(String s) {



           MainActivity.priceTextView.setVisibility(View.VISIBLE);
           MainActivity.progressBar.setVisibility(View.INVISIBLE);


           try{
               JSONObject jsonObject=(JSONObject)new JSONTokener(MainActivity.line).nextValue();
                MainActivity.priceInt=jsonObject.getJSONObject("bpi").getJSONObject("TRY").getInt("rate");

               MainActivity.priceTextView.setText(MainActivity.priceInt);
           }
           catch (Exception e){
               e.printStackTrace();
           }


       }
   }

我的Logcat

07-16 10:20:08.884 2269-2269/zaaa.trinki E/AndroidRuntime: FATAL EXCEPTION: main
                                                           Process: zaaa.trinki, PID: 2269
                                                           java.lang.RuntimeException: Unable to start activity ComponentInfo{zaaa.trinki/zaaa.trinki.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference
                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                                                               at android.app.ActivityThread.access$800(ActivityThread.java:144)
                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                               at android.os.Looper.loop(Looper.java:135)
                                                               at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                               at java.lang.reflect.Method.invoke(Method.java:372)
                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
                                                            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference
                                                               at zaaa.trinki.retrievePriceTask.onPreExecute(MainActivity.java:94)
                                                               at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
                                                               at android.os.AsyncTask.execute(AsyncTask.java:535)
                                                               at zaaa.trinki.MainActivity.onCreate(MainActivity.java:55)
                                                               at android.app.Activity.performCreate(Activity.java:5933)
                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
                                                               at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                               at android.os.Looper.loop(Looper.java:135) 
                                                               at android.app.ActivityThread.main(ActivityThread.java:5221) 
                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                               at java.lang.reflect.Method.invoke(Method.java:372) 
                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
07-16 10:37:42.062 16262-16262/zaaa.trinki E/AndroidRuntime: FATAL EXCEPTION: main
                                                             Process: zaaa.trinki, PID: 16262
                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{zaaa.trinki/zaaa.trinki.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x0
                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                                                                 at android.app.ActivityThread.access$800(ActivityThread.java:144)
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                 at android.os.Looper.loop(Looper.java:135)
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
                                                              Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
                                                                 at android.content.res.Resources.getText(Resources.java:274)
                                                                 at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
                                                                 at android.widget.TextView.setText(TextView.java:4122)
                                                                 at zaaa.trinki.MainActivity.onCreate(MainActivity.java:63)
                                                                 at android.app.Activity.performCreate(Activity.java:5933)
                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
                                                                 at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                 at android.os.Looper.loop(Looper.java:135) 
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5221) 
                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                 at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

1 个答案:

答案 0 :(得分:0)

使用ProgressDialog代替ProgressBar:

ProgressDialog myProgressDialog = ProgressDialog.show(Context CONTEXT, String TITLE, String MESSAGE, boolean INDETERMINATE, boolean CANCELABLE);

myProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

并在你的onPostExecute()中放入myProgressDialog.dismiss()