Java Android - 在null对象引用上的void android.widget.TextView.setText(java.lang.CharSequence)'

时间:2016-08-12 22:21:45

标签: java android sockets

我不知道为什么会这样。有人可以帮帮我吗?

客户类:

public class Cliente extends AsyncTask<Void,String,Void> {

    String dstAddress;
    int dstPort;
    TextView x_atual,y_atual,x_desejado,y_desejado;
    Scanner r;
    String[] valores = new String[4];

    public Cliente(String addr, int port) {
        //super();
        dstAddress = addr;
        dstPort = port;
    }

    public Cliente(TextView x_atual,TextView y_atual, TextView x_desejado, TextView y_desejado) {
        //super();
        this.x_atual = x_atual;
        this.y_atual = y_atual;
        this.x_desejado = x_desejado;
        this.y_desejado = y_desejado;
    }

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

        Socket socket = null;

        try {
            socket = new Socket(dstAddress, dstPort);

            r = new Scanner(new InputStreamReader(socket.getInputStream()));

        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        valores[0] = r.nextLine();
        valores[1] = r.nextLine();
        valores[2] = r.nextLine();
        valores[3] = r.nextLine();
        publishProgress(valores[0],valores[1],valores[2],valores[3]);
    try {
    Thread.sleep(60);
     } catch (InterruptedException e) {
    e.printStackTrace();
    }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        x_atual.setText(valores[0]);
        y_atual.setText(valores[1]);
        x_desejado.setText(valores[2]);
        y_desejado.setText(valores[3]);
       super.onPostExecute(result);
    }

     }

的活动:

public class SecondActivity extends AppCompatActivity {

TextView x_atual,y_atual,x_desejado,y_desejado;
Cliente myClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    x_atual = (TextView) findViewById(R.id.x_atual);
    y_atual = (TextView) findViewById(R.id.y_atual);
    x_desejado = (TextView) findViewById(R.id.x_desej);
    y_desejado = (TextView) findViewById(R.id.y_desej);

    x_atual.setText("0");
    y_atual.setText("0");
    x_desejado.setText("0");
    y_desejado.setText("0");

    myClient = new Cliente(x_atual,y_atual,x_desejado,y_desejado);
    myClient.execute();
}




}
  

处理:com.example.tiago.java_android,PID:8129                                                                                 java.lang.NullPointerException:尝试调用虚方法'void   android.widget.TextView.setText(java.lang.CharSequence)'为null   对象参考                                                                                     在   com.example.tiago.java_android.Cliente.onPostExecute(Cliente.java:73)                                                                                     在   com.example.tiago.java_android.Cliente.onPostExecute(Cliente.java:18)                                                                                     在android.os.AsyncTask.finish(AsyncTask.java:636)                                                                                     在android.os.AsyncTask.access $ 500(AsyncTask.java:177)                                                                                     在   android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:653)                                                                                     在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                     在android.os.Looper.loop(Looper.java:135)                                                                                     在android.app.ActivityThread.main(ActivityThread.java:5431)                                                                                     at java.lang.reflect.Method.invoke(Native Method)                                                                                     在java.lang.reflect.Method.invoke(Method.java:372)                                                                                     在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:914)                                                                                     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)

0 个答案:

没有答案