所以我试图将主要活动中的字符串值传递给片段,然后更改该片段中的textView以反映字符串值。但是,该方法未被调用,textView不会更改。我在这里做错了什么?
MainActivity.java:
//mainActivity
public class MainActivity extends Activity{
public FragmentCommunicator fragmentCommunicator;
//Declare popup button
Button button1;
// Declare Tab Variable
ActionBar.Tab Tab1, Tab2, Tab3;
Fragment fragmentTab1 = new FragmentTab1();
Fragment fragmentTab2 = new FragmentTab2();
Fragment fragmentTab3 = new FragmentTab3();
//public String graphsForce;
final int RECIEVE_MESSAGE = 1;
TextView txtArduino;
Handler h;
//bt variables
BluetoothAdapter btAdapter= BluetoothAdapter.getDefaultAdapter();
BluetoothSocket bs = null;
BluetoothDevice bd = btAdapter.getRemoteDevice("20:14:05:06:18:36");
StringBuilder sb = new StringBuilder();
OutputStream mmOutputStream;
InputStream mmInputStream;
Thread workerThread;
byte[] readBuffer;
int readBufferPosition;
int counter;
volatile boolean stopWorker;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//connect members
ActionBar actionBar = getActionBar();
// Hide Actionbar Icon
actionBar.setDisplayShowHomeEnabled(false);
// Hide Actionbar Title
actionBar.setDisplayShowTitleEnabled(false);
// Create Actionbar Tabs
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set Tab Icon and Titles
Tab1 = actionBar.newTab().setIcon(R.drawable.ic_action_alarms);
Tab2 = actionBar.newTab().setIcon(R.drawable.ic_action_picture);
Tab3 = actionBar.newTab().setIcon(R.drawable.ic_action_save);
// Set Tab Listeners
Tab1.setTabListener(new TabListener(fragmentTab1));
Tab2.setTabListener(new TabListener(fragmentTab2));
Tab3.setTabListener(new TabListener(fragmentTab3));
// Add tabs to actionbar
actionBar.addTab(Tab1);
actionBar.addTab(Tab2);
actionBar.addTab(Tab3);
txtArduino = (TextView) findViewById(R.id.txtArduino);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v){
//Creating popupmenu
PopupMenu popup = new PopupMenu(MainActivity.this, button1);
//inflating the popup menu
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
//register popup with onMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId()){
case R.id.btturnon:
if(!btAdapter.isEnabled()){
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
}
break;
case R.id.btconnect:
//Connect to known address
try{
bs =bd.createInsecureRfcommSocketToServiceRecord
(UUID.fromString("00001101-0000-1000-8000- 00805F9B34FB"));
}
catch(IOException io){
Toast.makeText(getApplicationContext(), "Socket Create : " +
io.toString() , Toast.LENGTH_SHORT).show();
}
try{
btAdapter.cancelDiscovery();
bs.connect();
mmOutputStream = bs.getOutputStream();
mmInputStream = bs.getInputStream();
}
catch(IOException io){
Log.e("Socket Connect", io.toString());
Toast.makeText(getApplicationContext(), "Socket Connect : " +
io.toString() , Toast.LENGTH_LONG).show();
}
beginListenForData();
txtArduino.setText("Bluetooth channel Openned");
break;
}
//Toast.makeText(MainActivity.this, "You Clicked : " + item.getTitle(),
Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();// shows the popup menu
}
});
}
void beginListenForData(){
final Handler handler = new Handler();
final byte delimiter = 10;
stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable()
{
public void run()
{
while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
try
{
int bytesAvailable = mmInputStream.available();
if(bytesAvailable >0)
{
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
for(int i = 0; i<bytesAvailable;i++){
byte b = packetBytes[i];
if(b == delimiter)
{
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0,
encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
handler.post(new Runnable() {
public void run(){
txtArduino.setText(data);
onPassToFragment(data);
}
});
}
else {
readBuffer[readBufferPosition++]=b;
}
}
}
} catch (IOException ex) {
stopWorker = true;
}
}
}
});
workerThread.start();
}
void closeBT() throws IOException {
stopWorker = true;
mmOutputStream.close();
mmInputStream.close();
bs.close();
txtArduino.setText("Bluetooth Closed");
}
public void onPassToFragment(String string){
if(fragmentCommunicator != null){
fragmentCommunicator.passDataToFragment(string);
}
}
}
我的片段:
public class FragmentTab2 extends Fragment implements FragmentCommunicator {
private TextView graphsForce;
private String graphs="Nothing yet";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment2, container, false);
graphsForce = (TextView) rootView.findViewById(R.id.graphs);
return rootView;
}
public void passDataToFragment(String someValue){
graphs = someValue;
graphsForce.setText(graphs);
}
}
接口FragmentCommunicator:
package com.example.eightcount;
public interface FragmentCommunicator {
public void passDataToFragment(String someValue);
}
.xml fragment2:
<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"
android:id="@+id/fragment2" >
<TextView
android:id="@+id/graphs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/graphs"
/>
</RelativeLayout>
对此的任何帮助都将非常感激,我已经挂了好几天了。
谢谢!
答案 0 :(得分:0)
fragmentCommunicator为null,它永远不会被设置。
也许你可以直接在片段上调用它:fragmentTab2.passDataToFragment(String)