按钮在片段内单击以新片段

时间:2015-12-08 20:07:15

标签: android android-fragments

我是android的新手,我被卡在从一个片段导航到其他片段上,只需点击一下按钮。在我的fragment_chat.xml中有两个按钮,一个用于chatManager,另一个用于导航到其他片段。问题是当我单击导航按钮(按钮)时,应用程序没有响应。这是我的代码

ChatFragment.java

  package com.example.android.wifidirect.discovery;

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

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

/**
 * This fragment handles chat related UI which includes a list view for messages
 * and a message entry field with send button.
 */
public class WiFiChatFragment extends Fragment {
    private android.support.v4.app.FragmentActivity context;
    private View view;
    private ChatManager chatManager;
    private TextView chatLine;
    private ListView listView;
    ChatMessageAdapter adapter = null;
    private List<String> items = new ArrayList<String>();

    @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);

        adapter = new ChatMessageAdapter(getActivity(), android.R.id.text1,
                items);
        listView.setAdapter(adapter);
        view.findViewById(R.id.button1).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();
                        }
                    }
                });
           view.findViewById(R.id.Button).setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                android.support.v4.app.Fragment fragment = new TopRatedFragment();
                android.support.v4.app.FragmentManager fragmentManager = context.getSupportFragmentManager();
                android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.infotraffic, fragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();``
            }
        });
        return view;
    }

chat_fragment.xml

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

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="50dip"
        android:layout_marginTop="50dp"
        android:transcriptMode="alwaysScroll" >

        <!-- Preview: listitem=@android:layout/simple_list_item_1 -->

    </ListView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dip"
        android:layout_gravity="bottom" >

        <EditText
            android:id="@+id/txtChatLine"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_weight="0.90"
            android:focusableInTouchMode="true"
            android:hint="@string/txt_hint"
            android:visibility="visible" >
        </EditText>

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|right"
            android:layout_weight="0.10"
            android:text="@string/btn_send" />
    </LinearLayout>

    <Button
        android:id="@+id/Button"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="@string/gps_location" />

</FrameLayout>

1 个答案:

答案 0 :(得分:0)

尝试替换代码的这一行:

android.support.v4.app.Fragment fragment = new TopRatedFragment();
android.support.v4.app.FragmentManager fragmentManager = context.getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.infotraffic, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

使用此代码:

TopRatedFragment fragment = new TopRatedFragment();
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.add(R.id.infotraffic, fragment);
transaction.addToBackStack(null);
transaction.commit();