繁体   English   中英

动态替换片段,无需单击按钮或更改方向

[英]Replacing fragments dynamically not on button click or orientation change

我正在从USB串行端口的微控制器读取数据。

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

当我在按钮上尝试下面的代码时,片段将被完美替换。 但是,当我尝试在某些其他条件下替换片段时,例如字符串匹配,则它不起作用。

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

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

在活动开始时,将添加firstscreen片段。 匹配的字符串上的RinseFragment应该替换firstscreen片段。

请参考下面的代码。

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();         
        }
    }

现在我的主要活动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>

现在我的片段文件都一样,没有内容,空的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);
}

}

嘿,抱歉,我犯了一个愚蠢的错误。 替换片段后,我正在调用finish()。 非常抱歉。

修改openDevice()方法内的try-catch块以获取错误日志

 try {
       //your code
  } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
  }catch (Exception e) {
        e.printStackTrace();
  }

这将打印错误日志,然后您可以确定确切的问题。 然后,当您发布错误日志时,stackoverflow人员很容易回答您的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM