AndroidスタジオでJavaを開発することで新しくなったので、私はチュートリアルをフォローして学習しています。私は今、ロボットを制御するアプリケーションを作成しています。私はBluetooth機能を実装したいと思います。だから私はハリーのチュートリアル:Harry's tutorialを使ってブルートゥースについてのチュートリアルを始めました。ArrayAdapter.add()がAndroidアプリをクラッシュする
いくつかのAndroidスタジオの知識を学んだ後、私は複数のアクティビティの代わりに異なるフラグメントを作成しました。今問題は、ArrayAdapter.add();
を呼び出すときstukを得ることです。この関数は文字列ライトを受け取りますか?だから私のアプリはいつもクラッシュする、mPairedDevicesArrayAdapter.add(deviceString)
を呼ぶ。
コードをデバッグするとき、プログラムは素早くデバイス名とMACアドレスを取得します。それはデバッグモニタにうまくログするので、deviceString
は問題ではないですか?他の人は、Stringをリストに入れる必要があると言っていますが、Androidのドキュメントによれば、正しいコンストラクタが呼び出されたときにStringだけを受け取ることができます。
誰かがこの問題を手伝うことができますか?ここに私のbluetoothFragmentクラスと.xmlファイルがあります。
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Set;
/**
* Created by rbesten on 5/2/2017.
*/
public class BluetoothFragment extends Fragment {
// textview for connection status
TextView textConnectionStatus;
ListView pairedListView;
//An EXTRA to take the device MAC to the next activity
public static String EXTRA_DEVICE_ADDRESS;
// Member fields
private BluetoothAdapter mBtAdapter;
private ArrayAdapter<String> mPairedDevicesArrayAdapter;
String deviceString = "Test if it works";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_bluetooth, container, false);
return rootView;
}
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
textConnectionStatus = (TextView) getActivity().findViewById(R.id.connecting);
textConnectionStatus.setTextSize(40);
//Initialize array adapter for paired devices
mPairedDevicesArrayAdapter = new ArrayAdapter<String>(getContext(), R.layout.device_name);
// Find and set up the ListView for paired devices
pairedListView = (ListView) getActivity().findViewById(R.id.paired_devices);
pairedListView.setAdapter(mPairedDevicesArrayAdapter);
//pairedListView.setOnItemClickListener(mDeviceClickListener);
}
@Override
public void onResume() {
super.onResume();
//check BT status at onResume in case something has changed while app was paused etc
checkBTState();
mPairedDevicesArrayAdapter.clear();// clears the array so items aren't duplicated when resuming from onPause
textConnectionStatus.setText(" "); //makes the textview blank
//Get the local Bluetooth adapter
mBtAdapter = BluetoothAdapter.getDefaultAdapter();
// Get a set of currently paired devices and append to pairedDevices list
Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();
// Add previously paired devices to the array
if (pairedDevices.size() > 0) {
getActivity().findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);//make title viewable
for(BluetoothDevice device : pairedDevices){
deviceString = (device.getName() + "\n" + device.getAddress());
mPairedDevicesArrayAdapter.add(deviceString);
mPairedDevicesArrayAdapter.notifyDataSetChanged();
}
} else {
mPairedDevicesArrayAdapter.add("no devices paired");
mPairedDevicesArrayAdapter.notifyDataSetChanged();
}
}
//method to check if the device has Bluetooth and if it is on.
//Prompts the user to turn it on if it is off
private void checkBTState() {
// Check device has Bluetooth and that it is turned on
mBtAdapter = BluetoothAdapter.getDefaultAdapter(); // CHECK THIS OUT THAT IT WORKS!!!
if (mBtAdapter == null) {
Toast.makeText(getActivity().getBaseContext(), "Device does not support Bluetooth", Toast.LENGTH_SHORT).show();
getActivity().finish();
} else {
if (!mBtAdapter.isEnabled()) {
//Prompt user to turn on Bluetooth
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
}
}
.XMLファイル!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/title_paired_devices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Select btSerial device from paired devices:"
android:visibility="gone"
android:background="#666"
android:textColor="#fff"
android:paddingLeft="5dp" />
<ListView
android:id="@+id/paired_devices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stackFromBottom="false"
android:layout_weight="1" />
<TextView
android:id="@+id/connecting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/infoText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="If no devices are listed please pair your device in Android settings"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
</LinearLayout>
</LinearLayout>
デバッガ情報は次のとおりです。
this = {[email protected]} "BluetoothFragment{4e73664 #3 id=0x7f0b005e android:switcher:2131427422:3}"
pairedDevices = {[email protected]} size = 2
device = {[email protected]} "50:32:75:DA:15:4A"
mPairedDevicesArrayAdapter = {[email protected]}
mBtAdapter = {[email protected]}
deviceString = "Test if it works"
そしてcrashlog:DEVICE_NAMEレイアウト内
05-03 10:10:32.364 32255-32255/com.example.rbesten.solscanrobot D/AndroidRuntime: Shutting down VM
05-03 10:10:32.367 32255-32255/com.example.rbesten.solscanrobot E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.rbesten.solscanrobot, PID: 32255
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:401)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:371)
at android.widget.AbsListView.obtainView(AbsListView.java:2494)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1349)
at android.widget.ListView.onMeasure(ListView.java:1250)
at android.view.View.measure(View.java:20084)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6282)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1705)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:797)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:657)
at android.view.View.measure(View.java:20084)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1658)
at android.view.View.measure(View.java:20084)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6282)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:214)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:20084)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6282)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
at android.view.View.measure(View.java:20084)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6282)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:214)
at android.view.View.measure(View.java:20084)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6282)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1705)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:797)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:657)
at android.view.View.measure(View.java:20084)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6282)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:214)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:766)
at android.view.View.measure(View.java:20084)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2640)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1577)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1846)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1462)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6965)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:907)
at android.view.Choreographer.doCallbacks(Choreographer.java:709)
at android.view.Choreographer.doFrame(Choreographer.java:644)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:893)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6247)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:388)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:371)
at android.widget.AbsListView.obtainView(AbsListView.java:2494)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1349)
at android.widget.ListView.onMeasure(ListView.java:1250)
at android.view.View.measure(View.java:20084)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6282)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1705)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:797)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:657)
at android.view.View.measure(View.java:20084)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1658)
at android.view.View.measure(View.java:20084)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6282)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:214)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:20084)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6282)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
at android.view.View.measure(View.java:20084)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6282)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:214)
at android.view.View.measure(View.java:20084)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6282)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1705)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:797)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:657)
at android.view.View.measure(View.java:20084)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6282)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:214)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:766)
at android.view.View.measure(View.java:20084)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2640)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1577)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1846)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1462)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6965)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:907)
at android.view.Choreographer.doCallbacks(Choreographer.java:709)
at android.view.Choreographer.doFrame(Choreographer.java:644)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:893)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6247)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:padding="5dp">
</TextView>
</LinearLayout>
あなたは必ずはい –
をログを投稿することができます役立ちますR.layout.device_name
希望の代わりにテスト目的のために、アレイアダプタのコンストラクタでIDの下 android.R.layout.simple_list_item_1を試してみてください!デバッガログは次のとおりです。this = {BluetoothFragment @ 4820} "BluetoothFragment {4e73664#3 id = 0x7f0b005e android:switcher:2131427422:3}" pairedDevices = {Collections $ UnmodifiableSet @ 4822} size = 2 device = {BluetoothDevice @ 4823 } mPairedDevicesArrayAdapter = {ArrayAdapter @ 4824} mBtAdapter = {BluetoothAdapter @ 4825} deviceString = "動作するかどうかのテスト" – RoboRichio
彼はクラッシュのログを意味していましたが、これをあなたの投稿にコメントとして編集しないでください。正しくフォーマットできます。 – Denny