gpt4 book ai didi

java - 无法在 fragment 中使用适配器显示 ListView

转载 作者:行者123 更新时间:2023-12-01 11:15:08 46 4
gpt4 key购买 nike

LogCat如下:

08-10 13:41:58.533  19439-19445/? E/jdwp﹕ Failed sending reply to debugger: Broken pipe
08-10 13:42:01.609 19439-19439/com.example.prabhukonchada.ebolamyapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prabhukonchada.ebolamyapp/com.example.prabhukonchada.ebolamyapp.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
at com.example.prabhukonchada.ebolamyapp.MainActivity.onCreate(MainActivity.java:15)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: Binary XML file line #12: Must specify unique android:id, android:tag, or have a parent with an id for com.example.prabhukonchada.ebolamyapp.NewsFragment
at android.app.Activity.onCreateView(Activity.java:4637)
at android.support.v7.app.AppCompatDelegateImplV11.callActivityOnCreateView(AppCompatDelegateImplV11.java:41)
at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:830)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:675)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at com.example.prabhukonchada.ebolamyapp.MainActivity.onCreate(MainActivity.java:15)
            at android.app.Activity.performCreate(Activity.java:5008)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

如果我删除 fragment ,我的代码就可以工作,否则它就不起作用。即使调试器也无法为我找到任何信息。我尝试使用 class 而不是 android:name ,这也不适合我

MainActivity.java

  package com.example.prabhukonchada.ebolamyapp;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

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

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<fragment
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="50dp"
class="com.example.prabhukonchada.ebolamyapp.NewsFragment"
tools:layout="@layout/fragment_news">
</fragment>

</RelativeLayout>

NewsFragment.java

package com.example.prabhukonchada.ebolamyapp;


import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;


/**
* A simple {@link Fragment} subclass.
*/
public class NewsFragment extends Fragment {

ListView mListView;
private List<String> newsList;

public NewsFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View fragementView = inflater.inflate(R.layout.fragment_news, container, false);
mListView =(ListView)fragementView.findViewById(R.id.myListView);

newsList = new ArrayList<String>();
newsList.add("My First Item on the list");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");
newsList.add("More");


mListView.setAdapter(new MyAdapter());

return fragementView;
}

class MyAdapter extends BaseAdapter {

@Override
public int getCount() {
return newsList.size();
}

@Override
public Object getItem(int position) {
return newsList.get(position);
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View rowView = getActivity().getLayoutInflater().inflate(R.layout.row,null);

return rowView;
}
}

}

fragment_news.xml

<FrameLayout 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"
tools:context="com.example.prabhukonchada.ebolamyapp.NewsFragment"
>

<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/myListView"></ListView>

</FrameLayout>

最佳答案

谢谢 Pedro Oliveira 我认为在 fragment 中膨胀 View 存在一些问题。非常抱歉:)

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

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<fragment
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/unquieId"
android:layout_marginTop="50dp"
class="com.example.prabhukonchada.ebolamyapp.NewsFragment"
tools:layout="@layout/fragment_news">
</fragment>

</RelativeLayout>

关于java - 无法在 fragment 中使用适配器显示 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31921193/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com