This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I have resolved this before but I can't remember what I did. Basically its a simple EditText and finding it's view by id but i get the error messagejava.lang.NullPointerException when running the app.
public class MainActivity extends Activity {
CallbackManager callbackManager;
public static final String SECRET_KEY = "65EB1D3A-F8D8-D6DC-FF21-E111F9409600";
public static final String APP_ID = "F5931D23-2392-1B65-FF65-BC4256BB9300";
RegisterAccount register;
EditText mName = (EditText) findViewById(R.id.userName);
EditText mEmail = (EditText) findViewById(R.id.userEmail);
EditText mPassword = (EditText) findViewById(R.id.userPassword);
EditText mConfirmPassword = (EditText) findViewById(R.id.userConfirmPassword);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
Backendless.initApp(MainActivity.this, APP_ID, SECRET_KEY, "v1");
BackendlessUser user = new BackendlessUser();
user.setEmail(mEmail.getText().toString());
user.setPassword(mPassword.getText().toString());
Backendless.UserService.register(user, new AsyncCallback<BackendlessUser>() {
#Override
public void handleResponse(BackendlessUser backendlessUser) {
Intent logIntent = new Intent(MainActivity.this, ContentActivity.class);
logIntent.putExtra("Name", mEmail.getText().toString());
startActivity(logIntent);
}
#Override
public void handleFault(BackendlessFault backendlessFault) {
// On failure
}
});
Put the following lines:
EditText mName = (EditText) findViewById(R.id.userName);
EditText mEmail = (EditText) findViewById(R.id.userEmail);
EditText mPassword = (EditText) findViewById(R.id.userPassword);
EditText mConfirmPassword = (EditText) findViewById(R.id.userConfirmPassword);
below: setContentView(R.layout.activity_main); in your onCreate() method.
You first load your XML view using setContentView() method and than you instantiate your view objects.
Related
public class MainActivity extends AppCompatActivity {
EditText EditText1,EditText2,EditText3,EditText4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText1 = findViewById(R.id.edittext);
EditText2 = findViewById(R.id.edittext);
EditText3 = findViewById(R.id.edittext);
EditText4 = findViewById(R.id.edittext);
addChildLayout();
addChildLayout();
addChildLayout();
addChildLayout();
}
public void addChildLayout(){
LayoutInflater layoutInfralte=(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayout=(LinearLayout)findViewById(R.id.infolayout);
View view=layoutInfralte.inflate(R.layout.activity_main_layout, null);
linearLayout.addView(view);
}
}
how do I select the EditText, from all the four view and display them , any ideas? what can i do here ?
I want to get the data from each of the 4 EditTexts
public class MainActivity extends AppCompatActivity {
EditText EditText1,EditText2,EditText3,EditText4;
List<EditText> list=new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText1 = findViewById(R.id.edittext);
EditText2 = findViewById(R.id.edittext);
EditText3 = findViewById(R.id.edittext);
EditText4 = findViewById(R.id.edittext);
addChildLayout();
addChildLayout();
addChildLayout();
addChildLayout();
}
public void addChildLayout(){
LayoutInflater layoutInfralte=(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayout=(LinearLayout)findViewById(R.id.infolayout);
View view=layoutInfralte.inflate(R.layout.activity_main_layout, null);
EditText inflatededitText=(EditText)view.findViewById(R.id.youredittextid);
list.add(inflatededitText);
linearLayout.addView(view);
}
}
Now you can get any edittext like below
list.get(0).getText.toString
here 0 is position of edittext
if u need all edittext value then use for loop like this
for(int i=0;i<list.length;i++){
Log.d("value","list.get(i).getText.toString");
}
Use .getText() on each editText reference
I'm a beginner, I did a simple task but I do not see the result in TextView. How can I solve my problem?
Code:
public class MainActivity extends AppCompatActivity {
Button button;
EditText editText1, editText2;
TextView textView;
public void Add(View v)
{
float x = Float.parseFloat(editText1.getText().toString());
float y = Float.parseFloat(editText2.getText().toString());
float sum = x + y;
textView.setText(String.valueOf(sum));
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
editText1 = findViewById(R.id.editText);
editText2 = findViewById(R.id.editText2);
textView = findViewById(R.id.textView);
}
}
The second option I tried was:
textView.setText("SUM = " + String.valueOf(sum));
But then I receive a message:
Do not concatenate text displayed with setText. Use resource string with placeholders.
PS. Of course i added method android:onClick = "Add"
You can try sum.toString() instead of String.valueOf(sum)
This will do your work
textView.setText(Float.toString(sum))
you better make button onclick in java, then display the string value to the textview
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handleButtonClick();
}
private void handleButtonClick() {
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
addAndSetResult();
}
});
}
private void addAndSetResult() {
// inside here do the add logic and set result to textview
EditText editText1 = (EditText) findViewById(R.id.editText1);
EditText editText2 = (EditText) findViewById(R.id.editText2);
float x = Float.parseFloat(editText1.getText().toString());
float y = Float.parseFloat(editText2.getText().toString());
float sum = x + y;
String sumStr = String.valueOf(sum);
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(sumStr);
}
I'am new to Android Studio, here is my problem, i used some other solutions but they were not useful. i don't know what is the problem with that.
Code is given below,
public class SignUpActivity extends AppCompatActivity {
EditText etName;
EditText etEmail;
EditText etPass;
RadioButton radioButton1;
RadioButton radioButton2;
RadioGroup radioGroup;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
Button sgButton = findViewById(R.id.sgButton);
etEmail = findViewById(R.id.input_Email);
etPass = findViewById(R.id.input_password);
etName = findViewById(R.id.inputName);
radioButton1 = findViewById(R.id.f);
radioButton2 = findViewById(R.id.m);
radioGroup = findViewById(R.id.radioGroup);
sgButton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
final String name = etName.getText().toString();
final String email = etEmail.getText().toString();
final String pass = etPass.getText().toString();
Toast toast = Toast.makeText(SignUpActivity.this,
"Crunch you felt it", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_HORIZONTAL,0,0);
toast.show(); // here is the Code for Toast.
}
}
);
}
}
Any help will be appreciated.
this is main activity java class
public class MainActivity extends AppCompatActivity implements AsyncResponse,View.OnClickListener {
EditText etusername,etpassword;
Button btnlogin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etusername = (EditText) findViewById(R.id.etusername);
etpassword = (EditText) findViewById(R.id.etpassword);
btnlogin = (Button) findViewById(R.id.btnlogin);
btnlogin.setOnClickListener(this);
}
#Override
public void processFinish(String result) {
Toast.makeText(this,result,Toast.LENGTH_LONG).show();
}
#Override
public void onClick(View v) {
HashMap postData = new HashMap();
postData.put("txtUsername", etusername.getText().toString());
postData.put("txtPassword", etpassword.getText().toString() );
PostResponseAsyncTask task = new PostResponseAsyncTask(this, (AsyncResponse) postData);
task.execute("http://10.0.3.2/client/login.php");
}
}
in xml file i have three fields.. username password and login button.
after clicking button , it says app currently stopped!
im using android phone t run my app!
I went through the library which u provided,U need to Change it Like this :
public class MainActivity extends AppCompatActivity implements AsyncResponse,View.OnClickListener {
EditText etusername,etpassword;
Button btnlogin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etusername = (EditText) findViewById(R.id.etusername);
etpassword = (EditText) findViewById(R.id.etpassword);
btnlogin = (Button) findViewById(R.id.btnlogin);
btnlogin.setOnClickListener(this);
}
#Override
public void processFinish(String result) {
Toast.makeText(this,result,Toast.LENGTH_LONG).show();
}
#Override
public void onClick(View v) {
HashMap postData = new HashMap();
postData.put("txtUsername", etusername.getText().toString());
postData.put("txtPassword", etpassword.getText().toString() );
PostResponseAsyncTask task = new PostResponseAsyncTask(this,postData,this);
task.execute("http://10.0.3.2/client/login.php");
}
}
This question already has answers here:
The method getText() is undefined for the type String error
(3 answers)
Closed 8 years ago.
Here is my code. I'm receiving The method getText() is undefined for the type String and cannot solve it by my own.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
EditText ETtitle = (EditText) findViewById(R.id.ETtitle);
EditText ETcontent = (EditText) findViewById(R.id.ETcontent);
button.setOnClickListener(new buttonOnClickListener());
}
public class buttonOnClickListener implements View.OnClickListener {
public void onClick(View v) {
String ETtitle = ETtitle.getText().toString();
}
}
}
you are redefining ETtitle locally
change
String ETtitle = ETtitle.getText().toString();
into
String editTextContent = ETtitle.getText().toString();
change
String ETtitle = ETtitle.getText().toString();
to
String ETtitleStr = ETtitle.getText().toString();
because ETtitle already defined as edittext
and define EditText ETtitle as class member.
i.e. simply rewrite your code as
public class MainActivity extends Activity {
private EditText ETtitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
ETtitle = (EditText) findViewById(R.id.ETtitle);
EditText ETcontent = (EditText) findViewById(R.id.ETcontent);
button.setOnClickListener(new buttonOnClickListener());
}
public class buttonOnClickListener implements View.OnClickListener {
public void onClick(View v) {
String ETtitleStr = ETtitle.getText().toString();
}
}
}