具有蓝牙名称的自定义TextView

时间:2018-10-10 14:44:48

标签: java android xml textview

我正在自定义TextView上工作,但对此我感到非常沮丧。 Idk如何完成代码。...我的代码是:

public class TestTextView extends TextView{
static BluetoothSocket mSocket;
Context c;

public TestTextView(Context mContext){
    super(mContext);
    mBTConnected(c);
}

public static boolean mBTConnected(Context c){
    String mOutputName;
    Method m;
    try{
        BluetoothDevice mDevice=BluetoothAdapter.getDefaultAdapter().getRemoteDevice("MAC_ADRS");
        m=mDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
        mSocket=(BluetoothSocket)m.invoke(mDevice, Integer.valueOf(1));
        mSocket.connect();
        mOutputName=mDevice.getName().toString();

        Log.i("Bluetooth device "+mOutputName," is connected");

        return true;
    }catch(Exception e){

    }return false;
}}

问题:如何将字符串“名称”设置为自定义TextView的文本以在Xml布局文件中调用?


日志:

10-10 17:34:14.900 E/SDAgentPackageStateReceiver(3797): Not going to handle 'com.mort015.BluetoothTextView'! 10-10 17:34:15.055 E/SDAgentPackageStateReceiver(3797): Not going to handle 'com.mort015.BluetoothTextView'! 10-10 17:34:18.610 E/SPPClientService(9467): [PackageInfoChangeReceiver] [handlePkgRemovedEvent] PackageName : com.mort015.BluetoothTextView, true, false 10-10 17:34:24.237 E/AndroidRuntime(30755): Process: com.mort015.BluetoothTextView, PID: 30755 10-10 17:34:24.237 E/AndroidRuntime(30755): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mort015.BluetoothTextView/com.mort015.BluetoothTextView.MainActivity}: android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class com.mort015.BluetoothTextView.TestTextView 10-10 17:34:24.237 E/AndroidRuntime(30755): Caused by: android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class com.mort015.BluetoothTextView.TestTextView 10-10 17:34:24.237 E/AndroidRuntime(30755): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class com.mort015.BluetoothTextView.TestTextView 10-10 17:34:24.237 E/AndroidRuntime(30755):     at com.mort015.BluetoothTextView.MainActivity.onCreate(MainActivity.java:17) 10-10 17:34:24.562 E/cm_cmc_c(5288): app launch:com.mort015.BluetoothTextView

2 个答案:

答案 0 :(得分:0)

将您的mBtConnected方法设为非静态以获取对this对象引用的访问权限,然后在setText(mOutputName)方法中使用mBtConnected即可完成工作

答案 1 :(得分:0)

我想我解决了:

public boolean mBTConnected(Context c){
    String mOutputName;
    Method m;

    try{
        BluetoothDevice mDevice=BluetoothAdapter.getDefaultAdapter().getRemoteDevice("MAC_ADRS");
        m=mDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
        mSocket=(BluetoothSocket)m.invoke(mDevice, Integer.valueOf(1));
        mSocket.connect();
        mOutputName=mDevice.getName();
        super.setText(mOutputName);
        Log.i("Bluetooth device "+mOutputName," is connected");


        return true;
    }
    catch(Exception e){

    }return false;
}

我的布局文件外观:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<com.myapp.myapp.TestTextView
    android:id="@+id/BtTxt"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"/></LinearLayout>

我的Mainactivity Java外观如下:

public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}}