我一直收到这个错误:
04-05 11:38:39.168 14995-14995/com.example.orestis.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.orestis.myapplication, PID: 14995
java.lang.NullPointerException
at com.example.orestis.myapplication.signup$1.onClick(signup.java:50)
at android.view.View.performClick(View.java:4633)
at android.view.View$PerformClick.run(View.java:19330)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5506)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
每次InputStreamReader inputStreamReader = new InputStreamReader(client.getInputStream());
我对这些东西都是新手,所以我真的不知道发生了什么,会非常感谢你的帮助:)
package com.example.orestis.myapplication;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class signup extends ActionBarActivity {
private EditText username;
private EditText password;
private Button button;
private String messsage;
private Socket client;
private PrintWriter printwriter;
private BufferedReader bufferedReader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
username = (EditText) findViewById(R.id.editText1); // reference to the text field
password = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button1); // reference to the send button
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
messsage = "SIGNUP-"+username.getText().toString() + "-" + password.getText().toString(); // get the text message on the text field
username.setText(""); // Reset the text field to blank
password.setText("");
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
resp: while(true){
try {
InputStreamReader inputStreamReader = new InputStreamReader(client.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader);
if (bufferedReader.ready()) {
String serverReply = bufferedReader.readLine();
if(serverReply.equals("SUCCESS")){
//get into game
break resp;
}else if(serverReply.equals("toast:Username exists")){
Toast toast = Toast.makeText(getApplicationContext(),"Username exists!", Toast.LENGTH_SHORT);
toast.show();
break resp;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
private class SendMessage extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
client = new Socket("IPGOESHERE", 4444); // connect to the server
printwriter = new PrintWriter(client.getOutputStream(), true);
printwriter.write(messsage); // write the message to output stream
printwriter.flush();
//printwriter.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_signup, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}