android - 动态替换 fragment 而不是在按钮单击或方向更改时

标签 android android-fragments

我正在通过 USB 串行端口从微 Controller 读取数据。

根据我收到的数据,我想替换 fragment 。

当我在按钮上尝试我的以下代码时,点击 fragment 被完美替换。但是当我尝试在其他一些条件下替换 fragment 时,比如字符串匹配,那么它就不起作用了。

它也没有给出任何错误。它只是重新启动应用程序。

我正在使用两个 fragment firstscreen 和 RinseFragment。

在 Activity 开始时添加了第一个屏幕 fragment 。在字符串匹配的 RinseFragment 上应该替换 firscreen fragment 。

请引用下面我的代码。

public class ProcessActivity extends FragmentActivity {

public Physicaloid phy;
TextView status;
Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_process);



    phy = new Physicaloid(this);

    openDevice();


    IntentFilter filter = new IntentFilter();
    filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
    filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
    registerReceiver(mUsbReceiver, filter);

    status = (TextView)findViewById(R.id.status);

    FirstScreen fs = new FirstScreen();
     getFragmentManager().beginTransaction().add(R.id.fragment_container, fs).commit();

}

 private void openDevice() {
        if (!phy.isOpened()) {
            if (phy.open()) { // default 9600bps
                    phy.addReadListener(new ReadLisener() {
                    String readStr;
                    @Override
                    public void onRead(int size) {
                        byte[] buf = new byte[size];

                        phy.read(buf, size);
                        try {
                            readStr = new String(buf, "UTF-8");
                           modeselector(readStr);
                           finish();
                        } catch (UnsupportedEncodingException e) {

                        }

                    }
                });
            }
        }
    } 

 public void modeselector(String s){

     if(s.equals("R_A")){

         RinseFragment rFragment = new RinseFragment();
                        FragmentTransaction transaction = getFragmentManager().beginTransaction();
                       transaction.replace(R.id.fragment_container, rFragment);
                       transaction.addToBackStack(null);
                       transaction.commit();
     }
     if(s.equals("R_S")){

         tvAppend(status,"RINSING . . .");

     }

     else
     {

     }

 }

 BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
                closeDevice();
            }
        }
    };

    private void closeDevice() {
        if(phy.close()) {
          phy.clearReadListener();         
        }
    }

现在我的主要 Activity 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"

tools:context=".ProcessActivity" >

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="80dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:background="#10000000" >

    <Button
        android:id="@+id/rinse"
        android:layout_width="175dp"
        android:layout_height="60dp"
        android:text="RINSE" 
        android:textSize="20sp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:shadowColor="#000000"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="0"
        android:background="#53CF29"
        android:onClick="onrinse"/>

    <Button
        android:id="@+id/dprep"
        android:layout_width="175dp"
        android:layout_height="60dp"
        android:textSize="20sp"
        android:text="PREPARATION" 
        android:layout_marginTop="10dp"
        android:layout_marginLeft="5dp"
        android:shadowColor="#000000"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="0"
        />

    <Button
        android:id="@+id/dialysis"
         android:layout_width="175dp"
        android:layout_height="60dp"
        android:textSize="20sp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="5dp"
        android:text="DIALYSIS" 
         android:shadowColor="#000000"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="0"
        />

    <Button
        android:id="@+id/disinfect"
         android:layout_width="175dp"
        android:layout_height="60dp"
        android:textSize="20sp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="5dp"
        android:text="DISINFECT"
         android:shadowColor="#000000"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="0"
        />

    <AnalogClock
        android:id="@+id/analogClock1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_marginLeft="200dp"/>

</LinearLayout>

<FrameLayout 
    android:id="@+id/fragment_container"
    android:layout_below="@id/linearLayout1"
    android:layout_above="@+id/statusbar"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    >

</FrameLayout>

<LinearLayout
    android:id="@+id/statusbar"
    android:layout_width="fill_parent"
    android:layout_height="60dp"
    android:layout_alignLeft="@+id/linearLayout1"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/linearLayout1"
    android:background="#30000000" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="10dp"
        android:text="STATUS : "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CALIBRATION FINISHED"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="20dp"
        android:textSize="20sp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

现在我的 fragment 文件都没有内容,都是空的 xml 文件。

FirstScreen.java

public class FirstScreen extends Fragment {


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

最佳答案

嘿对不起伙计们我犯了一个愚蠢的错误。我在替换 fragment 后调用了 finish() 。非常抱歉。

关于android - 动态替换 fragment 而不是在按钮单击或方向更改时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20760878/

相关文章:

Android > 4.0 fragment 和标签

android - 如何找出包中的 Activity 名称?安卓。亚行 shell

android - 多 fragment 最佳实践

java.lang.ClassCastException : android. support.transition.Fade 无法转换为 android.transition.Transition?

java - 在抽屉导航项目单击上切换 fragment

android - Fragment setRetainInstance(true) 保存View属性

java - Android SQLite ModernAsyncTask #1

java - 在Android中创建多个目录

android - 如何对未发布的应用程序进行 Beta 测试?

android - 对图像应用多个滤镜