-2

I'm new with developing Java in Android Studio, so I'm learning by folowing tutorials. I am now creating an App to control a robot and I want to implement Bluetooth functionality. So I started a tutorial about bluetooth using Harry's tutorial: Harry's tutorial.

After learning some Android Studio knowledge I created different Fragments in stead of multiply activity's. The problem now is that i get stuk when calling ArrayAdapter.add();. This function receives a String wright? So why is my app always crashing, Calling the mPairedDevicesArrayAdapter.add(deviceString).

When debugging the code the program nicely get's the device name and MAC address. It log's it nicely in the debug monitor so deviceString is not the problem right? Other people say you need to place the String into a list, but the Android documents says that when the right contructor was called it can receive just a String.

Can somebody help me with this problem. Here's my bluetoothFragment class and the .xml file.

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

And the .XML file!

<?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>

The debuggers information is:

this = {BluetoothFragment@4820} "BluetoothFragment{4e73664 #3 id=0x7f0b005e android:switcher:2131427422:3}"
pairedDevices = {Collections$UnmodifiableSet@4822}  size = 2
device = {BluetoothDevice@4823} "50:32:75:DA:15:4A"
mPairedDevicesArrayAdapter = {ArrayAdapter@4824} 
mBtAdapter = {BluetoothAdapter@4825} 
deviceString = "Test if it works"

And the crashlog:

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) 

Inside the device_name layout:

<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>
RoboRichio
  • 31
  • 5
  • Can you post the log – Dishonered May 03 '17 at 07:59
  • Yes sure! The debuggers log is: this = {BluetoothFragment@4820} "BluetoothFragment{4e73664 #3 id=0x7f0b005e android:switcher:2131427422:3}" pairedDevices = {Collections$UnmodifiableSet@4822} size = 2 device = {BluetoothDevice@4823} "50:32:75:DA:15:4A" mPairedDevicesArrayAdapter = {ArrayAdapter@4824} mBtAdapter = {BluetoothAdapter@4825} deviceString = "Test if it works" – RoboRichio May 03 '17 at 08:05
  • He/she meant the log of the crash, edit this into your post not as comment so it can get formatted properly – Denny May 03 '17 at 08:06
  • Post log exception(crash report) – Mohammad nabil May 03 '17 at 08:07
  • what is inside R.layout.device_name file? – Abdul Waheed May 03 '17 at 08:22

1 Answers1

0

R.layout.device_name must be the id of a xml layout file containing only a TextView(the TextView can't be wrapped by another layout, like a LinearLayout, RelativeLayout etc!), something like this:

For further details you may refer below link "ArrayAdapter requires the resource ID to be a TextView" xml problems

or you may try below id in array adapter constructor for testing purposes android.R.layout.simple_list_item_1 in place of R.layout.device_name

Hope that helps you

Community
  • 1
  • 1
Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58
  • Thanks! The problem is solved! It is necessary to give the adapter the layout file and textView ID. `mPairedDevicesArrayAdapter = new ArrayAdapter(getContext(), R.layout.device_name, R.id.ArrayAdapterTextView);` And for the xml I add an ID field, `android:id="@+id/ArrayAdapterTextView"`. Thanks for referencing to [link](http://stackoverflow.com/questions/9280965/arrayadapter-requires-the-resource-id-to-be-a-textview-xml-problems). This solved the problem. – RoboRichio May 03 '17 at 08:53
  • I am glad it was helpful for you – Abdul Waheed May 03 '17 at 09:57