【问题标题】:Android App for Outgoing Call Number Detection is Crashing用于去电号码检测的 Android 应用程序崩溃
【发布时间】:2014-07-21 16:20:35
【问题描述】:

这让我发疯,当我拨出电话时,应用程序崩溃了。

MainActivity 类:

import android.os.Bundle;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toast.makeText(getBaseContext(), "started", Toast.LENGTH_LONG).show();
}

}

OutgoingReceiver 广播接收器:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.Toast;

class OutgoingReceiver extends BroadcastReceiver {

    @Override
  public void onReceive(Context context, Intent intent) {
    String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

    Toast.makeText(context, "Outgoing: "+number, Toast.LENGTH_LONG).show();
}

}

清单:

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


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

<uses-sdk
    android:minSdkVersion="12"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name">
    <activity
        android:name=".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>


     <receiver android:name=".OutgoingReceiver" >
        <intent-filter>
    <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
    </intent-filter>
    </receiver>

</application>

</manifest>

更新: 日志输出

07-21 12:12:52.160: E/AndroidRuntime(3654): FATAL EXCEPTION: main
07-21 12:12:52.160: E/AndroidRuntime(3654): Process: com.example.secret, PID: 3654
07-21 12:12:52.160: E/AndroidRuntime(3654): java.lang.RuntimeException: Unable to instantiate receiver com.example.secret.OutgoingReceiver: java.lang.IllegalAccessException: access to class not allowed
07-21 12:12:52.160: E/AndroidRuntime(3654):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2400)
07-21 12:12:52.160: E/AndroidRuntime(3654):     at android.app.ActivityThread.access$1700(ActivityThread.java:135)
07-21 12:12:52.160: E/AndroidRuntime(3654):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
07-21 12:12:52.160: E/AndroidRuntime(3654):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-21 12:12:52.160: E/AndroidRuntime(3654):     at android.os.Looper.loop(Looper.java:136)
07-21 12:12:52.160: E/AndroidRuntime(3654):     at android.app.ActivityThread.main(ActivityThread.java:5017)

也许它有些愚蠢,但我自己看不到。 有什么想法吗?

【问题讨论】:

  • 你的 logcat 说什么?
  • 等等,我去看看。
  • 好的,添加了 logcat 输出。

标签: android detection phone-number phone-call


【解决方案1】:

来自您的日志:

无法实例化接收器 com.example.secret.OutgoingReceiver:java.lang.IllegalAccessException:不允许访问类

问题是您的OutgoingReceiver 没有访问修饰符,使其对您的包外的所有内容都不可见。

为了让 Android 实例化您的接收器并与之交互,它必须是公开的。

【讨论】:

    【解决方案2】:

    将接收器类公开

    【讨论】:

    • 你是对的,即使 Tanis.7x 回答也正确,我也会接受你的回答,因为你是第一个
    猜你喜欢
    • 2015-12-17
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 2021-04-20
    • 2016-04-06
    相关资源
    最近更新 更多