我是android的新手,我正在尝试向服务器发送请求并通过api从中获取数据。但是当我点击loginbtn时,我发现NullPointerException错误除非NullPointerException错误< / p>
这是我的代码
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
public class DisplayLoginActivity extends AppCompatActivity {
TextView errorText;
EditText email, password;
String Email, Password;
@Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_login);
email = (EditText)findViewById(R.id.emailText);
password = (EditText)findViewById(R.id.passwordText);
Button login_btn=(Button)findViewById(R.id.loginbtn);
login_btn.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v)
{
try{
// CALL GetText method to make post method call
loginAction();
}
catch(Exception ex)
{
errorText.setText("url! " );
}
}
});
}
// Create GetText Metod
public void loginAction() throws UnsupportedEncodingException
{
// Get user defined values
Email = email.getText().toString();
Password = password.getText().toString();
// Create data variable for sent values to server
String data = URLEncoder.encode("email", "UTF-8")
+ "=" + URLEncoder.encode(Email, "UTF-8");
data += "&" + URLEncoder.encode("password", "UTF-8")
+ "=" + URLEncoder.encode(Password, "UTF-8");
String text = "";
BufferedReader reader=null;
// Send data
try
{
// Defined URL where to send data
URL url = new URL("http://media-clouds.net/pharms/apilogin");
// Send POST data request
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write( data );
wr.flush();
// Get the server response
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
// Append server response in string
sb.append(line + "\n");
}
text = sb.toString();
}
catch(Exception ex)
{
errorText.setText( "234" );
}
finally
{
try
{
reader.close();
}
catch(Exception ex) {
errorText.setText( "234" );
}
}
// Show response on activity
errorText.setText( text );
}
@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);
}
}
答案 0 :(得分:3)
您没有像填写电子邮件密码TextView
那样夸大errorText EditText
。请夸大它。
尝试在onCreate()
:
errorText = (TextView)findViewById(R.id.your_error_textid);
我希望它有所帮助!