2017-05-28 65 views
-1

我的按鈕與ID收音機,開始第二個活動,不工作,我的應用程序不斷崩潰。然而,第二個按鈕開始另一個活動,目前爲空。有人可以告訴我我錯過了什麼,所以當我按下按鈕時,我的應用程序停止崩潰。按鈕不會啓動另一個活動

我的第一個活動的xml

<?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> 

的第一個活動

public class PrvaStran extends AppCompatActivity { 

private static Button btn1; 

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

    onClickButton(); 
    onClickButton2(); 
} 

public void onClickButton() 
{ 
    btn1 = (Button) findViewById(R.id.radio); 
    btn1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent openMainAct = new Intent(PrvaStran.this, Radio.class); 
      startActivity(openMainAct); 
     } 
    }); 
} 

public void onClickButton2() 
{ 
    btn1 = (Button) findViewById(R.id.predvajalnik); 
    btn1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent("com.example.mitja.radiohead.Predvajalnik"); 
      startActivity(intent); 
     } 
    }); 
} 
} 

和Android manifestxml代碼的Java代碼。

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.mitja.radiohead"> 

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.INTERNET" /> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".PrvaStran"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <activity 
     android:name=".Radio" 
     android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="com.example.mitja.radiohead.Radio" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 

    </activity> 
    <activity android:name=".Predvajalnik"> 
     <intent-filter> 
      <action android:name="com.example.mitja.radiohead.Predvajalnik" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 


</application> 

缺少什麼我在這裏?第二項活動來自我在YouTube上看到的一個教程,用圖像和文本創建列表視圖。我從這個網站獲得了代碼,現在我試圖在這個應用程序中使用它來練習。我還沒有任何與Android工作室的經驗之前,所以我要求一個小指南。

+0

忘記而與我的第二個活動上使用的代碼的鏈接按鈕被打開 https://stackoverflow.com/questions/42771902/i-am-trying-to-implement-a-custom-listview-using-arrayadapter-but-all-the-items –

+0

可以你發佈崩潰日誌? –

+0

請編輯您的問題標題。因爲Android Studio與您的問題無關。 Android Studio是_IDE_,Android是_Platform_ – Shashanth

回答

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); 
+0

sory的回覆花了這麼長時間。使用你的建議,它的工作感謝很多隊友:) –

+0

太棒了!你可以接受這個答案。您的項目其餘部分祝您好運。 –

0

你不需要寫按鈕的領域,但你需要實際設置兩個按鈕聽衆

findViewById(R.id.radio).setOnClickListener(
    ... 
); 
findViewById(R.id.predvajalnik).setOnClickListener(
    ... 
); 
相關問題