gpt4 book ai didi

java - 尝试将按钮值与进度条值进行比较

转载 作者:行者123 更新时间:2023-12-01 20:26:49 25 4
gpt4 key购买 nike

我的应用程序中有一个按钮,称为“启动引擎”。如果我按下按钮,我希望它检查燃料是否为空,然后将按钮内的文本更改为“引擎正在运行..”,但它不断崩溃,我不明白错误消息

新错误:

    05-04 19:06:52.435 18652-18652/com.example.schwarzerritter.remotecv02 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.schwarzerritter.remotecv02, PID: 18652
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setText(java.lang.CharSequence)' on a null object reference
at com.example.schwarzerritter.remotecv02.MainActivity.onClick(MainActivity.java:30)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
05-04 19:06:53.787 18652-18652/? I/Process: Sending signal. PID: 18652 SIG: 9

JAVA 代码主要 Activity//

> package com.example.schwarzerritter.remotecv02;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {

public Button lockB;
public Button engB;

public void onClick(View v){
ProgressBar fuelProgressBar = (ProgressBar)findViewById(R.id.fuelBar);
//if(fuelProgressBar.getProgress() > 0){
engB.setText("ENGINE RUNNING *BRUMBRUM*");
//}
}

public void lockPage(){
lockB = (Button)findViewById(R.id.lockB);
lockB.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent lockPage = new Intent(MainActivity.this, lockDoor.class);
startActivity(lockPage);
}
});
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lockPage();
}

}

ACTIVITYXML//

android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.schwarzerritter.remotecv02.MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RemoteC"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent" />

<Button
android:id="@+id/lockB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="64dp"
android:layout_marginRight="8dp"
android:layout_marginTop="39dp"
android:fontFamily="monospace"
android:text="LOCK"
app:layout_constraintBottom_toTopOf="@+id/engB"
app:layout_constraintHorizontal_bias="0.26"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/locationB"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.0" />

<Button
android:id="@+id/locationB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="86dp"
android:layout_marginTop="39dp"
android:fontFamily="monospace"
android:text="LOCATION"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<Button
android:id="@+id/engB"
android:layout_width="221dp"
android:layout_height="41dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="51dp"
android:fontFamily="monospace"
android:text="START ENGINE"
android:onClick="onClick"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/locationB" />

<ProgressBar
android:id="@+id/fuelBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="220dp"
android:layout_height="18dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:max="100"
android:progress="75"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/fuelT" />

<TextView
android:id="@+id/fuelT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:fontFamily="monospace"
android:text="FUEL BAR"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="52dp"
app:layout_constraintTop_toBottomOf="@+id/engB" />

<TextView
android:id="@+id/acT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="82dp"
android:layout_marginTop="27dp"
android:fontFamily="monospace"
android:text="AC:"
android:textSize="20sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/fuelBar" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="104dp"
android:layout_marginTop="27dp"
android:text="20"
android:textSize="20sp"
app:layout_constraintHorizontal_bias="0.416"
app:layout_constraintLeft_toRightOf="@+id/acT"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/fuelBar" />

<Button
android:id="@+id/button6"
android:layout_width="38dp"
android:layout_height="46dp"
android:text="+"
android:layout_marginTop="19dp"
app:layout_constraintTop_toBottomOf="@+id/fuelBar"
app:layout_constraintRight_toLeftOf="@+id/button8"
android:layout_marginRight="9dp"
app:layout_constraintLeft_toRightOf="@+id/textView4"
android:layout_marginLeft="8dp" />

<Button
android:id="@+id/button8"
android:layout_width="41dp"
android:layout_height="46dp"
android:text="-"
android:layout_marginTop="19dp"
app:layout_constraintTop_toBottomOf="@+id/fuelBar"
android:layout_marginRight="63dp"
app:layout_constraintRight_toRightOf="parent" />

最佳答案

将此行:ProgressBarfuelProgressBar = (ProgressBar)findViewById(R.id.fuelBar);移动到您的lockPage方法:

public void lockPage(){    
fuelProgressBar = (ProgressBar)findViewById(R.id.fuelBar);
//you have to find your Button before you use it.
engB = (Button) findViewById(R.id.engB);
lockB = (Button)findViewById(R.id.lockB);
lockB.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent lockPage = new Intent(MainActivity.this, lockDoor.class);
startActivity(lockPage);
}
});
}

当您的Activity被创建时,您无权访问Views,因为它们还不可用。

此外,您的方法 onClick 永远不会被调用,您必须将其添加到 XML 文件中,如下所示:

<Button
android:id="@+id/engB"
android:layout_width="221dp"
android:layout_height="41dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="51dp"
android:fontFamily="monospace"
android:text="START ENGINE"
android:onClick="onClick"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/locationB" />

关于java - 尝试将按钮值与进度条值进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43790318/

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