2013-02-22 43 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