[英]My app is crashing when I add Bluetooth button [duplicate]
我是 Android Studio 的新手我想通过蓝牙 HC-06 将数据从 Arduino 发送到 android 我从 AndroidStudio 的模板(导航抽屉活动)开始我的代码。 起初一切都很好,但是当我想将带有 SetOnClickListener 帮助的蓝牙按钮添加到我的代码时,噩梦就开始了。 1.它的外观从这里改变在这里输入图像描述
为此,请在此处输入图片说明。 为清楚起见:导航栏和其他工具消失了,而只出现了主页片段。 2.当按下这个片段上的蓝牙按钮时,我的应用程序关闭了。 我尝试关注 logcat ,但无法解决问题。
请帮我提供建议 最好的问候 ps 下面是我的 MainActivity.java 文件
package com.example.myecg2; import android.content.Intent; import android.os.Bundle; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.snackbar.Snackbar; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.view.View; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProviders; import androidx.navigation.NavController; import androidx.navigation.Navigation; import androidx.navigation.ui.AppBarConfiguration; import androidx.navigation.ui.NavigationUI; import com.google.android.material.navigation.NavigationView; import androidx.drawerlayout.widget.DrawerLayout; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import android.view.Menu; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import java.util.Set; public class MainActivity extends AppCompatActivity { private AppBarConfiguration mAppBarConfiguration; private TextView Tekst; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Savollaringiz bo\\'lsa Xat yozing", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); DrawerLayout drawer = findViewById(R.id.drawer_layout); NavigationView navigationView = findViewById(R.id.nav_view); mAppBarConfiguration = new AppBarConfiguration.Builder( R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow, R.id.nav_tools, R.id.nav_share, R.id.nav_send) .setDrawerLayout(drawer) .build(); NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment); NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration); NavigationUI.setupWithNavController(navigationView, navController); final int REQUEST_ENABLE_BT = 1; setContentView(R.layout.fragment_home); final BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter(); Button button =findViewById(R.id.button6); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (btAdapter == null) { Tekst.setText("Bluetooth NOT support"); return; } else { if (btAdapter.isEnabled()) { Tekst.setText("Bluetooth is currently in device discovery process."); Tekst.append("\\nPaired Devices are:"); Set<BluetoothDevice> devices = btAdapter.getBondedDevices(); for (BluetoothDevice device : devices) { Tekst.append("\\nDevice" + device.getName() + "," + device); } } else { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } } } } );} @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onSupportNavigateUp() { NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment); return NavigationUI.navigateUp(navController, mAppBarConfiguration) || super.onSupportNavigateUp(); } }
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/text_home" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:textAlignment="center" android:textSize="20sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView8" android:layout_width="394dp" android:layout_height="218dp" android:text="@string/matn" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toBottomOf="@+id/button6" /> <Button android:id="@+id/button6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="116dp" android:text="@string/LeadI" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toBottomOf="@+id/text_home"/> </androidx.constraintlayout.widget.ConstraintLayout>
和 LogCat 2019-11-30 02:09:01.342 13088-13088/com.example.myecg2 D/AndroidRuntime: Shutting down VM 2019-11-30 02:09:01.343 13088/com.example.myecg2 中给出的错误E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.myecg2, PID: 13088 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null对象引用在 com.example.myecg2.MainActivity$2.onClick(MainActivity.java:75) 在 android.view.View.performClick(View.java:6256) 在 android.view.View$PerformClick.run(View.java: 24701) 在 android.os.Handler.handleCallback(Handler.java:789) 在 android.os.Handler.dispatchMessage(Handler.java:98) 在 android.os.Looper.loop(Looper.java:164) 在 android。 app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com。 android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Tekst
为空,即您永远不会实例化该变量。
你的日志就在这里说:
java.lang.NullPointerException:
Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.myecg2.MainActivity$2.onClick(MainActivity.java:75)
at ...
它甚至说发生错误的类MainActivity
,发生在onClick
的方法名称和错误行:75
。
在您尝试访问它之前(即在第 75 行访问),您Tekst
在onCreate
执行findViewById
来实例化您的Tekst
变量。
旁注:
你调用setContentView
两次。 你不应该那样做。 删除第二个。
Java 和 Kotlin 中的变量名应该以小写字母开头,以帮助您将它们与其他事物区分开来。 将Tekst
重命名为tekst
。 这也是笔误吗? 也许它应该是test
并且帮助您调试的更好名称是testTextView
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.