如何在异步任务中从 onpostexecute 开始新的 activity

how to start new activity from onpostexecute in async task

**我有这三个 classes。我的 Mainactivity、newactivity 和扩展 AsyncTask 的那个,现在在扩展 Asynctask 的那个中,我需要在 AsyncTask 的 OnPostExecute() 上开始新的 activity。我怎样才能做到这一点?我已经完成了上下文,但我从 mainactivity 执行 backgroundtask 行时收到错误,但应用程序崩溃请帮助 **

这是主要的activity class

public class MainActivity extends AppCompatActivity {

    Button click;
    //public static TextView data;
    public static LineChart mchart;
    List<Entry> x;
    ArrayList<String> y;
    private fetchdata list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        click=(Button)findViewById(R.id.button);
        mchart=(LineChart)findViewById(R.id.linechart);
        //data=(TextView)findViewById(R.id.fetcheddata);
        list=new fetchdata();
        click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fetchdata process=new fetchdata();
                process.execute();


            }
        });


    }
}





 public class fetchdata extends AsyncTask<Void,Void,Void> {
        String data="";
        String dataparsed="";
        String singleparsed="";
        List<Entry> x;
        List<String> y;
        boolean flag=false;


        @Override
        protected Void doInBackground(Void... voids) {

            x = new ArrayList<Entry>();
            y = new ArrayList<String>();
            try {
                URL url=new URL("https://io.adafruit.com/api/v2/Yarev/feeds/pir-sensor/data");
                HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
                InputStream inputStream=httpURLConnection.getInputStream();
                BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
                String line="";
                while (line!=null)
                {
                    line=bufferedReader.readLine();
                    data=data+line;
                }
                JSONArray JA=new JSONArray(data);
                for(int i=0;i<JA.length();i++)
                {
                    JSONObject JO= (JSONObject) JA.get(i);
                    singleparsed="Value:"+JO.get("value")+"\n"+
                            "Feed key:"+JO.get("feed_key")+"\n"+
                            "Created:"+JO.get("created_at")+"\n";
                    int value=JO.getInt("value");
                    float v1=value;
                    x.add(new Entry(i,v1));
                    dataparsed=dataparsed+singleparsed;
                }


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

            return null;
        }


        @Override
        protected void onPostExecute(Void aVoid) {

            super.onPostExecute(aVoid);

            //start new activity?????


        }
        public List<Entry> getList() {
            return x;
        }

    }

您必须使用上下文创建构造函数

public class fetchdata extends AsyncTask<Void,Void,Void> {
        String data="";
        String dataparsed="";
        String singleparsed="";
        List<Entry> x;
        List<String> y;
        boolean flag=false;

        private WeakReference<Context> contextRef;

        public fetchdata(Context context) {
            contextRef = new WeakReference<>(context);
        } 


        @Override
        protected Void doInBackground(Void... voids) {

            x = new ArrayList<Entry>();
            y = new ArrayList<String>();
            try {
                URL url=new URL("https://io.adafruit.com/api/v2/Yarev/feeds/pir-sensor/data");
                HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
                InputStream inputStream=httpURLConnection.getInputStream();
                BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
                String line="";
                while (line!=null)
                {
                    line=bufferedReader.readLine();
                    data=data+line;
                }
                JSONArray JA=new JSONArray(data);
                for(int i=0;i<JA.length();i++)
                {
                    JSONObject JO= (JSONObject) JA.get(i);
                    singleparsed="Value:"+JO.get("value")+"\n"+
                            "Feed key:"+JO.get("feed_key")+"\n"+
                            "Created:"+JO.get("created_at")+"\n";
                    int value=JO.getInt("value");
                    float v1=value;
                    x.add(new Entry(i,v1));
                    dataparsed=dataparsed+singleparsed;
                }


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

            return null;
        }


        @Override
        protected void onPostExecute(Void aVoid) {

            super.onPostExecute(aVoid);

           if (context != null) {
            // start new activity with context
             Intent intent=new Intent(context,NewActivity.class);
             context.startActivity(intent);
           }


        }
        public List<Entry> getList() {
            return x;
        }

    }

开始你的攻略:

new fetchdata(this).execute();