开发者社区> 问答> 正文

学习fragment遇到的问题?报错

package com.activities;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.ArrayList;

import com.fragments.MyListFragment;
import com.fragments.MyListFragment.OnListListener;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;

@SuppressLint("NewApi")
public class MainActivity extends Activity implements OnListListener{

	private ArrayAdapter aa;
	private ArrayList<String> items;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
		
		FragmentManager fm = getFragmentManager();
		MyListFragment mf = (MyListFragment) fm.findFragmentById(R.id.MyListFragment);
		
		//创建ArrayList
		items = new ArrayList<String>();
		
		//创建ArrayAdapter用来将数组绑定到listview上
		aa = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, items);
		
		//将ArrayAdapter绑定到ListView上
		mf.setListAdapter(aa);
	}



	@Override
	public void onListListener(String list) {
		// TODO Auto-generated method stub
		items.add(list);
		aa.notifyDataSetChanged();
	}




	@Override
	public boolean isDestroyed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
		// TODO Auto-generated method stub
		
	}
}

package com.fragments;



import com.activities.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


@SuppressLint("NewApi")
public class MyListFragment extends ListFragment {

	private OnListListener mOnListListener;
	
	public interface OnListListener{
		public void onListListener(String list);
	}
	
	
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		
		try{
			mOnListListener = (OnListListener)getActivity();
		}catch(ClassCastException e){
			throw new ClassCastException(activity.toString()+"must implements OnListListener.");
		}
		
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return  inflater.inflate(R.layout.list_fragment, container, 	false);
		
	}
}



以上是主Activity类和其中一个Fragment类

下面是activity的布局xml:activity_main_xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment_container_layout"
    android:orientation="vertical">
    
    <fragment
        android:name="com.fragments.DetailsFragment"
        android:id="@+id/DetailsFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    
    <fragment
        android:name="com.fragments.MyListFragment"
        android:id="@+id/MyListFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    
    </LinearLayout>



下面是报错:




展开
收起
爱吃鱼的程序员 2020-06-09 15:48:20 536 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    你的写法好奇怪啊,fragment不应该都是java文件实例的吗,你的为什么是通过id从xml里面找出来的fragment标签

    fragment有两种加载方式。一种是代码动态加载,一种是xml布局静态加载。你说的应该是动态加载方式。问题已经处理了,不过感谢您的关注!
    2020-06-09 15:48:36
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Flutter in action 立即下载
《Flutter in action》 立即下载
Android组件化实现 立即下载