如何从onCreatemethod.?

时间:2016-04-16 14:46:55

标签: java android android-asynctask

我正在开发简单的Android应用程序,我正在使用此代码访问servlet,但我收到java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()错误。 我试图在谷歌搜索它但找不到任何东西。“MY_IP_ADDRESS”被认为是我删除的系统的Ipadress。请任何人都可以建议我在缺少调用AsyncTask的地方。?

package com.example.nirmal.gaminghub;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Looper;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
 import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;


public class DetailsOfGame extends AppCompatActivity {

public String gameID="";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details_of_game);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    TextView tv = (TextView) findViewById(R.id.ID);
    Bundle extras = getIntent().getExtras();
        String value = extras.getString("gameID");
        //tv.setText(value);
        gameID = value;
        Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_LONG).show();
        new ViewDetailsTask(DetailsOfGame.this).execute();
        Toast.makeText(getApplicationContext(), "hello there", Toast.LENGTH_LONG).show();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

public class ViewDetailsTask extends AsyncTask<Void, Void, Void>{
    TextView tv=(TextView)findViewById(R.id.ID);
    StringBuilder getOutput = new StringBuilder();

    DetailsOfGame detailsOfGame=null;
    ViewDetailsTask(DetailsOfGame detailsOfGame)
    {
        this.detailsOfGame=detailsOfGame;
    }
    protected Void doInBackground(Void... param) {

        try {
            String gmID=gameID;
            URL openUrl = new URL("http://MY_IP_ADDRESS:8080/GamingHub/ShowDataServlet");
            Toast.makeText(getApplicationContext(), "in async task ", Toast.LENGTH_LONG).show();

            HttpURLConnection connection = (HttpURLConnection) openUrl.openConnection();

            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setDoInput(true);
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line = "";

            while ((line = br.readLine()) != null) {
                getOutput.append(line);
            }
            br.close();

        } catch (Exception e) {
            e.printStackTrace();
           // Toast.makeText(getApplicationContext(), "in async task exception", Toast.LENGTH_LONG).show();
        }
        return null;
    }
    protected void onPostExecute(Void result)
    {
        tv.setText(getOutput);
    }
}
}

2 个答案:

答案 0 :(得分:1)

尝试从UI线程调用Toast.makeText(...):

getActivity().runOnUiThread(new Runnable() {
  public void run() {
    Toast.makeText(activity, "Hello", Toast.LENGTH_SHORT).show();
  }
});

答案 1 :(得分:0)

您的textView将无法从您的AsyncTask访问。

第1步。在Activity中创建一个TextView变量:

private TextView       tv;

步骤2.在活动的onCreate中,获取相应的textView:

tv=(TextView)findViewById(R.id.ID);

步骤3.删除异步任务中的声明

编辑:正如G. Blake Meike所提到的,错误来自于您无法在工作线程中调用UI函数(例如findViewById)。请参阅https://stackoverflow.com/a/3875204/4706693因此,您在doInBackground中对Toast.makeText()的调用也将被禁止。