繁体   English   中英

Toast 未在 android 工作室中显示

[英]Toast not showing in android studio

嗨,我正在尝试做一个小项目,但我有一个错误,当我单击一个按钮时,它没有执行预期的操作,即显示 toast,并且没有错误,这对初学者来说有点令人困惑,并试图使用调试器进行调试,但没有任何帮助,所以我希望你能帮助我,谢谢

活动:

public class Book_Actvity extends AppCompatActivity {
    private static final String TAG = "Book_Actvity";
    public static final String BOOK_ID = "bookId";
    private ImageView imgBook;
    private TextView txtBookName, txtAuthor, txtPages, txtShortDescreption, txtLongDescreption;
    private Button btnCurrentlyReading, btnWantToRead, btnAlreadyRead, btnAddToFavorites;
    boolean exist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.book_actvity);
        initViews();
        Intent intent = getIntent();
        if (intent != null) {
            int bookId = intent.getIntExtra(BOOK_ID, 0);
            if (bookId != 0) {
                Book book1 = Utils.getInstance().getBookById(bookId);
                if (null != book1) {
                    setData(book1);
                    addToALreadyReady(book1); } } } }

    private void initViews() {
        imgBook = findViewById(R.id.imgBookInTheBookActvity);
        txtBookName = findViewById(R.id.bookName);
        txtAuthor = findViewById(R.id.txtAuthorNameInBookActivity);
        txtPages = findViewById(R.id.txtPagesNum);
        txtShortDescreption = findViewById(R.id.txtDescreptionInBookActivity);
        txtLongDescreption = findViewById(R.id.txtLongDescreption);
        btnCurrentlyReading = findViewById(R.id.btnCurrentlyReading);
        btnWantToRead = findViewById(R.id.btnWantToReadInBookActvity);
        btnAlreadyRead = findViewById(R.id.btnAlreadyReadinBookActvity);
        btnAddToFavorites = findViewById(R.id.btnAddToFavoritesInBookActvity); }

    private void setData(Book book) {
        txtBookName.setText(book.getName());
        txtAuthor.setText(book.getAuthor());
        txtPages.setText(String.valueOf(book.getPages()));
        txtShortDescreption.setText(book.getShortDesc());
        txtLongDescreption.setText(book.getLongDesc());
        Glide.with(this).asBitmap().load(book.getImageUrl()).into(imgBook); }

    private void addToALreadyReady(final Book book) {
        ArrayList<Book> ArrayListAlreadyReadBook = Utils.getInstance().getAlreadyReadArrayList();
        exist = false;
        for (Book b : ArrayListAlreadyReadBook) {
            if (b.getId() == book.getId()) {
                exist = true; }
            if (exist) {
                btnAlreadyRead.setEnabled(false);
            } else {
                btnAlreadyRead.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (Utils.getInstance().addToAlreadyReadBooks(book)) {
                            Toast.makeText(Book_Actvity.this, "BOOK ADDED", Toast.LENGTH_SHORT).show();
                            System.out.println("DONE");
                        } else {
                            Toast.makeText(Book_Actvity.this, "SOMTHING WRONG",       Toast.LENGTH_SHORT).show();
                            System.out.println("NOT DONE");
                        }
                    }}); } }}}

和实用程序 Class:

    package com.example.newbookstoreplzwork;

import java.util.ArrayList;

public class Utils {
    public static Utils instance;
    private static ArrayList<Book> allBooksArrayList;
    private static ArrayList<Book> currentlyReadingArrayList;
    private static ArrayList<Book> AlreadyReadArrayList;
    private static ArrayList<Book> wishListArrayList;
    private static ArrayList<Book> favoritesArrayList;

    private Utils() {
        if (null == allBooksArrayList) {
            allBooksArrayList = new ArrayList<>();
            initData();
        }
        if (currentlyReadingArrayList == null) {
            currentlyReadingArrayList = new ArrayList<>();

        }
        if (AlreadyReadArrayList == null) {
            AlreadyReadArrayList = new ArrayList<>();

        }
        if (wishListArrayList == null) {
            wishListArrayList = new ArrayList<>();

        }
        if (favoritesArrayList == null) {
            favoritesArrayList = new ArrayList<>();

        }
    }


    private void initData() {
        allBooksArrayList.add(new Book(1, "wimpy kid", "karam Shadi", 134, "https://kbimages1-a.akamaihd.net/a93f1391-68be-478b-acb0-63cd7881aed8/1200/1200/False/the-getaway-diary-of-a-wimpy-kid-book-12.jpg",
                "asdfasd", "jhkjhkhkjhkjghjghgfg"));
        allBooksArrayList.add(new Book(2, "majduddin Book", "majduddin", 90, "https://s3images.coroflot.com/user_files/individual_files/702459_am5ekrun22pd6wav_kbesce8q.png ", "long", "dodo"));

    }

    public static Utils getInstance() {
        if (instance != null) {

            return instance;
        } else {
            instance = new Utils();
            return instance;
        }
    }

    public static ArrayList<Book> getAllBooksArrayList() {
        return allBooksArrayList;
    }

    public static ArrayList<Book> getCurrentlyReadingArrayList() {
        return currentlyReadingArrayList;
    }

    public static ArrayList<Book> getAlreadyReadArrayList() {
        return AlreadyReadArrayList;
    }

    public static ArrayList<Book> getWishListArrayList() {
        return wishListArrayList;
    }

    public static ArrayList<Book> getFavoritesArrayList() {
        return favoritesArrayList;
    }

    public Book getBookById(int id) {
        for (Book b : allBooksArrayList) {
            if (b.getId() == id) {
                return b;
            }
        }
        return null;
    }

    public boolean addToAlreadyReadBooks(Book book) {
        return AlreadyReadArrayList.add(book);
    }
}

XML:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Book_Actvity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:id="@+id/componentsRealtiveLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/imgBookInTheBookActvity"
                android:layout_width="150dp"
                android:layout_height="200dp"
                android:layout_centerHorizontal="false"
                android:layout_marginLeft="30dp"
                android:layout_marginTop="30dp"
                android:padding="16dp"
                android:src="@mipmap/ic_launcher" />

            <Button
                android:id="@+id/btnCurrentlyReading"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="false"
                android:layout_marginLeft="300dp"
                android:layout_marginTop="120dp"
                android:text="Currently Reading" />

            <Button
                android:id="@+id/btnWantToReadInBookActvity"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/btnCurrentlyReading"
                android:layout_marginLeft="300dp"
                android:layout_marginTop="24dp"
                android:text="Want To Read" />

            <Button
                android:id="@+id/btnAlreadyReadinBookActvity"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/btnWantToReadInBookActvity"
                android:layout_marginLeft="300dp"
                android:layout_marginTop="24dp"

                android:text="Already Read" />

            <Button
                android:id="@+id/btnAddToFavoritesInBookActvity"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/btnAlreadyReadinBookActvity"
                android:layout_marginLeft="300dp"
                android:layout_marginTop="24dp"

                android:text="Add to Favorites" />

            <TextView
                android:id="@+id/txtPages"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/txtAuthorInBookActivity"
                android:layout_marginTop="24dp"
                android:text="Pages" />

            <TextView
                android:id="@+id/bookName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/imgBookInTheBookActvity"
                android:layout_marginTop="130dp"
                android:text="Book Name" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/imgBookInTheBookActvity"
                android:layout_marginLeft="24dp"
                android:layout_marginTop="130dp"
                android:layout_toRightOf="@id/bookName"
                android:text="Wimpy Kid" />

            <TextView
                android:id="@+id/txtAuthorInBookActivity"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/bookName"
                android:layout_marginTop="24dp"
                android:text="Author" />

            <TextView
                android:id="@+id/txtDescreptionInBookActivity"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/txtPages"
                android:layout_marginTop="24dp"
                android:text="Descreption" />

            <TextView
                android:id="@+id/txtLongDescreption"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/txtDescreptionInBookActivity"
                android:layout_marginTop="24dp"
                android:text="Long Descreption" />

            <TextView
                android:id="@+id/txtAuthorNameInBookActivity"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/bookName"
                android:layout_marginLeft="50dp"
                android:layout_marginTop="24dp"
                android:layout_toRightOf="@id/txtAuthorInBookActivity"
                android:text="Jeff Kinney" />

            <TextView
                android:id="@+id/txtPagesNum"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/txtAuthorInBookActivity"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="25dp"
                android:layout_toRightOf="@id/bookName"
                android:text="150" />
        </RelativeLayout>

    </ScrollView>


    
</RelativeLayout>

感谢任何试图提供帮助的人

试试这个来理解这个问题:

private void addToALreadyReady(final Book book) {
        ArrayList<Book> ArrayListAlreadyReadBook = Utils.getInstance().getAlreadyReadArrayList();
        exist = false;
        for (Book b : ArrayListAlreadyReadBook) {
            if (b.getId() == book.getId()) {
                exist = true; 
                break;
            }
            
        }

        btnAlreadyRead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!exist) {
                    if (Utils.getInstance().addToAlreadyReadBooks(book)) {
                        Toast.makeText(Book_Actvity.this, "BOOK ADDED", Toast.LENGTH_SHORT).show();
                        System.out.println("DONE");
                    } else {
                        Toast.makeText(Book_Actvity.this, "SOMTHING WRONG", Toast.LENGTH_SHORT).show();
                        System.out.println("NOT DONE");
                    }
                }else {
                    Toast.makeText(Book_Actvity.this, "EXIST TRUE", Toast.LENGTH_SHORT).show();
                    System.out.println("EXISTS");
                }
            }
        });
        
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM