按钮不会启动另一个活动

时间:2017-05-28 13:27:15

标签: android

我的按钮带有id收音机,开始第二次活动,不工作,我的应用程序一直崩溃。然而,第二个按钮开始另一个当前为空的活动。有人可以告诉我我错过了什么,所以当我按下按钮时我的应用程序停止崩溃。

我的第一个活动xml

HTML:

<input v-model="system_id">
<button v-on:click="getSystemData" class="btn btn-primary">Get system data</button>


Data model:

data: () => ({
  errors: [],
  system_id: null,
  system_data: null
}),



Function:

getSystemData () {
    HTTP.get('/api/get_systems')
    .then(response => {
      this.response = response.data

      // equals 1234
      console.log(this.system_id)
      for (var key in this.response) {

        // the id 1234 is found here in a large list
        console.log(this.response[key].system_id)


        // Does not go true?!?!
        if (this.response[key].system_id === this.system_id) {
          this.system_data = this.response[key].system_data
        }
      }
    })
    .catch(e => {
      this.errors.push(e)
    })
  }

第一个活动的java代码

input = "Tokenzing sentence using expressions"

和android manifestxml代码。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout     
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="com.example.mitja.radiohead.PrvaStran">
<?xml version="1.0" encoding="utf-8"?>
   <android.support.constraint.ConstraintLayou                   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="com.example.mitja.radiohead.PrvaStran">

<LinearLayout
    android:layout_width="368dp"
    android:layout_height="0dp"
    android:orientation="vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="49dp"
    android:weightSum="1">


    <ImageView
        android:id="@+id/slika"
        android:layout_width="match_parent"
        android:layout_height="218dp"
        android:layout_weight="0.16"
        android:src="@drawable/radio"
        android:layout_marginLeft="1dp"
        android:layout_marginRight="2dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="16dp" />


    <Button
        android:id="@+id/radio"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:layout_weight="0.50"
        android:text="radio"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="323dp" />

    <Button
        android:id="@+id/predvajalnik"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:text="predvajalnik"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="371dp" />

</LinearLayout>

我在这里失踪了什么?第二个活动是从我在youtube上看到的教程中创建的,用于创建包含图像和文本的列表视图。我从这个网站获得了代码,现在我试图在这个应用程序中使用它进行练习。我没有任何关于Android工作室的经验,所以我要求一个小指南。

2 个答案:

答案 0 :(得分:0)

在您的代码中,您将相同的按钮引用到两个不同的ID

private Button btn1;
 btn1 = (Button) findViewById(R.id.radio);
 btn1 = (Button) findViewById(R.id.predvajalnik);

这就是应用程序崩溃的原因。 声明两个不同的按钮,然后将它们引用到受尊重的ID。

private Button btn1;
private Button btn2;
 btn1 = (Button) findViewById(R.id.radio);
 btn2 = (Button) findViewById(R.id.predvajalnik);

答案 1 :(得分:0)

您不需要将按钮编写为字段,但您需要实际设置两个按钮监听器

findViewById(R.id.radio).setOnClickListener(
   ... 
);
findViewById(R.id.predvajalnik).setOnClickListener(
   ... 
);