我正在尝试创建一个将按钮与类(意图定义)连接起来的意图。问题是,当我按下这个按钮时,一切都会崩溃。我认为问题出在intent类中。你能看出什么是错的吗?我正在尝试按下按钮下注时打开一个警告对话框。我已经创建了它:
Chips类,按下按钮后我想要使用的类:
public class Chips extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
}
private AlertDialog.Builder dialog;
public void scommessa () {
dialog = new AlertDialog.Builder(this);
final EditText txtInput = new EditText (this);
final String stringInput = txtInput.getText().toString();
dialog.setTitle("Welcome to poker");
dialog.setMessage("Player 2, place your bet!");
dialog.setView(txtInput);
dialog.setPositiveButton("Bet", new Dialog.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
int bet1 = 100;
int intInput = Integer.parseInt(stringInput);
int newPlayer1Bet = bet1 - intInput;
String total1 = "" + newPlayer1Bet;
}
});
dialog.setNegativeButton("Pass", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which){
Toast.makeText(getApplicationContext(), "Player 2 won", Toast.LENGTH_SHORT).show();;
}
});
AlertDialog dialogPoker = dialog.create();
dialogPoker.show();
}
public void onClick(View v){
switch (v.getId()){
case R.id.Cover1:
scommessa();
break;
}
}
}
然后这是我定义按钮的类,然后尝试调用类Chips:
final Button Cover1 = (Button) findViewById(R.id.Cover1);
final Button button1 = (Button) findViewById(R.id.A);
final Button button2 = (Button) findViewById(R.id.B);
final Button button3 = (Button) findViewById(R.id.C);
final Button button4 = (Button) findViewById(R.id.D);
final Button button5 = (Button) findViewById(R.id.E);
final Button Bet = (Button) findViewById(R.id.button1);
Cover1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
if(v.equals(Cover1)){
button1.setBackgroundResource(R.drawable.back);
button2.setBackgroundResource(R.drawable.back);
button3.setBackgroundResource(R.drawable.back);
button4.setBackgroundResource(R.drawable.back);
button5.setBackgroundResource(R.drawable.back);
}
}
});
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View x){
if(x.equals(Bet)){
Intent intent1 = new Intent(GameActivity.this, Chips.class);
startActivity(intent1);
Chips chip = new Chips();
chip.scommessa();
}
Intent intent1 = new Intent(GameActivity.this, Chips.class);
//startActivity(intent1);
Chips chip = new Chips();
chip.scommessa();
}
});
这是清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pokeroriginal"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.pokeroriginal.FullscreenActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="GameActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.pokeroriginal.GameActivity"
android:label="@string/title_activity_game"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.NoTitleBar">
</activity>
<activity
android:name= "com.example.pokeroriginal.Chips"
android:label= "@string/Bet"
android:screenOrientation= "landscape"
android:theme= "@android:style/Theme.NoTitleBar">
</activity>
</application>
</manifest>
还定义了所有按钮的activity_game.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/green_table_cloth"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".GameActivity" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal" >
<Button
android:id="@+id/Cover1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cover"
android:onClick="onClickChips" />
<Button
android:id="@+id/A"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:onClick="onClick"
android:background="@drawable/back"
/>
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" >
</View>
<Button
android:id="@+id/B"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:background="@drawable/back"
/>
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" >
</View>
<Button
android:id="@+id/C"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:background="@drawable/back"
/>
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" >
</View>
<Button
android:id="@+id/D"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:background="@drawable/back"
/>
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" >
</View>
<Button
android:id="@+id/E"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="40dp"
android:onClick="onClick"
android:background="@drawable/back"
/>
<View
android:layout_width="0dp"
android:layout_height="0dp" >
</View>
</LinearLayout>
<ImageSwitcher
android:id="@+id/imageSwitcher1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick(View x)"
android:text="@string/Bet" />
<Button
android:id="@+id/Button01"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Bet" />
</ImageSwitcher>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:orientation="horizontal" >
<Button
android:id="@+id/Cover2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cover" />
<Button
android:id="@+id/A2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:background="@drawable/back2" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" />
<Button
android:id="@+id/B2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/back2" />
<View
android:layout_width="0dp"
android:layout_height="10dp"
android:layout_weight="1" />
<Button
android:id="@+id/C2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/back2" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" />
<Button
android:id="@+id/D2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/back2" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" />
<Button
android:id="@+id/E2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="@drawable/back2" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" />
<!-- <ImageView
android:id="@+id/Image"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView> '
-->
</LinearLayout>
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Bet" />
</LinearLayout>
答案 0 :(得分:0)
如果没有看到logcat,很难确定错误是什么 - 将来你真的应该包含它。
查看代码,我在button1 OnClickListener:
中注意到了这一点Intent intent1 = new Intent(GameActivity.this, Chips.class);
startActivity(intent1);
Chips chip = new Chips();
chip.scommessa();
要开始其他活动,您需要做的是所有:
Intent intent1 = new Intent(GameActivity.this, Chips.class);
startActivity(intent1);
我认为尝试使用它的构造函数实例化Activity会导致问题。执行此操作时,活动的视图未正确设置,当您尝试显示对话框时,您将遇到错误。 尝试删除这些行:
Chips chip = new Chips();
chip.scommessa();
您的OnClickListener应如下所示:
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View x) {
if (x.equals(Bet)) {
Intent intent1 = new Intent(GameActivity.this, Chips.class);
startActivity(intent1);
}
}
});
答案 1 :(得分:0)
我认为你在这里得到了NullPointerException。
if(x.equals(Bet))
并且您的xml onClick()实现错误。
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick(View x)"
android:text="@string/Bet" />
java代码应该是这样的,
final Button Cover1 = (Button) findViewById(R.id.Cover1);
final Button button1 = (Button) findViewById(R.id.A);
final Button button2 = (Button) findViewById(R.id.B);
final Button button3 = (Button) findViewById(R.id.C);
final Button button4 = (Button) findViewById(R.id.D);
final Button button5 = (Button) findViewById(R.id.E);
final Button Bet = (Button) findViewById(R.id.button1);
Cover1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
button1.setBackgroundResource(R.drawable.back);
button2.setBackgroundResource(R.drawable.back);
button3.setBackgroundResource(R.drawable.back);
button4.setBackgroundResource(R.drawable.back);
button5.setBackgroundResource(R.drawable.back);
}
});
Bet.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Chips chip = new Chips();
chip.scommessa();
Intent intent1 = new Intent(GameActivity.this, Chips.class);
startActivity(intent1);
}
});
或者,如果您的xml实现应该是这样的。
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="NavigateToChips"
android:text="@string/Bet" />
你应该在像这样的活动中实现onClick()方法,
public void NavigateToChips(View v) {
Chips chip = new Chips();
chip.scommessa();
Intent intent1 = new Intent(GameActivity.this, Chips.class);
startActivity(intent1);
}
答案 2 :(得分:0)
在xml布局文件中,我看到ID为android:onClick="onClickChips"
的组件的属性android:id="@+id/Cover1"
。因此,java代码通常会查找名为onClickChips()
的方法,而您使用setOnClickListener()
代表Cover1
按钮。
<强>解决方案:强>
从XML中删除属性(android:onClick="onClickChips"
)或删除该按钮的setOnClickListener()
。只保留一个。