2017-07-31 100 views
0

我想要在主活動頁面上連接到藍牙的應用程序編碼,然後在第二個活動頁面上打開無聲振鈴模式。每次點擊按鈕打開下一個活動,應用程序就會關閉。根據Android Studio沒有錯誤。Android Studio應用程序在打開第二個活動時崩潰

這裏是我的SecondActivity.java文件

package fonephree.fonephreeconnecttobluetooth; 

import android.app.Activity; 
import android.content.Context; 
import android.media.AudioManager; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

    public class SecondActivity extends Activity { 
     Button button; 
     AudioManager audiomanager; 
     Context context; 
     TextView textview; 

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

      button = (Button)findViewById(R.id.button6); 

      textview = (TextView)findViewById(R.id.textView2); 

      context = getApplicationContext(); 

      audiomanager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 

      button.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 

        audiomanager.setRingerMode(AudioManager.RINGER_MODE_SILENT); 

        textview.setText("Silent Mode Enable"); 

       } 
      }); 
     } 

    } 

,這裏是我的activity_second.xml文件

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    android:background="@android:color/holo_orange_dark" 
    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="fonephree.fonephreeconnecttobluetooth.SecondActivity"> 

     <Button 
      android:id="@+id/button6" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true" 
      android:text="Silent" /> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_above="@+id/button6" 
      android:layout_centerHorizontal="true" 
      android:layout_marginBottom="44dp" 
      android:text="Silent Mode" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:srcCompat="@drawable/phree" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" /> 

</RelativeLayout> 

以防萬一的錯誤是在我AndroidManifest

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

    <uses-permission android:name="android.permission.BLUETOOTH" /> 
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 
    <uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" /> 

    <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=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".SecondActivity" 
      android:label="@string/title_activity_second" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="fonephree.fonephreeconnecttobluetooth.SecondActivity" /> 

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

    </application> 

</manifest> 

編輯

我修復了代碼,但是現在應用程序僅在按下按鈕時關閉,並且在打開應用程序之前它不處於靜音模式。該按鈕只是加強了靜音模式,它並沒有真正打開它。

+1

你有來自日誌的任何信息嗎?查看崩潰的位置和內容會很有幫助 – lalosoft

+0

它不會在任何消息或日誌中彈出。我不知道爲什麼。它唯一的消息是「應用終止」 –

+0

你在看logcat嗎?關閉所有過濾並顯示所有應用程序。 – Jon

回答

0

從你給什麼樣的信息,要設置你的XML被activity_main,而button6和textview2在activity_second.xml定義

所以,你的onCreate想下面要解決的問題

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

      button = (Button)findViewById(R.id.button6); 

      textview = (TextView)findViewById(R.id.textView2); 

      context = getApplicationContext(); 

      audiomanager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 

      button.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 

        audiomanager.setRingerMode(AudioManager.RINGER_MODE_SILENT); 

        textview.setText("Silent Mode Enable"); 

       } 
      }); 
     } 
+0

謝謝!應用程序現在運行到下一頁,但是當我單擊靜音模式按鈕時,應用程序將關閉,除非手機已處於靜音狀態。不知道如何解決這個問題。 –

相關問題