【问题标题】:Custom TextView with Bluetooth Name带有蓝牙名称的自定义 TextView
【发布时间】:2018-10-10 14:44:48
【问题描述】:

我正在使用自定义 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;
}}

问题:如何将字符串“名称”设置为 Text 到自定义 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

【问题讨论】:

    标签: java android xml textview


    【解决方案1】:

    我想我解决了:

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

    【讨论】:

      【解决方案2】:

      使您的mBtConnected 方法为非静态方法以访问this 对象引用,然后setText(mOutputName) 内的mBtConnected 方法将完成这项工作

      【讨论】:

      • 具体在哪里?当我尝试了所有行但它说错误。 “这里没有这样的参考资料”
      • @user10479845 试试super.setText()
      • 我知道你的意思。但是哪里?哪条线?顺便说一句,我打开应用程序时崩溃了。使用我的自定义 textview 编辑膨胀布局时出错:super.setText 也不起作用
      • @user10479845 在记录之前执行此操作,当您获得名称值时。
      • 不起作用...说“这里没有这样的参考资料”
      猜你喜欢
      • 2016-01-11
      • 2012-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-06
      • 1970-01-01
      • 2018-02-11
      相关资源
      最近更新 更多