2013-02-15 79 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