0

このコード行の getBaseContext() にエラーが表示されます。ここで何が起こるかわかりません単純なアダプターの代わりに配列アダプターを試しましたが、エラーが表示されます。現在、単純なアダプターでやっています..エラーが発生します..

  SimpleAdapter(getBaseContext(), aList, R.layout.list,          
       from, to);

headFragment.java

  public class HeadFragment extends ListFragment {
OnHeadlineSelectedListener mCallback;

// The container Activity must implement this interface so the fragment can deliver 
   messages
public interface OnHeadlineSelectedListener {
    /** Called by HeadlinesFragment when a list item is selected */
    public void onArticleSelected(int position);
}
static String[] Headlines = {
    "Article One",
    "Article Two",
    "Article 3"
};
int[] flags = new int[]{
        R.drawable.ic_11,
        R.drawable.ic_12,
        R.drawable.ic_13

    };
String[] currency = new String[]{
        "Indian Rupee",
        "Pakistani Rupee",
        "Sri Lankan Rupee",

    };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);


 // Each row in the list stores country name, currency and flag
    List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();        

    for(int i=0;i<10;i++){
        HashMap<String, String> hm = new HashMap<String,String>();
        hm.put("txt", "Headlines: " + Headlines[i]);
        hm.put("cur","Currency : " + currency[i]);
        hm.put("flag", Integer.toString(flags[i]) );            
        aList.add(hm);        
    }

    // Keys used in Hashmap
    String[] from = { "flag","txt","cur" };

    // Ids of views in listview_layout
    int[] to = { R.id.flag,R.id.txt,R.id.cur};        

    // Instantiating an adapter to store each items

    SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.list,          
       from, to);
    // Getting a reference to listview of main.xml lagetBayout file
    ListView listView =( ListView ) findViewById(R.id.listview);

    // Setting the adapter to the listView
    listView.setAdapter(adapter);      }

list.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
 >
<ImageView
    android:id="@+id/flag"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:paddingTop="10dp"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
>
    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15dp"
    />

    <TextView
        android:id="@+id/cur"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10dp"
    />
     <!-- Rightend Arrow -->
 <ImageView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/next"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"/>

</LinearLayout>
   </LinearLayout>
4

3 に答える 3

1

getBaseContext()使用禁止getActivity()またはgetApplicationContext()


getApplicationContext()アプリケーション コンテキストはアプリケーションに関連付けられており、ライフ サイクルを通じて常に同じです。


getBasecontext()アクティビティに関連付けられており、アクティビティが破棄されたときに破棄される可能性がある代わりに Context を使用してください。


getActivity()in a Fragment は、フラグメントが関連付けられているアクティビティのコンテキストを返します。


于 2013-04-05T10:19:14.377 に答える
0

@Abhay コードに問題はなく、 getBaseContext( )の代わりにgetApplicationContext()を使用する必要があります。@ DhavalSodhaParmar に完全に同意します。

詳細については、 getBaseContext()getApplicationContext()の違いをお読みください。

于 2013-04-05T10:55:22.690 に答える