2016-04-04 29 views
1

Kodumda bir sorunum var. Programımı öğle yemeğimde bir kaza geçiriyorum.Android SeekBar NullPointerException

04-04 20:21:10.369 2042-2042/com.led.led E/AndroidRuntime: FATAL EXCEPTION: main 
                 Process: com.led.led, PID: 2042 
                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.led.led/com.led.led.ledControlSeekBar}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SeekBar.setOnSeekBarChangeListener(android.widget.SeekBar$OnSeekBarChangeListener)' on a null object reference 
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358) 
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420) 
                  at android.app.ActivityThread.access$900(ActivityThread.java:154) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:135) 
                  at android.app.ActivityThread.main(ActivityThread.java:5294) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 
                 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SeekBar.setOnSeekBarChangeListener(android.widget.SeekBar$OnSeekBarChangeListener)' on a null object reference 
                  at com.led.led.ledControlSeekBar.onCreate(ledControlSeekBar.java:76) 
                  at android.app.Activity.performCreate(Activity.java:5990) 
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311) 
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)  
                  at android.app.ActivityThread.access$900(ActivityThread.java:154)  
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)  
                  at android.os.Handler.dispatchMessage(Handler.java:102)  
                  at android.os.Looper.loop(Looper.java:135)  
                  at android.app.ActivityThread.main(ActivityThread.java:5294)  
                  at java.lang.reflect.Method.invoke(Native Method)  
                  at java.lang.reflect.Method.invoke(Method.java:372)  
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)  
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)  

Ve sınıfı vardır:: Android'de i görebilirsiniz izlemek

package com.led.led; 

import android.bluetooth.BluetoothDevice; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.bluetooth.BluetoothSocket; 
import android.content.Intent; 
import android.view.View; 
import android.widget.Button; 
import android.widget.CompoundButton; 
import android.widget.Switch; 
import android.widget.SeekBar; 
import android.app.ProgressDialog; 
import android.bluetooth.BluetoothAdapter; 
import android.os.AsyncTask; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.view.View.OnClickListener; 

import java.io.IOException; 
import java.util.UUID; 
import java.lang.String; 

public class ledControlSeekBar extends AppCompatActivity { 

    Button btnDis; 
    SeekBar seek_r, seek_g, seek_b; 
    String address = null; 
    private ProgressDialog progress; 
    BluetoothAdapter myBluetooth = null; 
    BluetoothSocket btSocket = null; 
    private boolean isBtConnected = false; 
    static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
    ConnectBT bt = new ConnectBT(); 
    String phraseAEnvoyer = "000000000"; 
    String phraseTemp; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     //receive the address of the bluetooth device 
     Intent newint = getIntent(); 
     address = newint.getStringExtra(DeviceList.EXTRA_ADDRESS); 
     bt.execute(); 

     //view of the ledControl layout 
     setContentView(R.layout.activity_led_control); 
     //call the widgtes 
     btnDis = (Button)findViewById(R.id.button_disconnect); 
     seek_r = (SeekBar)findViewById(R.id.seekBar_r); 
     seek_g = (SeekBar)findViewById(R.id.seekBar_g); 
     seek_b = (SeekBar)findViewById(R.id.seekBar_b); 




     seek_r.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 
      int progressChanged = 0; 

      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 
       progressChanged = progress; 
      } 

      public void onStartTrackingTouch(SeekBar seekBar) { 

      } 

      public void onStopTrackingTouch(SeekBar seekBar) { 
       msg("Rouge : " + progressChanged); 
       phraseTemp = phraseAEnvoyer.substring(3, 9); 
       phraseAEnvoyer = progressChanged + phraseTemp; 
       envoyerPhrase(phraseAEnvoyer); 
      } 

     }); 

     seek_g.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 
      int progressChanged = 0; 

      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 
       progressChanged = progress; 
      } 

      public void onStartTrackingTouch(SeekBar seekBar) { 

      } 

      public void onStopTrackingTouch(SeekBar seekBar) { 
       msg("Vert : " + progressChanged); 
       phraseTemp = phraseAEnvoyer.substring(6, 9); 
       phraseAEnvoyer = phraseAEnvoyer.substring(0, 3); 
       phraseAEnvoyer = phraseAEnvoyer + progressChanged + phraseTemp; 
       envoyerPhrase(phraseAEnvoyer); 
      } 

     }); 

     seek_b.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 
      int progressChanged = 0; 

      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 
       progressChanged = progress; 
      } 

      public void onStartTrackingTouch(SeekBar seekBar) { 

      } 

      public void onStopTrackingTouch(SeekBar seekBar) { 
       msg("Bleu : " + progressChanged); 
       phraseTemp = phraseAEnvoyer.substring(0, 6); 
       phraseAEnvoyer = phraseTemp + progressChanged; 
       envoyerPhrase(phraseAEnvoyer); 
      } 

     }); 



     btnDis.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       Disconnect(); //close connection 
      } 
     }); 
    } 

    private void Disconnect() 
    { 
     if (btSocket!=null) //If the btSocket is busy 
     { 
      try 
      { 
       btSocket.close(); //close connection 
      } 
      catch (IOException e) 
      { msg("Error");} 
     } 
     finish(); //return to the first layout 
    } 

    private void envoyerPhrase(String phrase) 
    { 
     if (btSocket!=null) 
     { 
      try 
      { 
       btSocket.getOutputStream().write(phraseAEnvoyer.getBytes()); 
       msg(phraseAEnvoyer); 
      } 
      catch (IOException e) 
      { 
       msg("Error"); 
      } 
     } 
    } 

    private void msg(String s) 
    { 
     Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show(); 
    } 

    private class ConnectBT extends AsyncTask<Void, Void, Void> // UI thread 
    { 
     private boolean ConnectSuccess = true; //if it's here, it's almost connected 

     @Override 
     protected void onPreExecute() 
     { 
      progress = ProgressDialog.show(ledControlSeekBar.this, "Connecting...", "Please wait!"); //show a progress dialog 
     } 

     @Override 
     protected Void doInBackground(Void... devices) //while the progress dialog is shown, the connection is done in background 
     { 
      try 
      { 
       if (btSocket == null || !isBtConnected) 
       { 
        myBluetooth = BluetoothAdapter.getDefaultAdapter();//get the mobile bluetooth device 
        BluetoothDevice dispositivo = myBluetooth.getRemoteDevice(address);//connects to the device's address and checks if it's available 
        btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID);//create a RFCOMM (SPP) connection 
        BluetoothAdapter.getDefaultAdapter().cancelDiscovery(); 
        btSocket.connect();//start connection 
       } 
      } 
      catch (IOException e) 
      { 
       ConnectSuccess = false;//if the try failed, you can check the exception here 
      } 
      return null; 
     } 
     @Override 
     protected void onPostExecute(Void result) //after the doInBackground, it checks if everything went fine 
     { 
      super.onPostExecute(result); 

      if (!ConnectSuccess) 
      { 
       msg("Connection Failed. Is it a SPP Bluetooth? Try again."); 
       finish(); 
      } 
      else 
      { 
       msg("Connected."); 
       isBtConnected = true; 
      } 
      progress.dismiss(); 
     } 
    } 
} 

ben bitirmek değil ve bazı hatlar üzgün yararlıdır.

Birisi bana yardım ederse mutlu olurum.

Teşekkür ederiz.

+1

XML dosyanızın, bildirilen kural dışı durumlardan birinin bulunmadığını bildiren istisna olarak kontrol edin. – zec

+0

XML dosyamda aynı ada sahip üç arama çubuğum var. – jeanbie

+0

Onun hattı 76. Gönderdiğiniz koddan oraya gitmek zor. Lütfen 76 satır –

cevap

0

Aynı hatam da var. OnCreate() yönteminin dışındaki bir işlevi tanımlayarak bu hatayı çözdüm. Bu işlevin içinde farklı seek çubuğunu findViewByid yöntemini çağırmanız gerekir. Bu

private void initializeVariables() { // Showing progress dialog pDialog = new ProgressDialog(Search.this); pDialog.setMessage("Please wait..."); pDialog.setCancelable(false); seekBar = (SeekBar) findViewById(R.id.seekBarprice); seekBarKilometer = (SeekBar) findViewById(R.id.seekBarDis); }

gibi pDialog ilk özel ProgressDialog pDialog beyan;

oncreate yönteminin içindeki initializeVariables() yöntemini çağırın, burada, arama isminin bu isimleri kodunuzla değiştirdiğinden söz ediyorum.

`

İlgili konular