2016-07-25 23 views
0

TextViewを変更して、フラグメント内のBluetooth接続のステータスを表示しようとしていますが、msgReceived.setText(string)が呼び出されたときに何も起こりません。これをどうやってやりますか?同じアクティビティからフラグメント内のTextViewのテキストを変更できません

package dleedesign.dubcommunicationstestapp; 

import android.app.Fragment; 
import android.bluetooth.BluetoothAdapter; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.support.annotation.Nullable; 
import android.view.LayoutInflater; 
import android.util.Log; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.TextView; 

public class SecondFragment extends Fragment implements View.OnClickListener { 

View myView; 

public final String TAG = "Main"; 
private Bluetooth bt; 
public Button sendCommand; 
public TextView msgReceived; 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    myView = inflater.inflate(R.layout.second_layout, container, false); 

    sendCommand = (Button) inflater.inflate(R.layout.second_layout, container, false).findViewById(R.id.sendCommand); 
    msgReceived = (TextView) inflater.inflate(R.layout.second_layout, container, false).findViewById(R.id.msgReceived); 
    msgReceived.setText("Ready to connect"); 

    bt = new Bluetooth(new MainActivity(), mHandler); 
    connectService(); 

    return myView; 
} 

@Override 
public void onClick(View v) 
{ 
    connectService(); 
} 

public void connectService() 
{ 
    try { 
     msgReceived.setText("Connecting..."); 
     BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter(); 
     if(btAdapter.isEnabled()) 
     { 
      bt.start(); 
      bt.connectDevice("HC-06"); 
      Log.d(TAG, "Btservice started- listening"); 
      msgReceived.setText("Connected!"); 
     } 
     else 
     { 
      Log.w(TAG, "Btservice started - bluetooth is not enabled"); 
      msgReceived.setText("Bluetooth not enabled"); 
     } 
    } catch (Exception e) { 
     Log.e(TAG, "Unable to start bluetooth", e); 
     msgReceived.setText("Unable to connect: " + e); 
    } 
} 

private final Handler mHandler = new Handler() 
{ 
    @Override 
    public void handleMessage(Message msg) 
    { 
     switch (msg.what) 
     { 
      case Bluetooth.MESSAGE_STATE_CHANGE: 
       Log.d(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1); 
       break; 
      case Bluetooth.MESSAGE_WRITE: 
       Log.d(TAG, "MESSAGE_WRITE"); 
       break; 
      case Bluetooth.MESSAGE_READ: 
       Log.d(TAG, "MESSAGE_READ"); 
       break; 
      case Bluetooth.MESSAGE_DEVICE_NAME: 
       Log.d(TAG, "MESSAGE_DEVICE_NAME " + msg); 
       break; 
      case Bluetooth.MESSAGE_TOAST: 
       Log.d(TAG, "MESSAGE_TOAST " + msg); 
       break; 
     } 
    } 
}; 

} 

EDIT:ここ 私はSecondFragmentを選択したときに吐き出しLogCatメッセージです:

dleedesign.dubcommunicationstestapp D/BluetoothService: Bounded device 40:EF:4C:C2:A9:32 
dleedesign.dubcommunicationstestapp D/BluetoothService: Bounded device 98:D3:31:70:80:C5 
dleedesign.dubcommunicationstestapp D/BluetoothService: start 
dleedesign.dubcommunicationstestapp D/BluetoothService: setState() 0 -> 1 
dleedesign.dubcommunicationstestapp W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback 
dleedesign.dubcommunicationstestapp D/BluetoothService: Socket Type: nullBEGIN mAcceptThreadThread[Thread-20627,5,main] 
dleedesign.dubcommunicationstestapp D/BluetoothService: connect to: 98:D3:31:70:80:C5 
dleedesign.dubcommunicationstestapp D/BluetoothService: setState() 1 -> 2 
dleedesign.dubcommunicationstestapp D/Main: Btservice started- listening 
dleedesign.dubcommunicationstestapp I/BluetoothService: BEGIN mConnectThread SocketType:null 
dleedesign.dubcommunicationstestapp W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback 
dleedesign.dubcommunicationstestapp D/Main: MESSAGE_STATE_CHANGE: 1 
dleedesign.dubcommunicationstestapp D/Main: MESSAGE_STATE_CHANGE: 2 
dleedesign.dubcommunicationstestapp E/BluetoothService: Unable to connect socket 
                         java.io.IOException: read failed, socket might closed or timeout, read ret: -1 
                          at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:517) 
                          at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:528) 
                          at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:320) 
                          at dleedesign.dubcommunicationstestapp.Bluetooth$ConnectThread.run(Bluetooth.java:408) 
dleedesign.dubcommunicationstestapp D/BluetoothService: start 
dleedesign.dubcommunicationstestapp D/BluetoothService: setState() 2 -> 1 
dleedesign.dubcommunicationstestapp D/Main: MESSAGE_TOAST { when=-15ms what=5 target=dleedesign.dubcommunicationstestapp.SecondFragment$1 } 
dleedesign.dubcommunicationstestapp D/Main: MESSAGE_STATE_CHANGE: 1 
+0

(使用しようとしている他のTextViewと同じ、ボタンなど)あなたは

myview = inflater.inflate.... 

との最初の行で行ったように、レイアウトを膨らませると、その後、

msgReceived = (TextView) myView.findViewById(R.id.msgReceived); 

必要がありますあなたが見るメッセージ?また、logcatスニペットが便利です。あなたは 'AsyncTask'のようなものを使って、別のスレッドでブルートゥースと遊んで、UIスレッドに情報を表示するべきです。 –

+0

これはもっと効率的な方法でしょうか? –

答えて

1

を試してみてください。ここフラグメントのための私のJavaファイルがあります何が起こっているのかは、findViewByIdを実行するたびにレイアウトを指定してビューを膨張させてからTex tView。これは、別のビューでfindViewByIdを呼び出しています。あなたはまず何

+1

ボタンがうまく機能しないので、意味があります。ご説明ありがとうございます! –

+0

あなたは大歓迎です! – wanpanman

0

msgReceived = (TextView) myView.findViewById(R.id.msgReceived); 
+0

これはうまくいきました、ありがとう! –

+0

あなたは大歓迎です!それは答えとしてそれを検証し、人々がそれが良いものであることを認識できるようにupvoteに時間をかけてください、歓声! –

+0

また、このメソッドをすべてのUI要素で使用することをお勧めします。 –

関連する問題