我在Android应用程序中遇到错误。当我在pauseB上调用setVisible()时,由于pauseB被初始化为null,应用程序崩溃。这很奇怪,因为在startB上调用setVisible()可以正常工作。以下是相关代码:
MainActivity.java
...imports...
public class MainActivity extends AppCompatActivity {
public static boolean toggle=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void beginGame(View v)
{
Button startB= (Button)v.findViewById(R.id.startButton);
startB.setVisibility(View.GONE);
Button pauseB = (Button)v.findViewById(R.id.pauseButton);
pauseB.setVisibility(View.VISIBLE);
rest of method...
}
rest of code...
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.placemultiplebuttons_android_examples.com.MainActivity" >
<Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Start!"
android:onClick="beginGame" />
<Button
android:id="@+id/pauseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/startButton"
android:layout_alignBottom="@+id/startButton"
android:layout_toRightOf="@+id/startButton"
android:text="Pause"
android:onClick="pauseGame" />
</RelativeLayout>
以下是AndroidStudio提供的例外情况:
异常
FATAL EXCEPTION: main
Process: com.example.jackson.myfirstapp, PID: 2285
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.setVisibility(int)' on a null object reference
at com.example.jackson.myfirstapp.MainActivity.beginGame(MainActivity.java:29)
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)
重述问题,错误发生在
行pauseB.setVisibility(View.VISIBLE);
对于我的生活,我无法理解。由于我是Android新手,所以对此有任何帮助表示赞赏。非常感谢你的时间!
答案 0 :(得分:0)
而不仅仅v.findViewById(...)
尝试findViewById(...)
。
答案 1 :(得分:0)
你遇到崩溃的原因是,
var games = document.getElementById("Checkbox1").checked;
var music = document.getElementById("Checkbox2").checked;
var lastName, firstName, fullName, address, telephone, sex;
lastName = document.nameForm.lastName.value;
firstName = document.nameForm.firstName.value;
address = document.nameForm.address.value;
telephone = document.nameForm.telephone.value;
sex = document.nameForm.sex.value;
fullName = firstName + " " + lastName;
document.write();
document.write("Your name: ");
document.write(fullName);
document.write("<br>");
document.write("<br>");
document.write("Your address: " + address);
document.write("<br>");
document.write("<br>");
document.write("Your telephone: " + telephone);
document.write("<br>");
document.write("<br>");
document.write("Your gender: " + sex);
document.write("<br>");
document.write("<br>");
if (games) document.write(" Games");
if (music) document.write(" Music");
执行startButton
方法。beginGame
方法,beginGame
参数有v
按钮视图。startButton
点Button pauseB = (Button)v.findViewById(R.id.pauseButton);
位于R.id.pauseButton
按钮视图内。startButton
不包含startButton
。 pauseButton
视图位于活动pauseButton
而不是R.layout.activity_main
查看。要解决此问题,请全局声明按钮变量,并在startButton
之后的onCreate
中定义它们。
setContentView