gpt4 book ai didi

java - Android SeekBar NullPointerException

转载 作者:太空宇宙 更新时间:2023-11-04 12:45:04 27 4
gpt4 key购买 nike

我的代码有问题。当我吃午饭时,我的程序发生了崩溃。在 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) 

这是我的类(class):

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)无效initializeVariables() {
//显示进度对话框
pDialog = new ProgressDialog(Search.this);
pDialog.setMessage("请稍等...");
pDialog.setCancelable(false);
eekBar = (SeekBar) findViewById(R.id.seekBarprice);
eekBarKilometer = (SeekBar) findViewById(R.id.seekBarDis);
}

首先声明 pDialog private ProgressDialog pDialog;

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

`

关于java - Android SeekBar NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36410255/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com