吐司未在Android Studio中显示

马吉杜丁·奥尔本

嗨,我正在尝试做一个小项目,我遇到了一个错误,即当我单击一个按钮时,它没有执行预期的操作,即显示吐司,并且没有错误,这对于初学者来说是一种困惑,并试图使用debbuger进行调试,但没有任何帮助,所以我希望您能帮助我,谢谢

活动:

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");
                        }
                    }}); } }}}

和实用程序类:

    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>

感谢任何尝试帮助的人

NehaK

尝试此操作以了解问题:

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");
                }
            }
        });
        
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Android:无法显示吐司

来自分类Dev

吐司不显示-Android

来自分类Dev

Android显示最新的吐司

来自分类Dev

吐司消息未在x秒内显示为片段?

来自分类Dev

Android Studio Gradle风格未在Build Variants中显示

来自分类Dev

操作栏选项未在Android Studio中显示

来自分类Dev

Android Studio 2.0未在预览中显示菜单图标

来自分类Dev

android studio 视图未在设计模式中显示

来自分类Dev

图像未在 android studio 中与 Picasso 一起显示

来自分类Dev

无法显示非活动类的吐司(Android)

来自分类Dev

无法在吐司中显示吐司

来自分类Dev

如何在Android中取消吐司?

来自分类Dev

NUnit类别未在Visual Studio中显示

来自分类Dev

ServiceStack未在Visual Studio中显示

来自分类Dev

菜单中的图标未在android中显示

来自分类Dev

Android检测图像上是否有白色并显示吐司

来自分类Dev

通过android studio安装的应用未在启动器中显示

来自分类Dev

src / main / webapp / WEB-INF文件夹未在Android Studio中显示

来自分类Dev

第二个布局未在Android Studio中显示为包含标签

来自分类Dev

Flutter Web设备未在Android Studio和VS代码中显示

来自分类Dev

回收者视图未在Android Studio中显示一张卡片视图

来自分类Dev

在预览中显示Android Studio中的视图

来自分类Dev

SoftKeyboard未在Xamarin Android中显示

来自分类Dev

列未在Android的GridLayout中显示

来自分类Dev

卢比符号未在Android中显示

来自分类Dev

Android设备未在Eclipse中显示

来自分类Dev

数据未在gridView Android中显示

来自分类Dev

动态添加的行未在android中显示

来自分类Dev

外部图片未在Android中显示

Related 相关文章

热门标签

归档