list.setAdapter(适配器)错误

时间:2013-11-17 13:20:39

标签: android listview android-listview android-fragments android-arrayadapter

我的Mainactivity来自Activity

public class MainActivity extends Activity {
ProduitDataSource datasoure;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    datasoure = new ProduitDataSource(this);
    datasoure.open();
    FragmentManager fm = getFragmentManager();
    FragmentTransaction trans = fm.beginTransaction();
    Start_Fragment start = new Start_Fragment();
    trans.add(R.id.place_fragment, start);
    trans.commit();

}

public void gotocontact(View view) {
    FragmentManager fm = getFragmentManager();

    FragmentTransaction transaction = fm.beginTransaction();
    Contact_Fragment f1 = new Contact_Fragment();

    transaction.replace(R.id.place_fragment, f1);
    transaction.commit();
}

public void gotoitem(View view) {
    FragmentManager fm = getFragmentManager();
    FragmentTransaction transiction = fm.beginTransaction();
    List_produit_fragment f = new List_produit_fragment();
    transiction.replace(R.id.place_fragment, f);
    transiction.commit();

    List<Produit> values;
     values= datasoure.getAllProduit();
    ListView list ;
    list = (ListView)findViewById(R.id.list);
    ArrayAdapter<Produit> adapter;
    adapter = new ArrayAdapter<Produit>(this,
                android.R.layout.simple_list_item_1,values);


     list.setAdapter(adapter);


} 
}

和这个片段类:

   public class List_produit_fragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedIntanteState) {
    View view;
    view = inflater.inflate(R.layout.list_produit, container, false);

    return view;
}
}

main_activity.xml:

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<LinearLayout
    android:layout_width="74dp"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_weight="0.15"
    android:orientation="vertical" >

    <Button
        android:id="@+id/prodlist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="gotoitem"
        android:text="Liste des produits" />

    <Button
        android:id="@+id/contact"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="gotocontact"
        android:text="Contact" />

</LinearLayout>

<LinearLayout
    android:id="@+id/place_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_toRightOf="@+id/linearLayout1"
    android:layout_weight="3"
    android:orientation="vertical" >

</LinearLayout>

和片段xml:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Liste_produit" >

<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="350dp"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true" >

</ListView>

</RelativeLayout>

logcat的:

11-17 08:38:39.788: E/AndroidRuntime(799): FATAL EXCEPTION: main
11-17 08:38:39.788: E/AndroidRuntime(799): java.lang.IllegalStateException: Could not execute method of the activity
11-17 08:38:39.788: E/AndroidRuntime(799):  at android.view.View$1.onClick(View.java:3633)
11-17 08:38:39.788: E/AndroidRuntime(799):  at android.view.View.performClick(View.java:4240)
11-17 08:38:39.788: E/AndroidRuntime(799):  at android.view.View$PerformClick.run(View.java:17721)
11-17 08:38:39.788: E/AndroidRuntime(799):  at android.os.Handler.handleCallback(Handler.java:730)
11-17 08:38:39.788: E/AndroidRuntime(799):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-17 08:38:39.788: E/AndroidRuntime(799):  at android.os.Looper.loop(Looper.java:137)
11-17 08:38:39.788: E/AndroidRuntime(799):  at android.app.ActivityThread.main(ActivityThread.java:5103)
11-17 08:38:39.788: E/AndroidRuntime(799):  at java.lang.reflect.Method.invokeNative(Native Method)
11-17 08:38:39.788: E/AndroidRuntime(799):  at java.lang.reflect.Method.invoke(Method.java:525)
11-17 08:38:39.788: E/AndroidRuntime(799):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-17 08:38:39.788: E/AndroidRuntime(799):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-17 08:38:39.788: E/AndroidRuntime(799):  at dalvik.system.NativeStart.main(Native Method)
11-17 08:38:39.788: E/AndroidRuntime(799): Caused by: java.lang.reflect.InvocationTargetException
11-17 08:38:39.788: E/AndroidRuntime(799):  at java.lang.reflect.Method.invokeNative(Native Method)
11-17 08:38:39.788: E/AndroidRuntime(799):  at java.lang.reflect.Method.invoke(Method.java:525)
11-17 08:38:39.788: E/AndroidRuntime(799):  at android.view.View$1.onClick(View.java:3628)
11-17 08:38:39.788: E/AndroidRuntime(799):  ... 11 more
11-17 08:38:39.788: E/AndroidRuntime(799): Caused by: java.lang.NullPointerException
11-17 08:38:39.788: E/AndroidRuntime(799):  at com.isims.store.MainActivity.gotoitem(MainActivity.java:65)
11-17 08:38:39.788: E/AndroidRuntime(799):  ... 14 more

新日志

11-17 08:48:51.518: E/AndroidRuntime(852): FATAL EXCEPTION: main
11-17 08:48:51.518: E/AndroidRuntime(852): java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
11-17 08:48:51.518: E/AndroidRuntime(852):  at android.app.ListFragment.ensureList(ListFragment.java:402)
11-17 08:48:51.518: E/AndroidRuntime(852):  at android.app.ListFragment.onViewCreated(ListFragment.java:203)
11-17 08:48:51.518: E/AndroidRuntime(852):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:899)
11-17 08:48:51.518: E/AndroidRuntime(852):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
11-17 08:48:51.518: E/AndroidRuntime(852):  at android.app.BackStackRecord.run(BackStackRecord.java:682)
11-17 08:48:51.518: E/AndroidRuntime(852):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
11-17 08:48:51.518: E/AndroidRuntime(852):  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
11-17 08:48:51.518: E/AndroidRuntime(852):  at android.os.Handler.handleCallback(Handler.java:730)
11-17 08:48:51.518: E/AndroidRuntime(852):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-17 08:48:51.518: E/AndroidRuntime(852):  at android.os.Looper.loop(Looper.java:137)
11-17 08:48:51.518: E/AndroidRuntime(852):  at android.app.ActivityThread.main(ActivityThread.java:5103)
11-17 08:48:51.518: E/AndroidRuntime(852):  at java.lang.reflect.Method.invokeNative(Native Method)
11-17 08:48:51.518: E/AndroidRuntime(852):  at java.lang.reflect.Method.invoke(Method.java:525)
11-17 08:48:51.518: E/AndroidRuntime(852):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-17 08:48:51.518: E/AndroidRuntime(852):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-17 08:48:51.518: E/AndroidRuntime(852):  at dalvik.system.NativeStart.main(Native Method)
11-17 08:54:34.389: E/AndroidRuntime(879): FATAL EXCEPTION: main
11-17 08:54:34.389: E/AndroidRuntime(879): java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
11-17 08:54:34.389: E/AndroidRuntime(879):  at android.app.ListFragment.ensureList(ListFragment.java:402)
11-17 08:54:34.389: E/AndroidRuntime(879):  at android.app.ListFragment.onViewCreated(ListFragment.java:203)
11-17 08:54:34.389: E/AndroidRuntime(879):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:899)
11-17 08:54:34.389: E/AndroidRuntime(879):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
11-17 08:54:34.389: E/AndroidRuntime(879):  at android.app.BackStackRecord.run(BackStackRecord.java:682)
11-17 08:54:34.389: E/AndroidRuntime(879):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
11-17 08:54:34.389: E/AndroidRuntime(879):  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
11-17 08:54:34.389: E/AndroidRuntime(879):  at android.os.Handler.handleCallback(Handler.java:730)
11-17 08:54:34.389: E/AndroidRuntime(879):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-17 08:54:34.389: E/AndroidRuntime(879):  at android.os.Looper.loop(Looper.java:137)
11-17 08:54:34.389: E/AndroidRuntime(879):  at android.app.ActivityThread.main(ActivityThread.java:5103)
11-17 08:54:34.389: E/AndroidRuntime(879):  at java.lang.reflect.Method.invokeNative(Native Method)
11-17 08:54:34.389: E/AndroidRuntime(879):  at java.lang.reflect.Method.invoke(Method.java:525)
11-17 08:54:34.389: E/AndroidRuntime(879):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-17 08:54:34.389: E/AndroidRuntime(879):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-17 08:54:34.389: E/AndroidRuntime(879):  at dalvik.system.NativeStart.main(Native Method)

但是,list.setAdapter(adapter)不起作用,它在logcat中显示错误。

Idk问题在哪里,因为当我在一个简单的活动上尝试它时它可以正常工作

2 个答案:

答案 0 :(得分:0)

activity_main.xml

没有ListView

所以下面的

ListView list ;
list = (ListView)findViewById(R.id.lis); // should be in framgent

更改片段xml

中listview的ID
<ListView android:id="@android:id/list"

在片段onCreateView

ProduitDataSource datasoure;
public class List_produit_fragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedIntanteState) {

    View view = inflater.inflate(R.layout.list_produit, container, false);
    return view;
    }

覆盖onActivityCreated

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ListView list = getListView();  
    datasoure = new ProduitDataSource(getActivity());
    datasoure.open();
    List<Produit> values= datasoure.getAllProduit();
    ArrayAdapter<Produit> adapter;
    adapter = new ArrayAdapter<Produit>(getActivity(),android.R.layout.simple_list_item_1,values);
    list.setAdapter(adapter);

}

答案 1 :(得分:0)

我认为你导入了错误的R类;请确保导入com.isims.store.MainActivity.R而不是android.R