2013-02-15 77 views
-1

我正在使用在ICS上运行的Xperia。这是我编写的一个应用程序,用于了解内容提供者的工作方式。该应用程序在GingerBread仿真器上运行良好,但没有在我的手机中显示。导致应用程序行为如此的概率是什么? 这里是android清单的代码。内容提供商无法正常工作

<?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; 
    } 

} 

我无法解决它中的问题。 在此先感谢。

回答

0

你很近,只是还没有。

  1. 您应该避免使用自己定义的常量作为联系人提供程序的内容URI。
  2. 您应该在android.support.v4.content.CursorLoader中使用CursorLoader的支持库实现,而不是使用managedQuery。
  3. 您没有正确使用CursorLoader。有用于CursorLoader培训班,演示如何使用支持库的版本,在 http://developer.android.com/training/load-data-background/index.html

什么可能发生的情况是,你正在运行Honeycomb的或更小的平台版本,然后使用managedQuery模拟器。这样可行。您的设备可能正在使用更高版本的平台,然后尝试使用CursorLoader并失败。

你有正确的想法,你只需要使用正确的策略!

0

您将您的ListView ID设置为@+android:id/lst。我想你打算输入list。如果你没有在那里使用+,那么资源编译器就可以告诉你有关你的错误。

+0

先生,我检查了果冻豆模拟器中的理智应用程序,它运行良好,但不知何故,这个应用程序不适用于真正的设备。这怎么可能? – Paras 2013-02-16 06:27:29