Android app compiled but not working - java

I have made an android app named Yash on AIDE. It has an edittext,textview & a button. It is designed to first take input through the edittext then when we press the button, it should display the edittext text on the textview. It starts & takes the input but as I press the button, it hangs & shows:"Unfortunately Yash has stopped."
Xml:
<?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:gravity="center"
android:orientation="vertical" >
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/oButton"/>
/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Go!"
android:onClick="bc"
android:id="#+id/goButton"></Button>
</LinearLayout>
Java:
package com.mycompany.myapp4;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.Toast;
import android.widget.EditText;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void bc(View view){
EditText t = (EditText) findViewById(R.id.editText);
String s = t.getText().toString();
TextView textView = (TextView) findViewById(R.id.oButton);
textView.setText(s);
}
}

The following line
String s = t.toString();
won't work like expected, because this way 's' is the String representation of the EditText object 't'. Most likely you meant to write
String s = t.getText().toString();

Make sure you have declared your activity in AndroidManifest.xml

try on this way.
public class MainActivity extends Activity {
EditText t;
TextView textView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initilization();
}
public void initilization() {
// TODO Auto-generated method stub
t = (EditText) findViewById(R.id.editText);
textView = (TextView) findViewById(R.id.oButton);
String s = t.getText().toString();
if (!TextUtils.isEmpty(s)) {
textView.setText(s);
}else {
// check log its null
}
}
}

Related

How to increase the value by one each time i click switching the activity back and forth in Android Studio

I am trying to increase the value of variable count by one each time i click on the button to switch the activity back and forth. However, each time i clicked, the count value is still the same. Can someone kindly help ?
I am trying to put some life-cycle android code, but it still does not make any difference.
package com.darayuth.learning;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.darayuth.learning";
public static final String TAG = "MainActivity";
public static final String value = "value";
private int count = 0;
private TextView textView;
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt(value, count);
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
count = savedInstanceState.getInt(value);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView2);
Button btn = findViewById(R.id.button);
btn.setOnClickListener((View v)->{
sendMessage();
});
}
public void sendMessage(){
count = count + 1;
Log.i(TAG, "value: " + count);
Intent intent = new Intent(this, Main2Activity.class);
EditText editText = findViewById(R.id.editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message );
startActivity(intent);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:text="Button" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
</LinearLayout>
activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".Main2Activity">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
package com.darayuth.learning;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = findViewById(R.id.textView1);
textView.setText(message);
}
}
I would suggest using SharedPreferences to store and retrieve the value between your activities.
private void storeValue(){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
// Store an integer using the Shared Preferences editor
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("myPreferenceValueKey", value); // Store the value with a key to identify it
editor.apply();
}
private void retrieveValue(){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
int defaultValue = 0; // This is the value that's returned if the preference doesn't exist
// Retrieve the stored value using the associated key
value = prefs.getInt("myPreferenceValueKey", defaultValue);
}
In your first Activity, just call retrieveValue() in your onCreate() then call storeValue() after incrementing the value but before starting the next Activity.
You can create the same functions in your second Activity to store and retrieve the value as needed.
You can learn more about SharedPreferences at https://developer.android.com/training/data-storage/shared-preferences
I suggest you use a class with a static variable to hold the count variable. And add a function increment() to increase the count.

Adding a button to activity that opens a browser URL

I want to add a button to one of my activities (that displays a news article), so when the user clicks the button the article is opened in their browser. So far I have added the button in my xml and it appears. I'm just having some trouble with the click listener. Below is my code, i'm getting an error for 'setOnClickListener' which is 'Non-static method 'setOnClickListener(android.view.View.onClickListener)' cannot be referenced from a static content.
I'm not sure what this means! maybe i'm not calling the method in the right place or there is just an error with the method itself?
Please could someone take a look, thanks!
import android.content.Intent;
import android.media.Image;
import android.net.Uri;
import android.support.v4.app.ShareCompat;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ShareActionProvider;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.View.OnClickListener;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.NetworkImageView;
import org.json.JSONException;
import org.json.JSONObject;
public class NewsItemActivity extends AppCompatActivity {
//Declare java object for the UI elements
private TextView itemTitle;
private TextView itemDate;
private TextView itemContent;
private NetworkImageView itemImage;
private ShareActionProvider mShareActionProvider;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_item);
itemTitle = (TextView) findViewById(R.id.itemTitle);
itemDate = (TextView) findViewById(R.id.itemDate);
itemContent = (TextView) findViewById(R.id.itemContent);
itemImage = (NetworkImageView) findViewById(R.id.itemImage);
EDANewsApp app = EDANewsApp.getInstance();
Intent intent = getIntent();
int itemId = intent.getIntExtra("newsItemId", 0);
String url = "http://www.efstratiou.info/projects/newsfeed/getItem.php?id="+itemId;
JsonObjectRequest request = new JsonObjectRequest(url, listener, errorListener);
app.requestQueue.add(request);
Button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = getIntent();
int itemId = intent.getIntExtra("newsItemId", 0);
String shareUrl = "http://www.efstratiou.info/projects/newsfeed/getItem.php?id=" + itemId;
Intent buttonIntent = new Intent();
buttonIntent.setAction(Intent.ACTION_VIEW);
buttonIntent.addCategory(Intent.CATEGORY_BROWSABLE);
buttonIntent.setData(Uri.parse(shareUrl));
startActivity(buttonIntent);
}
});
};
activity_news_item.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"
tools:context=".NewsItemActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/itemTitle"
android:layout_margin="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#3183b9"/>
<TextView
android:id="#+id/itemDate"
android:layout_marginLeft="15dp"
android:layout_marginBottom="15dp"
android:textSize="16sp"
android:textStyle="italic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/itemImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/itemContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20sp"
android:textSize="16sp"
/>
<Button
android:id="#+id/BrowserButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View in browser"
android:layout_marginLeft="15dp"
style="#style/BrowserButton"
/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Change
Button.setOnClickListener(new View.OnClickListener() { ... });
to
Button button = (Button) findViewById(R.id.BrowserButton);
button.setOnClickListener(new View.OnClickListener() { ... });
You need to call the setOnClickListener method on an instance of Button, not on the class itself.
The problem is this :
Button.setOnClickListener(new View.OnClickListener() {
You have to declare it out of onCreate() as you did with your Textviews as :
Button btn;
Why?
Because if you put it on onCreate() you wont be able to use this object out of onCreate() so it's recomendable to put it as a public object.
Then in your onCreate() do this :
btn = (Button)findViewById(R.id.BrowserButton);
Then you do the OnclickListener() as follows :
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = getIntent();
int itemId = intent.getIntExtra("newsItemId", 0);
String shareUrl = "http://www.efstratiou.info/projects/newsfeed/getItem.php?id=" + itemId;
Intent buttonIntent = new Intent();
buttonIntent.setAction(Intent.ACTION_VIEW);
buttonIntent.addCategory(Intent.CATEGORY_BROWSABLE);
buttonIntent.setData(Uri.parse(shareUrl));
startActivity(buttonIntent);
}
});
I'm still getting an error for onClickListener, saying it 'cannot resolve symbol'.
Make sure that you've imported this :
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
You can do it doing this :
Add implements OnClickListener { as follows :
public class NewsItemActivity extends AppCompatActivity implements View.OnClickListener {
Then on your Button you do this :
btn = (Button)findViewById(R.id.BrowserButton);
btn.setOnClickListener(this);
Then add a switch case as follows :
public void onClick(View v) {
switch(v.getId()) {
case R.id.BrowserButton:
Intent intent = getIntent();
int itemId = intent.getIntExtra("newsItemId", 0);
String shareUrl = "http://www.efstratiou.info/projects/newsfeed/getItem.php?id=" + itemId;
Intent buttonIntent = new Intent();
buttonIntent.setAction(Intent.ACTION_VIEW);
buttonIntent.addCategory(Intent.CATEGORY_BROWSABLE);
buttonIntent.setData(Uri.parse(shareUrl));
startActivity(buttonIntent);
}
break;
}
}

Cannot find symbol id in button

Hello i am making a simple push button in android and this is my mainactivity file
package dk.troll.android;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.troll.dk"));
startActivity(browserIntent);
}
});
}
}
and this is my xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Lorem Ipsum and so much other stuff"
/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button - Go To Troll" />
</LinearLayout>
On this line button = (Button) findViewById(R.id.button1); i get the error
cannot find symbol:variable id location:class R
Since the contents of class
r
are auto-generated,how can i fix the error?
fist restart eclipse
then doing this :
1.import R class to your activity, Import dk.troll.android.R;
2.be sure the all recurse file have not any error then try to clean project from Project then Clean.
3.build your project

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

.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