我的代码有问题。当我午餐我的程序时,我崩溃了。
在Android显示器上,我可以看到:

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) 


这是我的课:

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


我还没有完成,对不起,有些话有用。

如果有人可以帮助我,我会很高兴。

谢谢。

最佳答案

我也有同样的错误。我通过在onCreate()方法之外定义一个函数来解决此错误。在该函数内部,您需要调用其他查找栏的findViewByid方法。像这样

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

首先声明pDialog的私有ProgressDialog pDialog;

在oncreate方法中调用initializeVariables()方法,在这里我提到我的搜索栏名称用您的代码替换了这些名称。

`

10-07 12:45