android-TextView 的 setText() 上的 NullPointerException

标签 android api nullpointerexception request http-post

我是 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/

相关文章:

android - 如何将标题添加到 recyclerview android 中的部分?

android - 是否可以通过 Google Assistant 测试我的应用操作集成 Slice?

java.lang.RuntimeException : Failure delivering result ResultInfo when capturing image 错误

javascript - 使用 Google Feeds API 使用 "tap"在 jquery mobile 中自动生成 feed 列表

javascript - 在多个客户端同步播放 YouTube 视频

Java Swing 空指针异常

java - 我想将一个数字拆分为一位数字并将其更改为android中的二进制

php - 强制下载YouTube视频

java - 将类实例(图像)放入数组列表中(空指针异常)

android - BoringLayout.isBoring 的 NullPointerException 一点也不无聊