For some reason my application crashes when I try to set the text of a textview? How can I over come this.
private TextView strStatus;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
txtUsername = (EditText) findViewById(R.id.username);
strStatus = (TextView) findViewById(R.id.status);
//strStatus.setText("test"); // SEEMS TO BE CAUSING THE CRASH
setContentView(R.layout.main);
}
<TextView
android:id="#+id/status"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
One small mistake , You should do that setContentView first
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtUsername = (EditText) findViewById(R.id.username);
strStatus = (TextView) findViewById(R.id.status);
strStatus.setText("test");
}
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
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.
I'm trying to do an calculator app (school project), so i need a TextView for displaying the numbers. Because it's my first java project i wanted for the beginning to make a counter which displays how many times the button was clicked(just to learn how to work with TextViews). My problem is that after the first update of the TextView(when it is displaying "1") when i press the button for the second time it gives me a crash.
My code :
MainActivity.java
public class MainActivity extends AppCompatActivity {
public Integer number = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.no1);
ImageButton button = (ImageButton) findViewById(R.id.zeroB);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
number++;
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.no1);
tv.setText(number.toString());
}
});
}
...
content_main.xml
...
<TextView
android:layout_width="172dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/no1"
android:layout_gravity="left|top"
android:editable="true" />
...
Modify your code to this:
public class MainActivity extends AppCompatActivity {
public Integer number = 0;
//i would use this instead of the above
private int number = 0;
private TextView tv;
private ImageButton button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.no1);
button = (ImageButton) findViewById(R.id.zeroB);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
number++;
tv.setText(String.valueOf(number));
}
});
}
...
can u tell me why this code is not working? The app stops as soon as I click on Image(Imageview1). Debugger points to tv.setText(x);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TextView tv = (TextView)findViewById(R.id.textView1);
setContentView(R.layout.activity_main);
ImageView img = (ImageView) findViewById(R.id.imageView1);
img.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String x="You clicked on the image.";
tv.setText(x);
}
});
}
You need to get your UI elements after inflating your layout, otherwise findViewById returns null and hence the NPE is thrown when you try to set the text on your textView.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); /// I swap these two lines
final TextView tv = (TextView)findViewById(R.id.textView1); ///
ImageView img = (ImageView) findViewById(R.id.imageView1);
I have an android program that I am making with 4 seekbars, and 4 textviews to accompany each seekbar. Right now, I am trying to make each textview's contents equal to the integer progress of the seekbars. This is my code as it stands, and I have tried many ways to properly implement the OnSeekBarChangeListener, but LogCat still shows that it is null when sb1-sb4 sets their OnSeekBarChangeListener to OnSeekBarProgress. Please help me identify my problem :(
public class TippopotamusActivity extends Activity {
SeekBar sb1;
SeekBar sb2;
SeekBar sb3;
SeekBar sb4;
TextView tv1;
TextView tv2;
TextView tv3;
TextView tv4;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
tv1 = (TextView) findViewById(R.id.textView1);
tv2 = (TextView) findViewById(R.id.textView2);
tv3 = (TextView) findViewById(R.id.textView3);
tv4 = (TextView) findViewById(R.id.textView4);
sb1 = (SeekBar) findViewById(R.id.seekBar1);
sb2 = (SeekBar) findViewById(R.id.seekBar2);
sb3 = (SeekBar) findViewById(R.id.seekBar3);
sb4 = (SeekBar) findViewById(R.id.seekBar4);
sb1.setOnSeekBarChangeListener(OnSeekBarProgress);
sb2.setOnSeekBarChangeListener(OnSeekBarProgress);
sb3.setOnSeekBarChangeListener(OnSeekBarProgress);
sb4.setOnSeekBarChangeListener(OnSeekBarProgress);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
OnSeekBarChangeListener OnSeekBarProgress =
new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar s, int progress, boolean touch){
if(s.getId() == R.id.seekBar1)
{
tv1.setText(progress);
}
else if(s.getId() == R.id.seekBar2)
{
tv2.setText(progress);
}
else if(s.getId() == R.id.seekBar3)
{
tv3.setText(progress);
}
else
{
tv4.setText(progress);
}
}
public void onStartTrackingTouch(SeekBar s){
}
public void onStopTrackingTouch(SeekBar s){
}
};
Move your setContentView(R.layout.main); before initializing your variables. Your onCreate method should look like this
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv1 = (TextView) findViewById(R.id.textView1);
tv2 = (TextView) findViewById(R.id.textView2);
tv3 = (TextView) findViewById(R.id.textView3);
tv4 = (TextView) findViewById(R.id.textView4);
sb1 = (SeekBar) findViewById(R.id.seekBar1);
sb2 = (SeekBar) findViewById(R.id.seekBar2);
sb3 = (SeekBar) findViewById(R.id.seekBar3);
sb4 = (SeekBar) findViewById(R.id.seekBar4);
sb1.setOnSeekBarChangeListener(OnSeekBarProgress);
sb2.setOnSeekBarChangeListener(OnSeekBarProgress);
sb3.setOnSeekBarChangeListener(OnSeekBarProgress);
sb4.setOnSeekBarChangeListener(OnSeekBarProgress);
}