FATAL EXCEPTION: main
Process: com.my.test, PID: 23639
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setEnabled(boolean)' on a null object reference
at com.my.test.MainActivity$MyOnClickListener.onClick(MainActivity.java:46)
public class MainActivity extends AppCompatActivity {
private TextView test0;
private Button result1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
test0 = findViewById(R.id.test0);
Button enable1 = findViewById(R.id.enable1);
enable1.setOnClickListener(new MyOnClickListener());
Button disable1 = findViewById(R.id.disable1);
disable1.setOnClickListener(new MyOnClickListener());
Button result1 = findViewById(R.id.result1);
result1.setOnClickListener(new MyOnClickListener());
}
class MyOnClickListener implements View.OnClickListener {
public void onClick(View v) {
SimpleDateFormat time = new SimpleDateFormat("h:mm:ss");
if (v.getId() == R.id.enable1) {
result1.setTextColor(Color.BLACK);
result1.setEnabled(true);
} else if (v.getId() == R.id.disable1) {
result1.setTextColor(Color.GRAY);
result1.setEnabled(false);
} else if (v.getId() == R.id.result1) {
String desc = String.format("%s 你点了按钮: %s",
time.format(new Date()), ((Button) v).getText());
test0.setText(desc);
}
}
}
}
xml文件如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00aaff"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/enable1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="启用按钮" />
<Button
android:id="@+id/disable1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="禁用按钮" />
LinearLayout>
<Button
android:id="@+id/result1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:text="结果按钮" />
<TextView
android:id="@+id/test0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这里查看点击结果"
android:textColor="@color/black"/>
LinearLayout>
你现在不是重复定义了吗?监听里面使用的是外层定义的result1,而这个是没有初始化的:
把oncreate里面的定义的去掉,enable和disable也是,修改如下:
public class MainActivity extends AppCompatActivity {
private TextView test0;
private Button result1;
private Button enable1;
private Button disable1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
test0 = findViewById(R.id.test0);
enable1 = findViewById(R.id.enable1);
enable1.setOnClickListener(new MyOnClickListener());
disable1 = findViewById(R.id.disable1);
disable1.setOnClickListener(new MyOnClickListener());
result1 = findViewById(R.id.result1);
result1.setOnClickListener(new MyOnClickListener());
}
class MyOnClickListener implements View.OnClickListener {
public void onClick(View v) {
SimpleDateFormat time = new SimpleDateFormat("h:mm:ss");
if (v.getId() == R.id.enable1) {
result1.setTextColor(Color.BLACK);
result1.setEnabled(true);
} else if (v.getId() == R.id.disable1) {
result1.setTextColor(Color.GRAY);
result1.setEnabled(false);
} else if (v.getId() == R.id.result1) {
String desc = String.format("%s 你点了按钮: %s",
time.format(new Date()), ((Button) v).getText());
test0.setText(desc);
}
}
}
}
建议还是多巩固下java基础。你外面的这个result1都没初始化。你在onCreate中又重新定义一个result1干嘛?