我正在制作一个投票应用程序,用于存储点击firebase。当按下任意两个按钮时,它应该转到常见的布局活动(votes.java)。对于Btn1会发生这种情况,但是当单击Btn2时应用程序崩溃。
MainActivity.java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.os.Handler;
import com.firebase.client.Firebase;
public class Main2Activity extends AppCompatActivity {
private Firebase mRootRef;
private Button mBtn1;
private Button mBtn2;
int counter = 0;
int counter1 = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Firebase.setAndroidContext(this);
mBtn1 = (Button) findViewById(R.id.btn1);
mBtn2 = (Button) findViewById(R.id.btn2);
mBtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.equals(mBtn1)) {
Intent i1= new Intent(Main2Activity.this, Votes.class);
startActivity(i1);
mRootRef = new Firebase("https://voting-cf0fa.firebaseio.com/House/Jupiter/Player 1");
Firebase mRefChild = mRootRef.child("Votes");
counter++;
mRefChild.setValue(counter);
}
mRootRef = new Firebase("https://voting-cf0fa.firebaseio.com/House/Jupiter/Player 2");
mBtn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.equals(mBtn2)) {
Intent i2= new Intent(Main2Activity.this, Votes.class);
startActivity(i2);
Firebase mRefChild = mRootRef.child("Votes");
counter1++;
mRefChild.setValue(counter1);
}
}
});
}
});
}}
感谢。
答案 0 :(得分:1)
这应该解决它:
public class Main2Activity extends AppCompatActivity {
private Firebase mRootRef;
private Button mBtn1;
private Button mBtn2;
int counter = 0;
int counter1 = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Firebase.setAndroidContext(this);
mBtn1 = (Button) findViewById(R.id.btn1);
mBtn2 = (Button) findViewById(R.id.btn2);
mBtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent i1= new Intent(Main2Activity.this, Voted.class);
startActivity(i1);
mRootRef = new Firebase("https://voting-cf0fa.firebaseio.com/House/Jupiter/Player 1");
Firebase mRefChild = mRootRef.child("Votes");
counter++;
mRefChild.setValue(counter);
});
mBtn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
mRootRef = new Firebase("https://voting-cf0fa.firebaseio.com/House/Jupiter/Player 2");
Intent i2= new Intent(Main2Activity.this, Voted2.class);
startActivity(i2);
Firebase mRefChild = mRootRef.child("Votes");
counter1++;
mRefChild.setValue(counter1);
}
});
}
}
答案 1 :(得分:1)
问题是你正在尝试处理Button 1中Button 2的点击监听器。 这应该解决它:
public class Main2Activity extends AppCompatActivity {
private Firebase mRootRef;
private Button mBtn1;
private Button mBtn2;
int counter = 0;
int counter1 = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Firebase.setAndroidContext(this);
mBtn1 = (Button) findViewById(R.id.btn1);
mBtn2 = (Button) findViewById(R.id.btn2);
mBtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent i1= new Intent(Main2Activity.this, Voted.class);
startActivity(i1);
mRootRef = new Firebase("https://voting-cf0fa.firebaseio.com/House/Jupiter/Player 1");
Firebase mRefChild = mRootRef.child("Votes");
counter++;
mRefChild.setValue(counter);
});
mBtn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
mRootRef = new Firebase("https://voting-cf0fa.firebaseio.com/House/Jupiter/Player 2");
Intent i2= new Intent(Main2Activity.this, Voted2.class);
startActivity(i2);
Firebase mRefChild = mRootRef.child("Votes");
counter1++;
mRefChild.setValue(counter1);
}
});
}
}
答案 2 :(得分:1)
这是因为按钮2的单击侦听器位于按钮1的单击侦听器内。您需要为不同的按钮初始化不同的onClick侦听器。
试试这个:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Firebase.setAndroidContext(this);
mBtn1 = (Button) findViewById(R.id.btn1);
mBtn2 = (Button) findViewById(R.id.btn2);
mBtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent i1= new Intent(Main2Activity.this, Voted.class);
startActivity(i1);
mRootRef = new Firebase("https://voting-cf0fa.firebaseio.com/House/Jupiter/Player 1");
Firebase mRefChild = mRootRef.child("Votes");
counter++;
mRefChild.setValue(counter);
});
mBtn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
mRootRef = new Firebase("https://voting-cf0fa.firebaseio.com/House/Jupiter/Player 2");
Intent i2= new Intent(Main2Activity.this, Voted2.class);
startActivity(i2);
Firebase mRefChild = mRootRef.child("Votes");
counter1++;
mRefChild.setValue(counter1);
}
});