2013-02-22 27 views
0

我不斷收到強制關閉錯誤在測試這個程序。應用程序打開罰款..但3-4秒後強制關閉錯誤對話框來了。包括代碼。 任何幫助將不勝感激。 感謝在Android Manifest,App強制關閉中聲明的BroadcastReceiver。

清單

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.cy.headset" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="17" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 

    <receiver android:name=".Main" > 
     <intent-filter> 
      <action android:name="android.content.Intent.ACTION_BATTERY_CHANGED" /> 
     </intent-filter> 
    </receiver> 

    <activity 
     android:name="com.cy.headset.Main" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

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

Java代碼的

package com.cy.headset; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.widget.Toast; 
public class Main extends BroadcastReceiver{ 

@Override 
public void onReceive(Context context, Intent i) { 

    Toast.makeText(context, "Headphone connected", Toast.LENGTH_LONG).show(); 

}} 

XML UI文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".Main" > 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:layout_margin="10dp" 
    android:layout_marginTop="151dp" 
    android:text="@string/hello_world" 
    android:textAlignment="center" /> 

+0

您對'Activity'和'BroadcastReceiver'類使用相同的名稱。更改一個任何其他名稱或如果'BroadcastReceiver'是另一個包,然後使用全名包括包名 – 2013-02-22 17:04:24

回答

0

什麼是你的廣播接收器類的包是Main.java?

<receiver android:name=".Main" > 
    <intent-filter> 
     <action android:name="android.content.Intent.ACTION_BATTERY_CHANGED" /> 
    </intent-filter> 
</receiver> 

它不應該com.cy.headset正確嗎?因爲它將與您的主要活動衝突Main.java

我認爲有兩種解決方案在這個指定清單中接收器的包。

例如:

<receiver android:name="com.package.name.Main" > 
    <intent-filter> 
     <action android:name="android.content.Intent.ACTION_BATTERY_CHANGED" /> 
    </intent-filter> 
</receiver> 

或者,如果你希望你的接收機是在同一個包你的主要活動是Main.java重命名你的接收機

例如:

<receiver android:name=".BatteryChange" > 
    <intent-filter> 
     <action android:name="android.content.Intent.ACTION_BATTERY_CHANGED" /> 
    </intent-filter> 
</receiver> 

<activity 
    android:name="com.cy.headset.Main" 
    android:label="@string/app_name" > 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 

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

另一種方法是。

<receiver android:name="com.cy.headset.BatteryChange" > 
    <intent-filter> 
     <action android:name="android.content.Intent.ACTION_BATTERY_CHANGED" /> 
    </intent-filter> 
</receiver> 

<activity 
    android:name="com.cy.headset.Main" 
    android:label="@string/app_name" > 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 

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

你同時聲明您Activity和你BroadcastReceivercom.cy.headset.Main。這是行不通的。

當安裝您的應用程序的Android試圖啓動名爲Main的活動,但發現一個BroadcastReceiver來代替。

獨立這些成兩個不同的類,一個延伸活性和一個延伸的BroadcastReceiver。

+0

謝謝你,我會給它一個鏡頭:) – 2013-02-22 17:12:50

0
<receiver android:name=".Main" > 
    <intent-filter> 
     <action android:name="android.content.Intent.ACTION_BATTERY_CHANGED" /> 
    </intent-filter> 
</receiver> 

<activity 
    android:name="com.cy.headset.MainActivity" <<Change it to MainActivity 
    android:label="@string/app_name" > 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 

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

您已定義的接收器和活動名稱相同,並且兩者都是在相同的封裝com.cy.headset所以重命名一個或在不同的封裝任一定義和給出完全限定的包名稱。

對於例如:如果接收器是在com.cv.headset.receiver然後寫在清單一樣

<receiver android:name="com.cv.headset.receiver.Main" > 
    <intent-filter> 
     <action android:name="android.content.Intent.ACTION_BATTERY_CHANGED" /> 
    </intent-filter> 
</receiver> 
+0

謝謝..我會試一試.. – 2013-02-22 17:13:14