2017-04-14 80 views
0

在我的主视图应用程序中有3个选项卡。这3个标签填充了带有查看页面适配器的独立片段。在我的第二个选项卡中,有一个列表视图(包含在一个片段中)。当用户点击一个列表视图时,我需要在那里显示另一个片段。但是,当用户点击一个列表视图项目时,我得到了这样的错误片段内的Listview onitemclick不起作用

No view found for id 0x7f0e008a (com.example.abc.myapp:id/new_output) for fragment MantraFragment{30391e5 #3 id=0x7f0e008a} 

我在其他问题在stackoverflow中发现此错误。试过他们的解决方案,但没有奏效。请帮我解决这个问题。

类,其中包括列表视图

public class TwoFragment extends Fragment { 
private ListView listView; 
private String items[]; 


public TwoFragment() { 
} 

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

    items = getResources().getStringArray(R.array.races_array); 

} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_two, container, false); 

    listView = (ListView) rootView.findViewById(R.id.mylistView); 
    CustomListAdapter adapter = new CustomListAdapter(getActivity(), items); 
    listView.setAdapter(adapter); 

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
           int position, long id) { 

      FragmentManager fm = getActivity().getSupportFragmentManager(); 
      android.support.v4.app.FragmentTransaction ft = fm.beginTransaction(); 


      Fragment newFrag = new MyNewFragment(); 
      Bundle arguments = new Bundle(); 
      newFrag.setArguments(arguments); 
      ft.replace(R.id.new_output, newFrag); 
      ft.addToBackStack(null); 
      ft.commit(); 

     } 
    }); 

    return rootView; 
}} 

MyNewFragment.class

public class MyNewFragment extends Fragment{ 

public MyNewFragment(){ 
    } 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

    } 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    View rootView= inflater.inflate(R.layout.mynewfrag_view, container, false); 

    return rootView; 
}} 

MyNewFragment视图(mynewfrag_view)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:gravity="center" 

    android:layout_height="match_parent"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:textColor="#FF0000" 
     android:textSize="20sp" 
     android:id="@+id/msg2"/> 


    <fragment 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#b0b0ff" 
     android:id="@+id/new_output" /> 


</LinearLayout> 

MainActivity类别

public class MainActivity extends AppCompatActivity { 

    private Toolbar toolbar; 
    private TabLayout tabLayout; 
    private ViewPager viewPager; 



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

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     viewPager = (ViewPager) findViewById(R.id.viewpager); 
     setupViewPager(viewPager); 

     tabLayout = (TabLayout) findViewById(R.id.tabs); 
     tabLayout.setupWithViewPager(viewPager); 
    } 


private void setupViewPager(ViewPager viewPager) { 
     ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); 
     adapter.addFragment(new OneFragment(), "One"); 
     adapter.addFragment(new TwoFragment(), "Two"); 
     adapter.addFragment(new ThreeFragment(), "Three"); 

    viewPager.setAdapter(adapter); 
    } 



public class ViewPagerAdapter extends FragmentPagerAdapter { 
     private final List<Fragment> mFragmentList = new ArrayList<>(); 
     private final List<String> mFragmentTitleList = new ArrayList<>(); 

    public ViewPagerAdapter(FragmentManager manager) { 
     super(manager); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     return mFragmentList.get(position); 
    } 

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

    public void addFragment(Fragment fragment, String title) { 
     mFragmentList.add(fragment); 
     mFragmentTitleList.add(title); 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return mFragmentTitleList.get(position); 
    } }} 

MainActivity布局(activity_main)

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:layout_scrollFlags="scroll|enterAlways" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:tabMode="fixed" 
     app:tabGravity="fill"/> 
</android.support.design.widget.AppBarLayout> 

<android.support.v4.view.ViewPager 
    android:id="@+id/viewpager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

PS:我仍然在Android开发初学者,我认为这是很好用的一个片段有insted的展示活动中。 (纠正我,如果我错了)

+0

你可以请张贴有关您是如何加载标签页的代码?可能是你正在加载基片段容器中的子片段。这就是你遇到问题的地方。 –

+0

@ TrickySolutons-非常感谢。我已经更新了代码。 – venura

+0

'<片段 机器人:layout_width = “match_parent” 机器人:layout_height = “match_parent” 机器人:背景= “#b0b0ff” 机器人:ID = “@ + ID/new_output”/>' 上面的FrameLayout替换 '<的FrameLayout 机器人:layout_width = “match_parent” 机器人:layout_height = “match_parent” 机器人:背景= “#b0b0ff” 机器人:ID = “@ + ID/new_output”/>' 应该解决您问题 –

回答

0

您正在添加MyNewFragment并给出id的视图存在于my_newfragment.xml这是仍然未知。您必须在TwoFragment的布局文件中添加/替换片段,即fragment_two.xml

fragment_two.xml应该像

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:gravity="center" 
    android:layout_height="match_parent"> 

    .... 

    <fragment 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#b0b0ff" 
     android:id="@+id/new_output" /> 

    .... 

</LinearLayout> 

mynewfrag_view.xml删除fragment标签new_outputfragment_two.xml添加像上面,并检查是否正常工作。

+0

我添加了一个类上面的代码与 <片段 的android:layout_width = “match_parent” 机器人:layout_height = “match_parent” 机器人:背景= “#b0b0ff” 机器人:ID = “@ + ID/new_output” 类= “com.abc.myapp.view.MyNewFragment” /> 现在有没有错误。但是当我点击一个项目时,什么都没有发生 – venura

+0

不要添加'class'属性。你有没有在'two_fragment.xml'中定义这个'fragment'标签? –

+0

我已经加入此内部two_fragment.xml <片段 机器人:layout_width = “match_parent” 机器人:layout_height = “match_parent” 机器人:背景= “#b0b0ff” 工具:布局= “@布局/ mynewfrag_view” Android:id =“@ + id/new_output”/> 现在得到这个错误android.view.InflateException:二进制XML文件行#17:二进制XML文件行#17:错误扩充类片段 引起:android.view .InflateException:引起:java.lang.NullPointerException – venura

0

这是不认识你的R.id.new_output在onclicklistener使用在TwoFragment类 ft.replace(R.id.new_output, newFrag);