0

私はアンドロイドとJavaプログラムが初めてです。

Android HP Bluetooth経由でスイッチをリモート制御するプログラムを作成しようとしています。

Bluetooth機能/アクティビティのスキャンのみがランダムに強制終了されるだけで、すべて問題ありません。私のコードで何が起こった/間違っていたのかわからないので、それに応じて誰かが私を助け/アドバイスしてくれることを願っています 以下は、抽出したlogcatファイルです。

08-08 17:23:40.035: D/getView(1876): WindowsCE
08-08 17:23:40.035: D/getView(1876): WindowsCE
08-08 17:23:42.949: E/BluetoothEventLoop.cpp(107): event_filter: Received signal org.bluez.Adapter:DeviceFound from /org/bluez/1610/hci0
08-08 17:23:42.953: D/BluetoothService(107): updateDeviceServiceChannelCache(00:15:A0:6B:BB:1B)
08-08 17:23:42.980: V/BluetoothEventRedirector(450): Received android.bluetooth.device.action.FOUND
08-08 17:23:42.988: D/getView(1876): WindowsCE
08-08 17:23:42.988: D/AndroidRuntime(1876): Shutting down VM
08-08 17:23:42.988: W/dalvikvm(1876): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-08 17:23:42.992: E/AndroidRuntime(1876): FATAL EXCEPTION: main
08-08 17:23:42.992: E/AndroidRuntime(1876): java.lang.NullPointerException: println needs a message
08-08 17:23:42.992: E/AndroidRuntime(1876):     at android.util.Log.println_native(Native Method)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at android.util.Log.d(Log.java:137)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at com.android.ui.single.ScanDeviceList.getView(ScanDeviceList.java:61)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at android.widget.AbsListView.obtainView(AbsListView.java:1409)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at android.widget.ListView.onMeasure(ListView.java:1127)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at android.view.View.measure(View.java:8313)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at android.view.View.measure(View.java:8313)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at android.view.View.measure(View.java:8313)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at android.view.View.measure(View.java:8313)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at android.view.View.measure(View.java:8313)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at android.os.Looper.loop(Looper.java:123)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at android.app.ActivityThread.main(ActivityThread.java:3683)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at java.lang.reflect.Method.invokeNative(Native Method)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at java.lang.reflect.Method.invoke(Method.java:507)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at dalvik.system.NativeStart.main(Native Method)
08-08 17:23:42.999: W/ActivityManager(107):   Force finishing activity com.android/.DogActivity
08-08 17:23:43.499: W/ActivityManager(107): Activity pause timeout for HistoryRecord{408014c0 com.android/.DogActivity}


08-09 21:32:27.968: D/SurfaceFlinger(107): About to give-up screen, flinger = 0x11df90
08-09 21:41:54.546: D/SntpClient(107): request time failed: java.net.UnknownHostException: north-america.pool.ntp.org
08-09 21:41:54.554: D/SntpClient(107): request time failed: java.net.UnknownHostException: north-america.pool.ntp.org
08-09 21:41:54.554: D/SntpClient(107): request time failed: java.net.UnknownHostException: north-america.pool.ntp.org
08-09 21:41:54.617: D/skia(107): purging 201K from font cache [12 entries]

以下は私のDogActivityコードです

package com.android;

import java.io.FileOutputStream;

import com.android.backend.data.bluetooth.BluetoothManager;
import com.android.ui.UIManager;
import com.android.ui.single.ScanDeviceList;

import android.app.Activity;
import android.app.Dialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class DogActivity extends Activity {

    public BroadcastReceiver mRvc = new BroadcastReceiver(){ // The new is instantiate the class **always read from the back
                                                             // The line from the back is creating an instantiate of this class         
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                // If it's already paired, skip it, because it's been listed
                // already

                // Bluetooth Manager Update

                BluetoothManager.getInstance().addDiscovered_Devices(device);
                // UI Manager Update

                UI_discovered_Devices.add(device);

            }
            if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                uiMgr.hideScanningProgressBox();
            }
        }
    };

    public ScanDeviceList UI_discovered_Devices;
    public UIManager uiMgr = new UIManager();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.overallscanentrylayout);
        uiMgr.owner = this;
        final DogActivity tt = this;

        // set your list view
        UI_discovered_Devices = new ScanDeviceList(this,R.layout.overallscanitemlayout);
        UI_discovered_Devices.set_view(R.id.toptexto1, R.id.bottomtexto1);
        ListView discoverListView = (ListView) findViewById(R.id.clistView1);
        UI_discovered_Devices.clear(); ///////////// Testing to solve memory issue
        discoverListView.setAdapter(UI_discovered_Devices);

        discoverListView
                .setOnItemLongClickListener(new OnItemLongClickListener() {

                    @Override
                    public boolean onItemLongClick(AdapterView<?> parent,
                            View view, int position, long id) {
                        // getItem = retrieving item from UI_discovered_Devices
                        // Arraylist
                        final BluetoothDevice getItem = UI_discovered_Devices
                                .getItem((int) id);

                        final Dialog dialog_createPL = new Dialog(tt);
                        dialog_createPL.setContentView(R.layout.catdogname);

                        Button okButt = (Button) dialog_createPL
                                .findViewById(R.id.catdogok);
                        okButt.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                EditText edit1 = (EditText) dialog_createPL
                                        .findViewById(R.id.catdogtext1);
                                Editable ret = edit1.getText();
                                String ls = ret.toString();

                                // Start activity
                                startActivity(new Intent(tt,
                                        ManualActivity.class));

                                // Write message to text
                                String msg = ls + "," + getItem.getName() + ","
                                        + getItem.getAddress();
                                writeDataToMem(msg);
                            }
                        });

                        Button cancelButt = (Button) dialog_createPL
                                .findViewById(R.id.catdogcancel);
                        cancelButt.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                dialog_createPL.cancel();
                            }
                        });

                        dialog_createPL.setTitle("Enter Name for Bluetooth Device");
                        dialog_createPL.show();

                        return false;
                    }
                });
        discoverListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                final BluetoothDevice getItem = UI_discovered_Devices
                        .getItem((int) id);


                final Dialog dialog_createPL = new Dialog(tt);
                dialog_createPL.setContentView(R.layout.catdogname);

                Button okButt = (Button) dialog_createPL
                        .findViewById(R.id.catdogok);
                okButt.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        EditText edit1 = (EditText) dialog_createPL
                                .findViewById(R.id.catdogtext1);
                        Editable ret = edit1.getText();
                        String ls = ret.toString();

                        // Start activity
                        startActivity(new Intent(tt,
                                ManualActivity.class));

                        // Write message to text
                        String msg = ls + "," + getItem.getName() + ","
                                + getItem.getAddress();
                        writeDataToMem(msg);
                    }
                });

                Button cancelButt = (Button) dialog_createPL
                        .findViewById(R.id.catdogcancel);
                cancelButt.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        dialog_createPL.cancel();
                    }
                });

                dialog_createPL.setTitle("Enter Name for Bluetooth Device");
                dialog_createPL.show();
            }
        });


        // Register for broadcasts when a device is discovered
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);  // IntentFilter class is important to declare for below filter
        registerReceiver(mRvc,filter); //mRvc is the location of logic, filter is message identify by android


        // Register for broadcasts when discovery has finished
        filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); //Must make sure the IntentFilter is declare
        registerReceiver(mRvc,filter); //mRvc is the location of logic, filter message indetiy by android


        // // Logic
        BluetoothManager.getInstance().doDiscovery();

        uiMgr.showScanningProgressBox();
        // uiMgr.clearUIDiscovered_Devices();
    }

    public void writeDataToMem(String msg) {
        // Write playlist to database
        FileOutputStream fos;
        try {
            fos = this.openFileOutput("bone.txt", Context.MODE_PRIVATE);
            fos.write(msg.getBytes());

            Log.d("bone.txt", msg);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            msg = msg +"\n";
            fos = this.openFileOutput("fish.txt", Context.MODE_APPEND);
            fos.write(msg.getBytes());

            Log.d("fish.txt", msg);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void onResume() { //restart bluetooth is accidently off it
        super.onResume();
        // The activity has become visible (it is now "resumed").
        BluetoothManager.getInstance().init();
        BluetoothManager.getInstance().getBluetoothAdapter().enable();

        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); // IntentFilter class is important to declare for below filter
        registerReceiver(mRvc,filter); // mRvc is the location of logic, filter is message identify by android

        filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); //Must make sure the IntentFilter is declare
        registerReceiver(mRvc,filter); 
    }


    protected void onPause() {  // Clear all mac address **is a trial for memory leak
        super.onPause();
        BluetoothManager.getInstance().ccDiscovery();
        BluetoothManager.getInstance().clearDiscoveredDevices();
        uiMgr.hideScanningProgressBox();
        unregisterReceiver(mRvc);
    }

}

以下は私のScanDeviceListコードです

package com.android.ui.single;

//import java.util.ArrayList;

import java.util.Set;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
//import android.widget.CompoundButton;
//import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
//import android.widget.ToggleButton;

//import com.android.R;
//import com.android.ScanListActivity;
//import com.android.backend.data.bluetooth.BluetoothManager;

public class ScanDeviceList extends ArrayAdapter<BluetoothDevice> {
    Context context;
    int layoutResourceID;
    int topViewID;
    int bottomViewID;

    public ScanDeviceList(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        this.context = context;
        layoutResourceID = textViewResourceId;
    }

//  public ScanDeviceList(Context context, int textViewResourceId, ArrayList<BluetoothDevice> items) {
//      super(context, textViewResourceId, items);
//      this.context = context;
//      layoutResourceID = textViewResourceId;
//  }

    public void set_view(int passtopView, int passBottomView){
        topViewID = passtopView;
        bottomViewID = passBottomView;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(layoutResourceID, null);
        }

        TextView topView = (TextView) v.findViewById(topViewID);
        TextView bottomView = (TextView) v.findViewById(bottomViewID);

        final BluetoothDevice o = this.getItem(position);

        // Get the local Bluetooth adapter
        BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();

        // Get a set of currently paired devices
        Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();

        //if (o != null) 
        if (pairedDevices.size() > 0) 
        {

            if (topView != null) {
                topView.setText("Name: " + o.getName());
                Log.d("getView", o.getName());
            }

            if (bottomView != null) {
                String msg = "MAC: " + o.getAddress();
                if(o.getBondState() == BluetoothDevice.BOND_BONDED)
                    {msg = msg + "/" + "Paired";}
                else
                    {msg = msg + "/" + "Not Paired";}

                bottomView.setText(msg);
            }
        }
        return (v);
        }

}

誰かが私の間違いを教えてくれることを願っています。ありがとうございました

4

1 に答える 1

2

ScanDeviceList.javaのこの行

if (topView != null) {
   topView.setText("Name: " + o.getName());
   Log.d("getView", o.getName());
}

o.getName()logcat の次の行にnull基づいています。

08-08 17:23:42.992: E/AndroidRuntime(1876): FATAL EXCEPTION: main
08-08 17:23:42.992: E/AndroidRuntime(1876): java.lang.NullPointerException: println needs a message
08-08 17:23:42.992: E/AndroidRuntime(1876):     at android.util.Log.println_native(Native Method)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at android.util.Log.d(Log.java:137)
08-08 17:23:42.992: E/AndroidRuntime(1876):     at com.android.ui.single.ScanDeviceList.getView(ScanDeviceList.java:61)

上記のように、logcat のスニペットの最後の行は、クラッシュするポイントです。

解決:

if (topView != null) {
   if (o != null && o.getName() != null){
      topView.setText("Name: " + o.getName());
      Log.d("getView", o.getName());
   }else topView.setText("Name: !UNKNOWN!");
}

UI コントロールtopViewに文字列 "!UNKNOWN!"があるかどうかがわかります。そこに -o.getName()が null であるためです。

于 2012-08-09T22:23:14.547 に答える