感谢您花时间与我一起调查我的问题。我是Android开发的新手,这是我第一次尝试应用程序。任何和所有的帮助将不胜感激。此时所有建议都是必要的建议。
手头的主要问题是btnBack和btnSubmit没有触发onClick事件。请指教。
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>
新来源:
//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(){
}
答案 0 :(得分:0)
问题是,由于你的onClick监听器改变了布局,他们自己也需要为新布局设置处理程序。您最好在Activity中创建新的私有方法来设置处理程序,然后在onCreate和处理程序中调用该方法。
答案 1 :(得分:0)
View
仅在布局xml膨胀时创建。你正在膨胀R.layout.main:这只会创建btnNext
按钮,因此设置了一个监听器,但是btnBack
和btnSubmit
按钮上没有设置监听器,因为它们是onCreate
方法中的null(因为它们尚不存在)。
一个快速解决方法是在致电btnBack
后立即将setOnClickListener
setContentView(R.layout.second);
移至。
答案 2 :(得分:0)
您的内容视图(main.xml)不包含btnBack和btnSubmit的定义。您的代码应如下所示:
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){
...
}
});
}
}
});
}