2010-12-04 14 views
8

मैं एंड्रॉइड ऐप्स विकसित करने के लिए बहुत नया हूं, और मुझे उम्मीद है कि मैं इसे सही जगह पर पोस्ट कर रहा हूं। मैं अपने ब्लूटूथ मॉड्यूल से कनेक्ट करने के लिए फोन ब्लूटूथ कैसे प्राप्त करना सीखने की कोशिश कर रहा हूं। मैं एक संदर्भ पुस्तक में पाया गया कोड का उपयोग करने की कोशिश कर रहा हूं, लेकिन यह मुझे एक त्रुटि दे रहा है। मैंने पहले जावा में लिखा है लेकिन मुझे एंड्रॉइड ऐप की संरचना को समझने में कठिनाई हो रही है। वैसे भी मुझे जो त्रुटि मिल रही है वह अज्ञात त्रुटि है: java.lang.nullPointerException। क्या मैं शायद एक पुस्तकालय आयात करना भूल रहा हूं जिसकी मुझे आवश्यकता है, या जब मैंने प्रोजेक्ट बनाया था तो क्या मैंने पैकेजिंग गलती की थी?शुरुआती डेवलपर: अज्ञात त्रुटि: java.lang.nullPointerException

package android.app.bluetooth; 
//import java.io.InputStream; 
//import java.io.OutputStream; 
import java.util.ArrayList; 
import java.util.UUID; 
import java.io.IOException; 
import android.app.Activity; 
import android.os.AsyncTask; 
import android.os.Bundle; 
//import android.os.Handler; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.bluetooth.BluetoothServerSocket; 
import android.bluetooth.BluetoothSocket; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
//import android.view.View.OnKeyListener; 
import android.widget.ArrayAdapter; 
import android.widget.AdapterView; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListView;  
import android.widget.TextView; 
import android.widget.AdapterView.OnItemClickListener; 

public class bluetooth extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    //Get the bluetooth adapter (phone) 
    configureBluetooth(); 

    //Setup ListView of discovered devices 
    setupListView(); 

    //Setup search button 
    setupSearchButton(); 

    //Setup listen button 
    setupListenButton(); 
} 

private BluetoothAdapter bluetooth; 
private BluetoothSocket socket; 
private UUID uuid = UUID.fromString("985c75a3-66ae-4b5b-9fac-894659d6f6ee"); 
private void configureBluetooth(){ 
    BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter(); 
    } 

//Create switchUI method that will be called once a connection is 
//established to enable views for reading and writing messages 
private ListView list; 
private void switchUI(){ 
    final TextView messageText = (TextView)findViewById(R.id.text_messages); 
    final EditText textEntry = (EditText)findViewById(R.id.text_message); 

    messageText.setVisibility(View.VISIBLE); 
    list.setVisibility(View.GONE); 
    textEntry.setEnabled(true); 
    } 

//Create server listener. Listen button will prompt user to enable discovery 
//When discovery window returns, open bluetooth socket to listen for connection  requests for discovery duration 
//Once a connection has been made, make a call to switchUI 

private static int DISCOVERY_REQUEST = 1; 
private void setupListenButton(){ 
Button listenButton = (Button)findViewById(R.id.button_listen); 
listenButton.setOnClickListener(new OnClickListener(){ 
    public void onClick(View view){ 
    Intent disc; 
    disc = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); 
    startActivityForResult(disc, DISCOVERY_REQUEST); 
     } 
    }); 
    } 

//Find out if user has accepted or rejected the request 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data){ 
    if(requestCode == DISCOVERY_REQUEST){ 
    boolean isDiscoverable = resultCode > 0; 
    if (isDiscoverable){ 
     String name = "bluetoothserver"; 
     try{ 
     final BluetoothServerSocket btserver = bluetooth.listenUsingRfcommWithServiceRecord(name, uuid); 
     AsyncTask<Integer, Void, BluetoothSocket> acceptThread = new AsyncTask<Integer, Void, BluetoothSocket>(){ 
     @Override 
     protected BluetoothSocket doInBackground(Integer ... params){ 
     try{ 
      socket = btserver.accept(params[0]*1000); 
      return socket; 
      } catch (IOException e){ 
      Log.d("BLUETOOTH", e.getMessage()); 
      } 
      return null; 
     } 
     @Override 
     protected void onPostExecute(BluetoothSocket result){ 
      if (result != null) 
      switchUI(); 
     } 
     }; 
     acceptThread.execute(resultCode); 
     } catch (IOException e){ 
     Log.d("BLUETOOTH", e.getMessage()); 
     } 
     } 
     //int discoverableDuration = resultCode; 
    } 
    } 

//Provide a means for client device to search for listening server 
private ArrayAdapter<BluetoothDevice> aa; 
private ArrayList<BluetoothDevice> foundDevices; 
private void setupListView(){ 
    aa = new ArrayAdapter<BluetoothDevice>(this, android.R.layout.simple_list_item_1, foundDevices); 
    list = (ListView)findViewById(R.id.list_discovered); 
    list.setAdapter(aa); 

    //Include onItemClickListener that will attempt to asynchronously initiate a client-side connection 
    //with the selected remote Bluetooth Device 
    //If successful, keep a reference to the socket it creates and make a call to switchUI 
    list.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> arg0, View view, int index, long arg3){ 
     AsyncTask<Integer, Void, Void> connectTask = new AsyncTask<Integer, Void, Void>(){ 
     @Override 
     protected Void doInBackground(Integer ... params){ 
     try{ 
     BluetoothDevice device = foundDevices.get(params[0]); 
     socket = device.createRfcommSocketToServiceRecord(uuid); 
     socket.connect(); 
     } catch(IOException e){ 
     Log.d("BLUETOOTH_CLIENT", e.getMessage()); 
     } 
     return null; 
     } 

     @Override 
     protected void onPostExecute(Void result){ 
     switchUI(); 
     } 
     }; 
     connectTask.execute(index); 
     } 
    }); 
    } 

//Create a broadcast receiver that listens for Bluetooth Device discovery broadcasts, 
//adds each discovered device to the array of found devices and notifies the Array Adapter 
//Discover remote bluetooth devices 
BroadcastReceiver discoveryResult = new BroadcastReceiver(){ 
@Override 
public void onReceive(Context context, Intent intent){ 
//String remoteDeviceName = intent.getStringName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME); 
BluetoothDevice remoteDevice; //remote bluetooth device 
remoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
if(bluetooth.getBondedDevices().contains(remoteDevice)){ 
    foundDevices.add(remoteDevice); 
    aa.notifyDataSetChanged(); 
    } 
} 
}; 

    //Register Broadcast Receiver and initiate discovery session 
    private void setupSearchButton(){ 
    Button searchButton = (Button)findViewById(R.id.button_search); 

    searchButton.setOnClickListener(new OnClickListener(){ 
     public void onClick(View view){ 
     registerReceiver(discoveryResult, new IntentFilter(BluetoothDevice.ACTION_FOUND)); 

     if (!bluetooth.isDiscovering()){ 
     foundDevices.clear(); 
     bluetooth.startDiscovery(); 
     } 
     } 
    }); 
    } 
} 

यह लेआउट फ़ाइल है::

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<EditText 
android:id="@+id/text_message" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_alignParentBottom="true" 
android:enabled="false" 
/> 
<Button 
android:id="@+id/button_search" 
android:text="Search for listener" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_above="@id/text_message" 
/> 
<Button 
android:id="@+id/button_listen" 
android:text="Listen for connection" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_above="@id/button_search" 
/> 
<ListView 
android:id="@+id/list_discovered" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:layout_above="@id/button_listen" 
android:layout_alignParentTop="true" 
/> 
<TextView 
android:id="@+id/text_messages" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:layout_above="@id/button_listen" 
android:layout_alignParentTop="true" 
android:visibility="gone" 
/> 
</RelativeLayout> 

और यहाँ मैनिफ़ेस्ट फ़ाइल है: यहाँ कोड है

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    android:versionCode="1" 
    android:versionName="1.0" package="android.app.bluetooth"> 
<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".bluetooth" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

</application> 
<uses-permission android:name="android.permission.BLUETOOTH"/> 
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> 
</manifest> 

जैसा मैंने कहा कि मैं तो यह करने के लिए बहुत नया हूँ मैं क्षमा चाहता हूं अगर यह ज्यादा समझ में नहीं आता है लेकिन किसी भी मदद की सराहना की जाएगी।

+4

आप सही जगह पर पोस्ट कर रहे हैं। सबसे महत्वपूर्ण बात यह है कि हमें स्टैक ट्रेस देखने की ज़रूरत है जिसके परिणामस्वरूप nullPointerException में परिणाम होता है। कृपया स्टैक ट्रेस के साथ प्रश्न अपडेट करें। (यदि आप एक आयात या संदर्भ खो रहे थे तो प्रोग्राम संकलित नहीं होगा।) – cfeduke

+0

मुझे माफ़ कर दो लेकिन मैं यह कैसे कर सकता हूं? मैंने Thread.dumpStack() जोड़ने की कोशिश की; लेकिन जब मैं प्रोग्राम चलाने की कोशिश करता हूं तो यह कहता है कि 'आपकी प्रोजेक्ट में त्रुटि है) कृपया अपना एप्लिकेशन चलाने से पहले उन्हें ठीक करें। – Patty

+1

लॉगक –

उत्तर

2

इसे चरण-दर-चरण डीबग करने का प्रयास करें। इस तरह आप NullPointerException का कारण पाएंगे। कुछ एक्सेस किए गए क्षेत्र शायद शून्य होंगे। एक बार जब आप जो क्षेत्र पता है, तुम ने कहा कि क्षेत्र को डिफ़ॉल्ट मान देकर NullPointerException रोका जा सकता है ..

0

कार्यक्षेत्र से परियोजना हटाने और पुन: आयात करने का सबसे अच्छा तरीका यह तय करने के लिए (लॉग बिल्ली खाली है) है ..

0

बस मेरे पर्यावरण में इस समस्या को ठीक किया गया। मेरे पास मेरे प्रोजेक्ट से जुड़ा संसाधन (स्रोत कोड) था। जब मैंने प्रोजेक्ट को एक अलग वर्कस्पेस में दोबारा खोल दिया तो यह फाइलें खोलने में असफल रहा।

जल्दी शून्य सूचक अपवाद से छुटकारा पाने के लिए आप इसे आजमा सकते हैं।
बाहर निकलें Eclipse
। प्रोजेक्ट फ़ाइल खोलें और अनुभाग को हटाएं।
ग्रहण को पुनरारंभ करें, साफ करें और
बनाएं अब आप वास्तविक त्रुटि देख सकते हैं। अब आप इसे परियोजना गुणों में ठीक कर सकते हैं।

शुभकामनाएं!

क्रेडिट: http://blog.thehappydeveloper.com/?p=112

0

मैं इसका समाधान करने में सक्षम था:

  1. उद्घाटन मेरे .project फ़ाइल और खुलने .classpath फ़ाइल में गलत जुड़ा हुआ पुस्तकालय
  2. के लिए कोई प्रविष्टि को हटाने और चरण 1
0

में हटाए गए वही गलत तरीके से लिंक की गई लाइब्रेरी के संदर्भ को हटाकर अपनी परियोजना.प्रॉप जांचें आपके प्रोजेक्ट फ़ोल्डर के अंदर erties में अमान्य प्रविष्टियां हो सकती हैं

0

मुझे एक ही समस्या थी। यह एक बंद पुस्तकालय परियोजना से था जिसे मेरी परियोजना से संदर्भित किया गया था। तो

  • सही परियोजना के नाम पर क्लिक गुणों पर
  • क्लिक
  • गलत संदर्भ के लिए एंड्रॉयड
  • और में पुस्तकालय की ओर से चेक चुन लें।
  • और यदि कोई बंद प्रोजेक्ट है (जैसे एंड्रॉइड एपकोपेट) इसे खोलने का प्रयास करें।
1

मेरे लिए यह

मूल रूप से एक परियोजना शामिल किया गया था दो बार एक निर्भरता समस्या थी,। इस तरह का त्रुटि संदेश भी एंड्रॉइड एडीटी ग्रहण संस्करण के साथ हुआ। > लाइब्रेरी बी की जरूरत -

मूल रूप से, वहाँ
प्रोजेक्ट ए था।
लेकिन
प्रोजेक्ट ए -> निर्भरता सी की आवश्यकता थी।

लेकिन साथ ही,
निर्भरता सी-> भी लाइब्रेरी बी
को निर्भरता था।

तो पुस्तकालय बी दो बार था।
आशा है कि समाधान स्पष्ट है, कि मेरे लिए समस्या :)

आशा है कि यह मदद करता है :) चीयर्स, माइक

0

सही परियोजना पर क्लिक करें था -> गुण -> एंड्रॉयड -> संदर्भ जाँच दाएं विंडो में देखें कि संदर्भ में सभी पुस्तकालय कार्यक्षेत्र में मौजूद हैं और कोई भी बंद नहीं है

3

मेरे मामले में इसे इस तथ्य से करना पड़ा कि मेरे पास एक अप्रयुक्त प्रोजेक्ट का संदर्भ था, appcompat_v7 (यह एक संगतता पुस्तकालय है पुराने एंड्रॉइड पर काम करने के लिए एक्शन बार)।

मैं इसे कैसे हल:

सही परियोजना पर क्लिक किया, गुण, एंड्रॉयड टैब, पुस्तकालय के लिए हटा दिया संदर्भ पर।

पैकेज एक्सप्लोरर में सभी परियोजनाओं को हटा दिया गया (उन्हें डिस्क से हटाया नहीं गया)।

तब मैं अपने प्रोजेक्ट वापस फिर से आयातित, इस तरह:

राइट क्लिक करें, आयात, मौजूदा कार्यक्षेत्र में परियोजनाओं, परियोजना फ़ोल्डर का चयन करें।

फिर मैंने इसे पुनर्निर्मित किया, और कुछ त्रुटियां थीं, शैलियों.एक्सएमएल फाइलों में कुछ गायब संसाधन थे।

मैंने शैलियों.एक्सएमएल फाइलों को हटा दिया, क्योंकि मुझे उनकी आवश्यकता नहीं थी।

मैंने इस प्रविष्टि को androidmanifest.xml से हटा दिया: एंड्रॉइड: थीम = "@ शैली/ऐपथीम"।

संबंधित मुद्दे