【问题标题】:Error when running Android TabHost运行 Android TabHost 时出错
【发布时间】:2010-12-07 22:33:58
【问题描述】:

嘿,我正在尝试实现两个选项卡。每个人都调用一个规范活动。但是当我尝试运行时出现此错误:

12-07 20:26:22.164: ERROR/AndroidRuntime(363): FATAL EXCEPTION: main
12-07 20:26:22.164: ERROR/AndroidRuntime(363): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.psyhclo/com.psyhclo.Main}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.psyhclo/com.psyhclo.RatedCalls}: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.CallLogProvider from ProcessRecord{405a8ac8 363:com.psyhclo/10030} (pid=363, uid=10030) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at android.os.Looper.loop(Looper.java:123)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at android.app.ActivityThread.main(ActivityThread.java:3647)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at java.lang.reflect.Method.invokeNative(Native Method)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at java.lang.reflect.Method.invoke(Method.java:507)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at dalvik.system.NativeStart.main(Native Method)
12-07 20:26:22.164: ERROR/AndroidRuntime(363): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.psyhclo/com.psyhclo.RatedCalls}: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.CallLogProvider from ProcessRecord{405a8ac8 363:com.psyhclo/10030} (pid=363, uid=10030) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1462)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at android.widget.TabHost.setCurrentTab(TabHost.java:326)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at android.widget.TabHost.addTab(TabHost.java:216)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at com.psyhclo.Main.onCreate(Main.java:17)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     ... 11 more
12-07 20:26:22.164: ERROR/AndroidRuntime(363): Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.CallLogProvider from ProcessRecord{405a8ac8 363:com.psyhclo/10030} (pid=363, uid=10030) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at android.os.Parcel.readException(Parcel.java:1322)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at android.os.Parcel.readException(Parcel.java:1276)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:1882)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at android.app.ActivityThread.getProvider(ActivityThread.java:3311)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at android.app.ActivityThread.acquireProvider(ActivityThread.java:3336)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at android.app.ContextImpl$ApplicationContentResolver.acquireProvider(ContextImpl.java:1634)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at android.content.ContentResolver.acquireProvider(ContentResolver.java:748)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at android.content.ContentResolver.query(ContentResolver.java:256)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at com.psyhclo.RatedCalls.onCreate(RatedCalls.java:27)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
12-07 20:26:22.164: ERROR/AndroidRuntime(363):     ... 20 more

这里是 Main.java

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;

public class Main extends TabActivity {

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TabHost tabHost = getTabHost();
    tabHost.addTab(tabHost.newTabSpec("one").setIndicator("Rated Calls")
            .setContent(new Intent(this, RatedCalls.class)));
    tabHost.addTab(tabHost.newTabSpec("two").setIndicator("Rated Contacts")
            .setContent(new Intent(this, SMS.class)));

    tabHost.setCurrentTab(2);

}

    }

这是 RatedCalls.java

import java.text.DateFormat;
import java.util.ArrayList;

import com.psyhclo.R;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class RatedCalls extends Activity {

private static TextView txtView;
private CallDataHelper dh;
StringBuilder sb = new StringBuilder();

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.ratedcalls);        

    //txtView = (TextView) findViewById(R.id.TextView01);
    TextView txtView = new TextView(this);
    Cursor cursor = getContentResolver().query(
            android.provider.CallLog.Calls.CONTENT_URI, null, null, null,
            android.provider.CallLog.Calls.DATE + " DESC ");

    startManagingCursor(cursor);
    int numberColumnId = cursor
            .getColumnIndex(android.provider.CallLog.Calls.NUMBER);
    int durationId = cursor
            .getColumnIndex(android.provider.CallLog.Calls.DURATION);
    int contactNameId = cursor
            .getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME);
    int dateId = cursor.getColumnIndex(android.provider.CallLog.Calls.DATE);
    int numTypeId = cursor.getColumnIndex(android.provider.CallLog.Calls.CACHED_NUMBER_TYPE);

    ArrayList<String> callList = new ArrayList<String>();
    if (cursor.moveToFirst()) {
        do {

            String callerPhoneNumber = cursor.getString(numberColumnId);
            String contactName = cursor.getString(contactNameId);
            String duration = cursor.getString(durationId);
            String callDate = DateFormat.getDateInstance().format(dateId);      
            String numType = cursor.getString(numTypeId);

            //this.dh.insert(1, contactName, callerPhoneNumber, numType, duration, callDate, "01:20");
            Toast.makeText(getBaseContext(), "Inserted!", Toast.LENGTH_LONG);
            callList.add("\nNew Info: \nContact Number: "
                    + callerPhoneNumber + "\nContact Name: " + contactName
                    + "\nDuration: " + duration + "\nDate: " + callDate);

        } while (cursor.moveToNext());
    }
    txtView.setText(callList.toString());
}
    }

这里是 ratedcalls.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp">
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp" />
</LinearLayout>
</TabHost>

这里是 ma​​in.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp">
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp" />
</LinearLayout>
</TabHost>

【问题讨论】:

  • 基于该堆栈跟踪,问题实际上不在任何这些文件中。检查您的 AndroidManifest.xml 文件并确保您有正确的permissions 来访问联系人。如果您仍然需要帮助,那么我建议您发布您的 AndroidManifest.xml 文件的源代码。

标签: android android-emulator android-widget android-layout android-tabhost


【解决方案1】:

日志清楚地说明了问题:

SecurityException: Permission Denial: opening provider
com.android.providers.contacts.CallLogProvider requires
android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS

在清单文件中检查您的权限。

【讨论】:

  • 是不是这个错误。我已经改变了它,并设置了正确的权限。我添加了:
  • 仍然是相同的堆栈跟踪?您是否在正确的位置添加了权限?
  • 权限进入清单标签,而不是应用标签。顺便说一句,最好编辑您的问题并添加信息,而不是添加答案。
  • 我已添加到清单标签,但现在当我运行应用程序时,它说它已安装在模拟器上,但图标没有出现在屏幕上。并且 logcat 没有错误
  • 很高兴为您提供帮助!祝你的应用好运!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多