我有两个名为WiFiChatFragment的片段,另一个是AboutUsFrag 所以,我想从WiFiChatFragment调用AboutUsFrag,但是我已经实现了溢出菜单项,但它在替换方法时出错 这是我的WiFiChatFragment代码
public class WiFiChatFragment extends Fragment {
private View view;
private ChatManager chatManager;
private TextView chatLine;
private ListView listView;
public Animation hyperspaceJump;
public AboutUsFrag fragment=null;
public static final String TAG = "Settings";
ChatMessageAdapter adapter = null;
private List<String> items = new ArrayList<String>();
Context context;
private FragmentTransaction ft;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_chat, container, false);
chatLine = (TextView) view.findViewById(R.id.txtChatLine);
listView = (ListView) view.findViewById(android.R.id.list);
hyperspaceJump = AnimationUtils.loadAnimation(getActivity(), R.anim.animation_leave);
chatLine.startAnimation(hyperspaceJump);
setHasOptionsMenu(true);
adapter = new ChatMessageAdapter(getActivity(), android.R.id.text1,
items);
listView.setAdapter(adapter);
view.findViewById(R.id.btn_send).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (chatManager != null) {
chatManager.write(chatLine.getText().toString()
.getBytes());
pushMessage("Me: " + chatLine.getText().toString());
chatLine.setText("");
//chatLine.clearFocus();
}
}
});
return view;
}
public interface MessageTarget {
public Handler getHandler();
}
public void setChatManager(ChatManager obj) {
chatManager = obj;
}
public void pushMessage(String readMessage) {
adapter.add(readMessage);
adapter.notifyDataSetChanged();
}
/**
* ArrayAdapter to manage chat messages.
*/
public class ChatMessageAdapter extends ArrayAdapter<String> {
List<String> messages = null;
public ChatMessageAdapter(Context context, int textViewResourceId,
List<String> items) {
super(context, textViewResourceId, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(android.R.layout.simple_list_item_1, null);
}
String message = items.get(position);
if (message != null && !message.isEmpty()) {
TextView nameText = (TextView) v
.findViewById(android.R.id.text1);
if (nameText != null) {
nameText.setText(message);
if (message.startsWith("Me: ")) {
nameText.setBackgroundResource(R.drawable.out_message_bg );
nameText.setTextAppearance(getActivity(),
R.style.normalText);
} else {
nameText.setBackgroundResource(R.drawable.in_message_bg );
nameText.setTextAppearance(getActivity(),
R.style.boldText);
}
}
}
return v;
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_main, menu);
}
public void replaceFragment(Fragment someFragment ) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.about_frag, someFragment);
transaction.addToBackStack(null);
transaction.commit();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle item selection
switch (item.getItemId()) {
case R.id.action_settings:
// do s.th.
return true;
case R.id.clean:
adapter.clear();
return true;
case R.id.about_us:
fragment = new AboutUsFrag();
replaceFragment(fragment);
return true;
case R.id.rating:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
和另一个名为AboutUsFrag的类
public class AboutUsFrag extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_about_us, container, false);
}
}
我该如何实现?
答案 0 :(得分:0)
检查导入部分。
您需要导入
import android.support.v4.app.Fragment
我希望它有效。
答案 1 :(得分:0)
让我们尝试
import android.app.Fragment;
并以这种方式替换片段
getFragmentManager().beginTransaction().replace(R.id.frame_container,frgment,"fragment_id").commit();