Android 内容提供商不工作
我正在使用运行在ICS上的Xperia。这是我写的一个应用程序,用来了解内容提供商的工作情况。该应用程序在姜饼模拟器上运行良好,但在我的手机上没有显示任何内容。是什么原因导致应用程序出现这种行为? 下面是android清单的代码Android 内容提供商不工作,android,Android,我正在使用运行在ICS上的Xperia。这是我写的一个应用程序,用来了解内容提供商的工作情况。该应用程序在姜饼模拟器上运行良好,但在我的手机上没有显示任何内容。是什么原因导致应用程序出现这种行为? 下面是android清单的代码 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="legac
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="legacy_systems.contentprovider"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="legacy_systems.contentprovider.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>
</application>
</manifest>
和布局代码
<LinearLayout 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"
android:orientation="vertical" >
<ListView
android:id="@+id/android:lst"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:stackFromBottom="false"
android:transcriptMode="normal" />
<TextView
android:id="@+id/con_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold" />
<TextView
android:id="@+id/con_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
最后,代码为java源文件
package legacy_systems.contentprovider;
import android.os.Bundle;
import android.view.Menu;
import android.app.ListActivity;
import android.content.CursorLoader;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.widget.CursorAdapter;
import android.widget.SimpleCursorAdapter;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Uri allCont = Uri.parse("content://contacts/people");
Cursor c;
if(android.os.Build.VERSION.SDK_INT <11){
c = managedQuery(allCont, null, null, null, null);
}
else{
CursorLoader cl = new CursorLoader(this,
allCont,
null,
null,
null,
null);
c = cl.loadInBackground();
}
String[] columns = new String[]{ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts._ID};
int views[]=new int[]{R.id.con_name, R.id.con_id};
SimpleCursorAdapter ada;
if(android.os.Build.VERSION.SDK_INT<11){
ada = new SimpleCursorAdapter(this, R.layout.activity_main,c, columns, views);
}
else
{
ada = new SimpleCursorAdapter(this, R.layout.activity_main,c, columns, views, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
}
this.setListAdapter(ada);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
package legacy_systems.contentprovider;
导入android.os.Bundle;
导入android.view.Menu;
导入android.app.ListActivity;
导入android.content.CursorLoader;
导入android.database.Cursor;
导入android.net.Uri;
导入android.provider.contacts合同;
导入android.widget.CursorAdapter;
导入android.widget.SimpleCursorAdapter;
公共类MainActivity扩展了ListActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Uri allCont=Uri.parse(“content://contacts/people");
光标c;
如果(android.os.Build.VERSION.SDK_INT您将ListView的ID设置为@+android:ID/lst
。我想您的意思是键入list
。如果您没有在那里使用+
,资源编译器将能够告诉您错误。您已经接近了,只是还没有
您应该避免使用自己定义的常量作为联系人提供程序的内容URI
您应该在android.support.v4.content.CursorLoader中使用CursorLoader的支持库实现,而不要使用managedQuery
您没有正确使用CursorLoader。有一个关于CursorLoader的培训课程,演示如何使用支持库版本,位于
可能发生的情况是,您正在使用Honeycom或更低版本的平台运行emulator,然后使用managedQuery。这是可行的。您的设备可能正在使用更高版本的平台,然后尝试使用CursorLoader,但失败了
您的想法是正确的,您只需要使用正确的策略!先生,我在jelly bean emulator中检查了sane应用程序,它工作正常,但不知何故,该应用程序无法在真实设备上工作。这怎么可能?