NullPointerException when calling GetText() - java

First off, I'm an Android noob and I'm making a simple app that takes text from an EditText widget and shows it in a TextView when you tap on a button. But when the button is clicked, it forcecloses and I get a NullPointerException in the logcat.
My activity code is:
package com.deltablue.freeearl.ama.ama;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button b1;
private EditText e1;
private TextView t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Definiciones de los elementos
Button b1 = (Button) findViewById(R.id.bttn1);
EditText e1 = (EditText) findViewById(R.id.edit1);
TextView t1 = (TextView) findViewById(R.id.txt1);
}
public void b1click(View view) {
Toast.makeText(this, "Pressed", Toast.LENGTH_SHORT).show();
String text = e1.getText().toString();
t1.setText("Input: ");
}
}
The thing is, when debugging, the findViewById() calls return correct values. But when b1click() is executed, e1 & t1 are null. All the other similar questions I've seen have to see with findViewById() returning null or wrong widget IDs, but that's not my case, AFAIK. I know it probably is some really stupid error, but I don't see where I'm wrong.
My layout is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.deltablue.freeearl.ama.ama.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/edit1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show text"
android:id="#+id/bttn1"
android:layout_gravity="center_horizontal"
android:onClick="b1click" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/txt1"
android:editable="true" />
</LinearLayout>
</RelativeLayout>
Thanks in advance for the help!

Here, in onCreate:
Button b1 = (Button) findViewById(R.id.bttn1);
EditText e1 = (EditText) findViewById(R.id.edit1);
TextView t1 = (TextView) findViewById(R.id.txt1);
... you're declaring three local variables. Those are completely separate from the fields that you declared earlier:
private Button b1;
private EditText e1;
private TextView t1;
I'm pretty sure you just wanted to change the values of the fields instead, so you just want to assign new values to the variables, rather than redeclaring them:
b1 = (Button) findViewById(R.id.bttn1);
e1 = (EditText) findViewById(R.id.edit1);
t1 = (TextView) findViewById(R.id.txt1);

Related

My android app stopping when I touch the button

This is my code guys when I touch the "saldir" button or another button my app
crashes and this is my app's image https://ibb.co/ce3ATm
This is my error (https://ibb.co/jBxb16):
Dergilik APP has stopped
package com.example.rg.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener
{
int sayac =0;
TextView tv;
**//kind of turkish words don't focus the variables**
TextView tvkarekterOzellikleri;
Button byemekye;
#Override
**//THIS IS MY MAIN_Activity.Java**
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.Answer);
//I'm trying very basic app when I touch button this layout'll changed
tvkarekterOzellikleri = (TextView) findViewById(R.id.Question);
Button bsaldir = (Button) findViewById(R.id.saldir);
//comment/i have 3 buttons as you can see on link at top
Button buyu = (Button) findViewById(R.id.uyu);
Button byemekye = (Button) findViewById(R.id.yemekYe);
bsaldir.setOnClickListener(this);
buyu.setOnClickListener(this);
byemekye.setOnClickListener(this);*
//This part useless now because I didn't added this part later I'll add this part
//Character k = new Character();
//k.Movementnum = 10;
//k.Kilos = 10;
//k.FightPower = 10;
//}
#Override
//This part is PROBLEM WHEN I DELETE THIS PART MY CODE IS WORKING
public void onClick(View v){
if (v.getId()==byemekye.getId())
tv.setText("yemek yenildi");
else
tv.setText("Nar" + (sayac++));
}
}
When I delete if.(v.getId....) app is running normal.
This side is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.rg.myapplication.MainActivity">
<Button
android:id="#+id/saldir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:elevation="0dp"
android:text="saldir" />
<TextView
android:id="#+id/Question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|bottom|center"
android:padding="3dp"
android:text="#string/Dilektasi"
android:textAppearance="#style/TextAppearance.AppCompat.Display2"
android:layout_above="#+id/saldir"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="75dp" />
<TextView
**android:id="#+id/Answer**"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/saldir"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:text="" />
//I have 3 button for my app as you can see on image
<Button
android:id="#+id/yemekYe"
**im saying twice pls dont focus on variables**
**next time i'll make this variables english**
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/saldir"
android:layout_toLeftOf="#+id/saldir"
android:layout_toStartOf="#+id/saldir"
android:text="yemek ye" />
smth smth smth smth
<Button
android:id="#+id/uyu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/saldir"
android:layout_toEndOf="#+id/saldir"
android:layout_toRightOf="#+id/saldir"
android:text="Uyu" />
//dont focus the variable names
</RelativeLayout>
I think your problem is this:
Define Buttons:
private Button bt;
Set id of the button on xml, than on onCrate()
bt= (Button) findViewById(R.id.button1); //button1 is the id of the button
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do actions
}
});
code:
public class YourActivity extends AppCompatActivity {
private Button btn1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_details);
parent_view = findViewById(android.R.id.content);
btn1= (Button) findViewById(R.id.button1); //button1 is the id of the button
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do actions
}
});
}
on xml
<Button
android:id="#+id/butto1" <!-- the id -->
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />

i get the Error: java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification

I don't know what is wrong with this code. I am trying to make a to do list app. I am new to Android and Java programming so please don't get mad if this is a simple errror.It gives me the error in the title.
Here is my XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lvItems"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:onClick="onAddItem"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/etNewItem"
android:hint="Enter a new item"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/button"
android:layout_toStartOf="#+id/button" />
</RelativeLayout>
Here is my Java:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends Activity {
private ArrayList<String> items;
private ArrayAdapter<String> itemsAdapter;
private ListView lvItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//EditText
final EditText etTask = (EditText) findViewById(R.id.etNewItem);
final String Task = etTask.getText().toString();
lvItems = (ListView) findViewById(R.id.lvItems);
items = new ArrayList<String>();
itemsAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
lvItems.setAdapter(itemsAdapter);
items.add("First Item");
items.add("Second Item");
//Button
Button btn = (Button) findViewById(R.id.button);
//Adding a Task
//OnClickListeners
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
items.add(etTask.getText().toString());
etTask.setText("");
}
});
}
}
Since you made a change to adapter after ListView was populated, Listview must be informed to repopulate the views with new values by calling notifydatasetchanged() on your adapter ie,
items.add(etTask.getText().toString());
((ArrayAdapter)lvItems.getAdapter()).notifydatasetchanged();
etTask.setText("");

"Unfortunately, <App Name> has stopped." Common unexplained Error

I am having a problem when debugging apps that respond to user input. An ordinary app that just displays text works fine, but when I give a button an event handler I get a generic message "Unfortunately, 'App Name' has stopped.". I have tried many solutions, but to no avail. I am using IntelliJ IDEA 12.0.4 (Community Edition).
Sorry for asking a very common question but it seems the solution is different for everyone.
Edit - new working code
Source Code:
package com.example.Android_Test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import static com.example.Android_Test.R.*;
public class MyActivity extends Activity {
EditText editText;
TextView textView;
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Declare all widget controls.
editText = (EditText) findViewById(R.id.txtEdit);
Button button = (Button) findViewById(R.id.btnChange);
textView = (TextView) findViewById(R.id.lblText);
button.setOnClickListener(listener);
}
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(editText.getText());
}
};
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/layout">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtEdit" android:layout_gravity="left|center_vertical"
android:layout_alignParentLeft="true" android:layout_marginLeft="0dp" android:layout_alignParentTop="true"
android:layout_marginTop="0dp" android:layout_alignParentRight="true" android:hint="Type here change text"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Text"
android:id="#+id/btnChange"
android:layout_alignLeft="#+id/txtEdit" android:layout_below="#+id/txtEdit"
android:layout_alignRight="#+id/txtEdit"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change this text"
android:id="#+id/lblText"
android:layout_alignLeft="#+id/txtEdit" android:layout_below="#+id/btnChange"
android:layout_alignRight="#+id/txtEdit" android:textSize="18dp"/>
</RelativeLayout>
Logcat is very long so I pasted it here: http://pastebin.com/2qaY8ZJj.
Everyone's Logcat is so much shorter i don't know why
Move this code
// Declare all widget controls.
EditText editText = (EditText) findViewById(R.id.txtEdit);
Button button = (Button) findViewById(R.id.btnChange);
TextView textView = (TextView) findViewById(R.id.lblText);
inside onCreate() and use R.id... .
Edit
I edited youre code
public class MyActivity extends Activity {
EditText editText;
TextView textView;
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Declare all widget controls.
editText = (EditText) findViewById(R.id.txtEdit);
Button button = (Button) findViewById(R.id.btnChange);
textView = (TextView) findViewById(R.id.lblText);
button.setOnClickListener(listener);
}
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(editText.getText().toString);
}
};
}

Unknown Error - Integer Parsing

My ultimate goal for this app is to make a sort of "tip book" for waiters/waitresses so they can record their tips they make each day. The app will display amount made, hours worked and hourly amount. It will also have an option to review all previous work days, showing daily, weekly and monthly totals.
I am trying to take the numbers input into a two EditText boxes in the main.xml file, id etTip and etHour, display them in two TextView, tvTip and tvHour, then divide the number in etTip by etHour to get the wage, which would then be displayed in tvWage after pressing the Submit button, bSubmit.
The tips and hours display themselves easily enough, no errors found when all the app is doing that, but when I attempt to use the Integer.parseInt(String string) command, I get an error message. If the parsing is done during the onCreate method, the error is immediate after opening the app. If the parsing is done after pressing the button, the app force closes quickly after pressing the button.
I've located the problem, but I just don't know how to go about fixing it.
Below is the main xml file:
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal">
<EditText
android:id="#+id/etTip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal"
android:layout_weight="50"
android:layout_margin="8dp"
android:hint="Tips">
<requestFocus />
</EditText>
<EditText
android:id="#+id/etHour"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal"
android:layout_weight="50"
android:layout_margin="8dp"
android:hint="Hours" >
<requestFocus />
</EditText>
</LinearLayout>
<Button
android:id="#+id/bSubmit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="#string/btSubmit"
android:textSize="35sp"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/tvTip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_weight="50"
android:text="Tips"
android:gravity="center"/>
<TextView
android:id="#+id/tvHour"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_weight="50"
android:text="Hours"
android:gravity="center"/>"
</LinearLayout>
<TextView
android:id="#+id/tvWage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="Wage"
android:gravity="center"/>
</LinearLayout>
And the main Activity:
package com.smarticle.tipbook;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class TipBookActivity extends Activity {
/** Called when the activity is first created. */
TextView textTip,textHour,textWage;
EditText editHour,editTip;
int tip,wage,hour;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textTip = (TextView) findViewById(R.id.tvTip);
textHour = (TextView) findViewById(R.id.tvHour);
textWage = (TextView) findViewById(R.id.tvWage);
editTip = (EditText) findViewById(R.id.etTip);
editHour = (EditText) findViewById(R.id.etHour);
tip = Integer.parseInt(textTip.getText().toString());
hour = Integer.parseInt(textHour.getText().toString());
Button bSubmit = (Button) findViewById(R.id.bSubmit);
bSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
textTip.setText(editTip.getText());
textHour.setText(editHour.getText());
wage = tip / hour;
textWage.setText(wage);
}
});
}
}
It's just because you parsed to int your edit text content at load which is why the first value is null which is why it can't be parsed into integer. That is why I just placed the parse part inside the onClick for the content of the editview to be converted after clicking. Also change your edit view to accept only numeric so that it won't fail in case a user input an invalid characters. :) There you go.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textTip = (TextView) findViewById(R.id.tvTip);
textHour = (TextView) findViewById(R.id.tvHour);
textWage = (TextView) findViewById(R.id.tvWage);
editTip = (EditText) findViewById(R.id.etTip);
editHour = (EditText) findViewById(R.id.etHour);
//int tip = Integer.parseInt((String) textTip.getText());
//int hour = Integer.parseInt((String) textHour.getText());
Button bSubmit = (Button) findViewById(R.id.bSubmit);
bSubmit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
textTip.setText(editTip.getText());
textHour.setText(editHour.getText());
wage = Integer.parseInt(textTip.getText().toString()) / Integer.parseInt(textHour.getText().toString());
textWage.setText(String.valueOf(wage));
}
});
}
}
Use double instead:
double tip,wage,hour;
And,
tip = parseDouble(textTip.getText().toString());
hour = parseDouble(textHour.getText().toString());
Also, you should check that the input in EditText is actually a number before parsing. And you should enclose the parsing in try/catch block.

.setText() crashes my android app

I have tried many other questions on this site today but none of them have been helpful, i seem to have identical syntax that works for other people but every time i hit send (when i want my text fields to update) my app just crashes, I have tried a bunch of different syntax for the setText() method but nothing has worked.
Thanks for any help in advance
this is my main class
package com.example.myapp;
import android.app.Activity;
import java.io.IOException;
import java.util.ArrayList;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class TestprojectsActivity extends Activity {
private int index;
private int QsClicked = 0;
private ArrayList<String> categories = new ArrayList<String>();
private ArrayList<Button> buttons = new ArrayList<Button>();
Button b1;
Button b2;
Button b3;
Button b4;
Button changer;
private int Qselected;
TextView txt;
EditText edit;
private ArrayList<String> questions = new ArrayList<String>();
private int qnum = 0;
// updates all the buttons so we can see more categories
public void updateButtons(View v){
for(int i = 0; i < 4; i++){
if(index >= categories.size()){
index = 0;
}
buttons.get(i).setText(categories.get(index));
index++;
}
}
public void Q1(View v){
setContentView(R.layout.sub);
Qselected = index;
//send(null);
}
public void Q2(View v){
setContentView(R.layout.sub);
Qselected = (index+1);
//send(null);
}
public void Q3(View v){
setContentView(R.layout.sub);
Qselected = (index+2);
//send(null);
}
public void Q4(View v){
setContentView(R.layout.sub);
Qselected = (index+3);
//send(null);
}
public void send(View v){
edit.setText("WHY ISNT THIS WORKING?");
if(qnum < questions.size()){
txt.setText("i work!");
qnum++;
}
else{
qnum = 0;
System.exit(0);
}
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
index = 0;
b1 = (Button) findViewById(R.id.B1);
buttons.add(b1);
b2 = (Button) findViewById(R.id.B2);
buttons.add(b2);
b3 = (Button) findViewById(R.id.B3);
buttons.add(b3);
b4 = (Button) findViewById(R.id.B4);
buttons.add(b4);
txt = (TextView) findViewById(R.id.textView1);
edit = (EditText) findViewById(R.id.editText2);
categories.add("dumb questions");
categories.add("smart questions");
questions.add("hi there, what is your name?");
questions.add("what did you have for breakfast today?");
questions.add("how old are you?");
questions.add("what color is your hair?");
updateButtons(null);
}
}
this is my main xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutrowtop"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/B4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="B4"
android:onClick="Q4" />
<Button
android:id="#+id/B3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="B3"
android:onClick="Q3" />
<Button
android:id="#+id/B2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="B2"
android:onClick="Q2" />
<Button
android:id="#+id/B1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="B1"
android:onClick="Q1" />
<Button
android:id="#+id/Button05"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="More Categories"
android:onClick="updateButtons" />
</LinearLayout>
this is my sub xml (i use it so i can have another layout)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.22"
android:text="Question"
/>
<EditText
android:id="#+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.51"
android:ems="10"
android:inputType="text" />
<Button
android:id="#+id/send"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.08"
android:text="Send"
android:onClick="send" />
</LinearLayout>
Your activity is using main.xml as its layout, and that does not have a TextView with id editText2. Just putting such a view in sub.xml doesn't create the view; the layout file needs to be inflated and set as the content view for the activity. Call findViewById after you change layouts and all should be well.
AS in when Oncreate get call main.xml was you layout xml and it has not R.id.editText2 so at that time edit would be null..........
Put this line
edit = (EditText) findViewById(R.id.editText2);
in
public void Q1(View v){
setContentView(R.layout.sub);
Qselected = index;
edit = (EditText) findViewById(R.id.editText2); <-----------------here in Q1 Q2 Q3 Q4
//send(null);
}
In the activity's onCreate method you set the content view to R.layout.main and you search for the TextView that you want to set the text. Because the TextView is not in that layout(is in the R.layout.sub and not in the R.layout.main) it will be null and you'll get a NullPointerException when you set the text, although you set the content view as the new layout that contains that TextView(you already search for it in the onCreate method so the reference is initialized with null).
The solution is to search for the TextView after you set the new content view(R.layout.sub), for example:
public void send(View v){
txt = (TextView) findViewById(R.id.textView1);
edit = (EditText) findViewById(R.id.editText2);
edit.setText("WHY ISNT THIS WORKING?");
if(qnum < questions.size()){
txt.setText("i work!");
qnum++;
}
else{
qnum = 0;
System.exit(0);
}
}
Ans the same thing goes for that Edittext.

Categories