E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mylibary, PID: 16960
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mylibary/com.example.mylibary.BookActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.mylibary.BookActivity.handleCurrentlyReadingBooks(BookActivity.java:100)
at com.example.mylibary.BookActivity.onCreate(BookActivity.java:42)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
I/Process: Sending signal. PID: 16960 SIG: 9
班级
package com.example.mylibary;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
public class BookActivity extends AppCompatActivity {
public static final String BOOK_ID_KEY = "bookId";
private Button btnAddCurrentlyReading, btnAddWishList, btnAddAllReadyRead, btnAddToFavorites;
private TextView txtBookName, txtLalala, txtAuthor, txtNikita, txtPages, txtPagesNumber,
txtShortDesk, txtLongDesk;
private ImageView imgBook;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book);
initViews();
Intent intent = getIntent();
if (null != intent) {
int bookId = intent.getIntExtra(BOOK_ID_KEY, -1);
if (bookId != -1) {
Books incomingBook = Utils.getInstance().getBookById(bookId);
if (null != incomingBook) {
setData(incomingBook);
handleAllReadyRead(incomingBook);
handleCurrentlyReadingBooks(incomingBook);
handleWantToReadBooks(incomingBook);
handleFavoriteBooks(incomingBook);
}
}
}
}
private void handleFavoriteBooks(final Books books){
Utils.getInstance();
ArrayList<Books> FavoriteBooks = Utils.getFavoritesBooks();
boolean existInFavoriteBooks = false;
for (Books b: FavoriteBooks){
if (b.getId() == books.getId()) {
existInFavoriteBooks = true;
break;
}
}
if (existInFavoriteBooks){
btnAddToFavorites.setEnabled(false);
}else{
btnAddToFavorites.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Utils.getInstance().addToFavoritesBooks(books)){
Toast.makeText(BookActivity.this, "book add successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(BookActivity.this, FavoritesBooksActivity.class);
startActivity(intent);
}else{
Toast.makeText(BookActivity.this, "try again", Toast.LENGTH_SHORT).show();
}
}
});
}
}
private void handleCurrentlyReadingBooks(final Books books){
Utils.getInstance();
ArrayList<Books> CurrentlyReadingBooks = Utils.getCurrentlyReadingBooks();
boolean existInCurrentlyReadingBooks = false;
for (Books b: CurrentlyReadingBooks){
if (b.getId() == books.getId()) {
existInCurrentlyReadingBooks = true;
break;
}
}
if (existInCurrentlyReadingBooks){
btnAddCurrentlyReading.setEnabled(false);
}else{
btnAddCurrentlyReading.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Utils.getInstance().addCurrentlyReadingBooks(books)){
Toast.makeText(BookActivity.this, "book add successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(BookActivity.this, CurrentlyReadingActivity.class);
startActivity(intent);
}else{
Toast.makeText(BookActivity.this, "try again", Toast.LENGTH_SHORT).show();
}
}
});
}
}
private void handleWantToReadBooks(final Books books){
Utils.getInstance();
ArrayList<Books> WantToReadBooks = Utils.getWantToReadBooks();
boolean existInWantToReadBooks = false;
for (Books b: WantToReadBooks){
if (b.getId() == books.getId()) {
existInWantToReadBooks = true;
break;
}
}
if (existInWantToReadBooks){
btnAddWishList.setEnabled(false);
}else{
btnAddWishList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Utils.getInstance().addWantToReadBooks(books)){
Toast.makeText(BookActivity.this, "book add successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(BookActivity.this, WantToReadActivity.class);
startActivity(intent);
}else{
Toast.makeText(BookActivity.this, "try again", Toast.LENGTH_SHORT).show();
}
}
});
}
}
private void handleAllReadyRead(final Books books){
Utils.getInstance();
ArrayList<Books> AllReadyReadBooks = Utils.getAllReadyReadBooks();
boolean existInAllReadyReadBooks = false;
for (Books b: AllReadyReadBooks){
if (b.getId() == books.getId()) {
existInAllReadyReadBooks = true;
break;
}
}
if (existInAllReadyReadBooks){
btnAddAllReadyRead.setEnabled(false);
}else{
btnAddAllReadyRead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Utils.getInstance().addAllReadyReadBook(books)){
Toast.makeText(BookActivity.this, "book add successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(BookActivity.this, AllReadyReadBooksActivity.class);
startActivity(intent);
}else{
Toast.makeText(BookActivity.this, "try again", Toast.LENGTH_SHORT).show();
}
}
});
}
}
private void setData(Books books) {
txtLalala.setText(books.getName());
txtNikita.setText(books.getAuthor());
txtPagesNumber.setText(String.valueOf(books.getPages()));
txtShortDesk.setText(books.getShortDesk());
txtLongDesk.setText(books.getLongDesk());
Glide.with(this).asBitmap()
.load(books.getImageUrl())
.into(imgBook);
}
private void initViews() {
btnAddCurrentlyReading = findViewById(R.id.btnCurrentlyReading);
btnAddWishList = findViewById(R.id.btnAddWishList);
btnAddAllReadyRead =findViewById(R.id.btnAddAllReadyRead);
btnAddToFavorites = findViewById(R.id.btnAddToFavorites);
txtBookName =findViewById(R.id.txtBookName);
txtLalala = findViewById(R.id.txtLalala);
txtAuthor = findViewById(R.id.txtAuthor);
txtNikita =findViewById(R.id.txtNikita);
txtPages = findViewById(R.id.txtPages);
txtPagesNumber = findViewById(R.id.txtPagesNumber);
txtShortDesk = findViewById(R.id.txtShortDesk);
txtLongDesk = findViewById(R.id.txtLongDesk);
imgBook = findViewById(R.id.imgBook);
}
我做了一个图书馆应用程序,每次我按这本书我都想看看,这给了我这个例外
这里有一些代码
1条答案
按热度按时间vawmfj5a1#
findViewById()
-返回“如果找到具有给定id的视图,否则返回null”,这意味着R.id.btnCurrentlyReading
找不到看看你写得好不好