一个以上的按钮使我的应用程序崩溃

时间:2018-08-19 17:30:47

标签: java android xml

我目前正在Android Studio上创建一个应用。

我在同一活动中使用了不同的按钮(在同一活动中)。问题在于,一个按钮就可以正常工作,但是当我添加另一个按钮时,模拟器会崩溃。

这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start);

    defineButtons();
}

public void defineButtons() {
    findViewById(R.id.next1).setOnClickListener(buttonClickListener);
    findViewById(R.id.next2).setOnClickListener(buttonClickListener);
    findViewById(R.id.next3).setOnClickListener(buttonClickListener);
}

private View.OnClickListener buttonClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.next1:
                Intent Next1 = new Intent (Start.this, Next1.class);
                startActivity(Next1);
                break;
            case R.id.next2:
                Intent Next2 = new Intent (Start.this, Next2.class);
                startActivity(Next2);
                break;
            case R.id.next3:
                Intent Next3 = new Intent (Start.this, Next3.class);
                startActivity(Next3);
                break;
        }
    }
};

这里是XML:

<RelativeLayout 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=".Start"
android:background="@drawable/background">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="700px"
        android:id="@+id/statement"
        android:text="TEXT1"
        android:background="@drawable/button_border"
        android:textColor="#000000"
        android:alpha="0.95"
        android:layout_marginHorizontal="70px"
        android:layout_marginTop="180px"
        android:gravity="center"
        android:paddingHorizontal="50px"
        android:textSize="16sp"
        />

    <Button
        android:id="@+id/next1"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="match_parent"
        android:layout_height="150px"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="160dp"
        android:layout_marginHorizontal="70px"
        android:alpha="0.9"
        android:background="@drawable/button_border"
        android:text="TEXT2"
        android:textColor="@android:color/black"
        android:textSize="16sp"
        android:textAllCaps="false"/>

    <Button
        android:id="@+id/next2"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="match_parent"
        android:layout_height="150px"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="90dp"
        android:layout_marginHorizontal="70px"
        android:alpha="0.9"
        android:background="@drawable/button_border"
        android:text="TEXT3"
        android:textColor="@android:color/black"
        android:textSize="16sp"
        android:textAllCaps="false"/>

    <Button
        android:id="@+id/next3"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="match_parent"
        android:layout_height="150px"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"
        android:layout_marginHorizontal="70px"
        android:alpha="0.9"
        android:background="@drawable/button_border"
        android:text="TEXT4"
        android:textColor="@android:color/black"
        android:textSize="16sp"
        android:textAllCaps="false"/>
</RelativeLayout>

我知道这很麻烦,我是编程新手。

1 个答案:

答案 0 :(得分:3)

您的代码非常好,您只需要在清单文件中声明这样的Next2和Next3活动

const cartesian = (xs, ys, ...zs) => ys 
  ? cartesian([].concat(...xs.map(x => ys.map(y => [].concat(x, y)))), ...zs) 
  : xs;

const toObj = xs => xs.reduce((a, b) => Object.assign(a, b), {})

const toNames = people => people.map(person => Object.keys(person)
  .filter(name => ['name', 'short'].includes(name))
  .map(name => ({[person[name]]: person.city}))
)

const nameCombos = people => cartesian.apply(null, toNames(people)).map(toObj)

const people = [
    { name: "Robert",    city: "New York",    short: "Bob"  },
    { name: "Allison",   city: "Los Angeles", short: "Ally" },
    { name: "James",     city: "Chicago",     short: "Jim"  },
    { name: "Hannah",    city:"Toronto"                     },
    { name: "Elizabeth", city: "London"                     }
]

console.log(nameCombos(people))

Android从清单文件中读取您的应用程序组件,有关更多详细信息,请参考此链接https://developer.android.com/guide/topics/manifest/manifest-intro