I am reading data from a microcontroller from usb serial port.
Based on the data i receive i want to replace the fragments.
When i try my below code on button click the fragment gets replaced perfectly. But when i try to replace the fragment under some other conditions like is strings matched then it is not working.
It is not giving any error also. It just restarts the application.
I am using two fragmets firstscreen and RinseFragment.
On activity start firstscreen fragment is added . on string matched RinseFragment should replace firstscreen fragment.
Please refer my code below.
public class ProcessActivity extends FragmentActivity {public Physicaloid phy;TextView status;Handler mHandler = new Handler();@Overrideprotected 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(); } }Now my main activity xml file
Now my fragment files both are same with no contents , empty xml files.
FirstScreen.java
public class FirstScreen extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.firstscreen, container,false);}}
ليست هناك تعليقات:
إرسال تعليق