gpt4 book ai didi

android-TextView 的 setText() 上的 NullPointerException

转载 作者:太空宇宙 更新时间:2023-11-03 13:53:36 24 4
gpt4 key购买 nike

我是 android 的新手,我正在尝试向服务器发送请求并通过 api 从服务器获取数据。但是当我单击 loginbtn 时出现 NullPointerException 错误,除非 NullPointerException 错误

这是我的代码

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);
}
}

最佳答案

您没有像填充电子邮件、密码 EditText 那样填充错误文本 TextView。请给它充气。

尝试在 onCreate() 中添加:

errorText = (TextView)findViewById(R.id.your_error_textid);

希望对您有所帮助!

关于android-TextView 的 setText() 上的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32375316/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com