我只是想在Mainactivity中使用2个按钮。一个打开一个网站(有效),另一个打开我的第二堂课(为简化起见,我仅设置为第二个网站)。但是,尽管按钮设置类似于其他按钮,但使用setOnClicklistener以及资源中定义的变量,但我收到以下错误消息。 我已经排除了几个片段-错误消息似乎与访问第二类的按钮有关。
为那些琐碎的事情而致歉...我迷失了,但为任何暗示而感激。
java.lang.RuntimeException: Unable to start activity ComponentInfo{paperpad.app/paperpad.app.Mainactivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClicklistener(android.view.View$OnClicklistener)' on a null object reference
strings.xml
<resources>
<string name="logo">Logo</string>
<string name="app_name">PaperPad2</string>
<string name="button_go2activity">Start Drawing</string>
<string name="button_openPicture">Load Picture</string>
<string name="button_closePicture">Stop</string>
<string name="button_help">help</string>
<string name="websiteAddress">PaperPad.app</string>
</resources>
Mainactivity.java
package paperpad.app;
import androidx.annotation.NonNull;
//etc.
public class Mainactivity extends AppCompatactivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button_openHelp = findViewById(R.id.button_openHelp);
button_openHelp.setOnClicklistener(new View.OnClicklistener() {
@Override
public void onClick(View view) {
Intent openHelp = new Intent(Intent.actION_VIEW,Uri.parse("http://www.youtube.com"));
startactivity(openHelp);
}
});
Button button_go2activity = findViewById(R.id.button_go2activity);
button_go2activity.setOnClicklistener(new View.OnClicklistener() {
@Override
public void onClick(View view) {
Intent openLink = new Intent(Intent.actION_VIEW,Uri.parse("http://www.google.com"));
startactivity(openLink);
}
});
}
}
我的activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
tools:context=".Mainactivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="254dp"
android:layout_height="237dp"
android:layout_marginTop="48dp"
android:layout_marginBottom="8dp"
android:contentDescription="@string/logo"
app:layout_constraintBottom_toTopOf="@+id/imageView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/logo_paperpad" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="59dp"
android:layout_marginTop="96dp"
android:layout_marginBottom="100dp"
app:layout_constraintBottom_toTopOf="@+id/button_openPicture"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView"
app:srcCompat="@drawable/logo_titel"
tools:ignore="ContentDescription" />
<Button
android:id="@+id/button_go2activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="269dp"
android:text="@string/button_go2activity"
android:textSize="24sp"
android:typeface="normal"
app:layout_constraintBottom_toTopOf="@+id/button_openHelp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2" />
<Button
android:id="@+id/button_openHelp"
android:layout_width="45dp"
android:layout_height="25dp"
android:layout_marginBottom="10dp"
android:background="#FFFFFF"
android:text="@string/button_help"
android:textColor="@android:color/black"
android:textSize="12sp"
app:layout_constraintBottom_toTopOf="@+id/linkWebsite"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_go2activity" />
<TextView
android:id="@+id/linkWebsite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:inputType="none"
android:text="@string/websiteAddress"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_openHelp" />
</androidx.constraintlayout.widget.ConstraintLayout>