O1DMBFan O1DMBFan - 2 months ago 6
Android Question

Buttons not working after ContentView change

Thanks for taking the time to look into my problem with me. I'm very new to Android Development and this is my first application attempt. Any and all help would be appreciated. All advice is needed advice at this point.

Main issue at hand is the btnBack and btnSubmit are not triggering an onClick event. Please advise.

helloformstuff.java

package com.uDrew.helloformstuff;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
public class helloformstuff extends Activity implements OnClickListener {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Log.i("DrewDebug", "Loading: layout.main");

setContentView(R.layout.main);

Log.i("DrewDebug", "Loaded: layout.main");

Log.i("DrewDebug", "Loading: Drunk Spinner");

//Drunk Spinner
Spinner spLevel = (Spinner) findViewById(R.id.spLevel);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.drunk_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spLevel.setAdapter(adapter);

Log.i("DrewDebug", "Loaded: Drunk Spinner");

Log.i("DrewDebug", "Listening for Buttons");
//Listen for button clicks
Button btnNext = (Button) this.findViewById(R.id.btnNext);
Button btnSubmit = (Button) this.findViewById(R.id.btnSubmit);
Button btnBack = (Button) this.findViewById(R.id.btnBack);

if (btnNext!= null) {
//If btnNext was hit
btnNext.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

Log.i("DrewDebug", "Button Hit: Next");
//Log the beginning of this code
Log.i("DrewDebug", "BEGIN Next Code");

//Change to second Screen
setContentView(R.layout.second);

//Initialize State Spinner
Spinner spState = (Spinner) findViewById(R.id.spState);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
helloformstuff.this, R.array.state_array, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spState.setAdapter(adapter2);

//Log the end of the this code
Log.i("DrewDebug", "END Next Code");
}
});
}

if (btnBack != null) {
//If btnBack was hit
btnBack.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Log.i("DrewDebug", "Button Hit: Back");
//Log the beginning of this code
Log.i("DrewDebug", "BEGIN Back Code");
//Change to first screen
setContentView(R.layout.main);

//Drunk Spinner
Spinner spLevel = (Spinner) findViewById(R.id.spLevel);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
helloformstuff.this, R.array.drunk_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spLevel.setAdapter(adapter);

//Log the end of the this code
Log.i("DrewDebug", "END Back Code");
}
});
}
}
}


main.xml



<EditText
android:id="@+id/txtQuote"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Quote" />

<TextView
android:id="@+id/lblWho"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/txtQuote"
android:text="Who Said It?"/>

<EditText
android:id="@+id/txtFirst"
android:layout_width="250px"
android:layout_height="wrap_content"
android:layout_below="@id/lblWho"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dip"
android:text="Anonymous" />

<EditText
android:id="@+id/txtLast"
android:layout_width="50px"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/txtFirst"
android:layout_alignTop="@id/txtFirst"
android:text="X"/>

<Spinner
android:id="@+id/spLevel"
android:prompt="@string/drunk_prompt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/txtFirst"
android:layout_alignParentLeft="true"/>

<Button
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/spLevel"
android:layout_alignParentRight="true"
android:text="Next"/>




second.xml



<TextView
android:id="@+id/lblShare"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Who's Sharing?"/>

<EditText
android:id="@+id/txtFirstShare"
android:layout_width="250px"
android:layout_height="wrap_content"
android:layout_below="@id/lblShare"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dip"
android:text="Anonymous" />

<EditText
android:id="@+id/txtLastShare"
android:layout_width="50px"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/txtFirstShare"
android:layout_alignTop="@id/txtFirstShare"
android:text="X"/>

<EditText
android:id="@+id/txtCity"
android:layout_width="250px"
android:layout_height="wrap_content"
android:layout_below="@id/txtLastShare"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dip"
android:text="NoWhere"/>

<Spinner
android:id="@+id/spState"
android:prompt="@string/state_prompt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/txtCity"
android:layout_alignTop="@id/txtCity"/>

<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/spState"
android:layout_alignParentRight="true"
android:text="Submit"/>

<Button
android:id="@+id/btnBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/btnSubmit"
android:layout_alignParentLeft="true"
android:layout_alignTop="@id/btnSubmit"
android:text="Back"/>




string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Random Drunk Quotes</string>
<string name="drunk_prompt">Level of Buzz</string>
<string-array name="drunk_array">
<item>One Beer Syndrome</item>
<item>Buzzed</item>
<item>Drunk</item>
<item>Trashed</item>
<item>Retarded</item>
</string-array>

<string name="state_prompt">State</string>
<string-array name="state_array">
<item>AL</item>
<item>AK</item>
<item>AS</item>
<item>AZ</item>
<item>AR</item>
<item>CA</item>
<item>CO</item>
<item>CT</item>
<item>DE</item>
<item>DC</item>
<item>FM</item>
<item>FL</item>
<item>GA</item>
<item>GU</item>
<item>HI</item>
<item>ID</item>
<item>IL</item>
<item>IN</item>
<item>IA</item>
<item>KS</item>
<item>KY</item>
<item>LA</item>
<item>ME</item>
<item>MH</item>
<item>MD</item>
<item>MA</item>
<item>MI</item>
<item>MN</item>
<item>MS</item>
<item>MO</item>
<item>MT</item>
<item>NE</item>
<item>NH</item>
<item>NV</item>
<item>NJ</item>
<item>NM</item>
<item>NY</item>
<item>NC</item>
<item>ND</item>
<item>MP</item>
<item>OH</item>
<item>OK</item>
<item>OR</item>
<item>PW</item>
<item>PA</item>
<item>PR</item>
<item>RI</item>
<item>SC</item>
<item>SD</item>
<item>TN</item>
<item>TX</item>
<item>UT</item>
<item>VT</item>
<item>VI</item>
<item>VA</item>
<item>WA</item>
<item>WV</item>
<item>WI</item>
<item>WY</item>
</string-array>




New source:

//Listen for btnNext button click
Button btnNext = (Button) this.findViewById(R.id.btnNext);
Button btnBack = (Button) this.findViewById(R.id.btnBack);
Button btnSubmit = (Button) this.findViewById(R.id.btnSubmit);

if (btnNext!= null) {
btnNext.setOnClickListener(new OnClickListener() {
public void onClick(View v) {

clickNext();
}
});
}
if (btnBack != null) {
//If btnBack was hit
btnBack.setOnClickListener(new OnClickListener() {
public void onClick(View v){
clickBack();
}
});
}
if (btnSubmit != null) {
//If btnSubmit was hit
btnSubmit.setOnClickListener(new OnClickListener() {
public void onClick(View v){
clickSubmit();
}
});
}
}



public void clickNext(){
//Change to second Screen
setContentView(R.layout.second);

//Initialize State Spinner
Spinner spState = (Spinner) findViewById(R.id.spState);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
RandomDrunkQuotes.this, R.array.state_array, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spState.setAdapter(adapter2);

Button btnBack = (Button) findViewById(R.id.btnBack);
btnBack.setOnClickListener(new OnClickListener() {
public void onClick(View v){
clickBack();
}
});
}
public void clickBack(){
//Change to first screen
setContentView(R.layout.main);

//Drunk Spinner
Spinner spLevel = (Spinner) findViewById(R.id.spLevel);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
RandomDrunkQuotes.this, R.array.drunk_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spLevel.setAdapter(adapter);

Button btnNext = (Button) findViewById(R.id.btnNext);
Button btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnNext.setOnClickListener(new OnClickListener() {
public void onClick(View v) {

clickNext();
}
});
}
public void clickSubmit(){

}

Answer

Your content view (main.xml) doesn't contain definitions of btnBack and btnSubmit. Your code should look like this:

Button btnNext = (Button) this.findViewById(R.id.btnNext);
if (btnNext!= null) {
    btnNext.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            ...

            //Change to second Screen
            setContentView(R.layout.second);

            //Initialize State Spinner
            ...
            Button btnBack = (Button) this.findViewById(R.id.btnBack);
            if (btnBack != null) {
                //If btnBack was hit
                btnBack.setOnClickListener(new OnClickListener() {
                    public void onClick(View v){
                        ...
                    }
                });
            }
            Button btnSubmit = (Button) this.findViewById(R.id.btnSubmit);
            if (btnSubmit != null) {
                //If btnSubmit was hit
                btnSubmit.setOnClickListener(new OnClickListener() {
                    public void onClick(View v){
                        ...
                    }
                });
            }
        }
    });
}