如何解决android.view.InflateException

时间:2019-01-09 13:14:30

标签: android

我已经做了一些尝试来纠正错误。由于我不了解它,因此我没有其他解决方法。

这是我的XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bgLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="20dp"
            android:layout_marginBottom="20dp"
            android:clickable="true"
            app:backgroundTint="@color/black"
            app:srcCompat="@drawable/ic_coins2" />

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <!--<include layout="@layout/toolbar" />-->

        </android.support.design.widget.AppBarLayout>

        <android.support.v4.widget.SwipeRefreshLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/SwipeRefreshLayout">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_view2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/bg_black"
                android:scrollbars="vertical" />

        </android.support.v4.widget.SwipeRefreshLayout>

    </RelativeLayout>

    <!--bottom sheet container-->
    <FrameLayout
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/bottom_sheet_behavior" />

</android.support.design.widget.CoordinatorLayout>

这是我的代码

public class BottomSheetBasic extends AppCompatActivity {
    private BottomSheetBehavior mBehavior;
    private BottomSheetDialog mBottomSheetDialog;
    private View bottom_sheet;
    private RecyclerView recyclerView;
    private GridLayoutManager gridLayoutManager;
    private CustomAdapter adapter;
    private List<MyData> data_list;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    ProgressDialog dialog;
    String currentUid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottom_sheet_basic);
        bottom_sheet = findViewById(R.id.bottom_sheet);
        mBehavior = BottomSheetBehavior.from(bottom_sheet);
        //showBottomSheetDialog();
        dialog = new ProgressDialog(this);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showBottomSheetDialog();
            }
        });

        //BackButton
        if(getSupportActionBar()!=null){
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

        sum_gold_auszahlen sum_gold = new sum_gold_auszahlen(BottomSheetBasic.this);
        sum_gold.execute();

        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.SwipeRefreshLayout);

        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                load_data_from_server(0);
                mSwipeRefreshLayout.setRefreshing(false);
            }
        });



        FirebaseUser user  = FirebaseAuth.getInstance().getCurrentUser();
        currentUid = user.getUid();

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view2);
        data_list  = new ArrayList<>();
        load_data_from_server(0);

        gridLayoutManager = new GridLayoutManager(this,1);
        recyclerView.setLayoutManager(gridLayoutManager);

        adapter = new CustomAdapter(this,data_list);
        recyclerView.setAdapter(adapter);

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

                if(gridLayoutManager.findLastCompletelyVisibleItemPosition() == data_list.size()-1){
                    load_data_from_server(data_list.size()-1);
                }
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        } else {
            Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
        }
        return super.onOptionsItemSelected(item);
    }


    private void showBottomSheetDialog() {
        SharedPreferences sharedPrefs = getSharedPreferences("preferences", 0);
        Float gold = sharedPrefs.getFloat("gold2",0f);
        String next_payout = sharedPrefs.getString("next_payout","");
        final String playername =sharedPrefs.getString("nickname","");

        if (mBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
            mBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
        }

        final View view = getLayoutInflater().inflate(R.layout.sheet_auszahlung, null);
        //((TextView) view.findViewById(R.id.tv_gold)).setText("aa");
        //sum_gold_auszahlen sum_gold_auszahlen = new sum_gold_auszahlen();
        //sum_gold_auszahlen.execute();
        final RadioButton rb_1000 = (RadioButton) view.findViewById(R.id.rb_1000);
        final RadioButton rb_2500 = (RadioButton) view.findViewById(R.id.rb_2500);
        final RadioButton rb_5500 = (RadioButton) view.findViewById(R.id.rb_5500);
        final TextView tv_gold = (TextView) view.findViewById(R.id.tv_gold);
        final TextView tv_next_payout = (TextView) view.findViewById(R.id.tv_next_payout);

        tv_gold.setText(gold.toString());
        tv_next_payout.setText(next_payout);
        (view.findViewById(R.id.bt_close)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mBottomSheetDialog.dismiss();
            }
        });

        (view.findViewById(R.id.btn_payment)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Float val = Float.parseFloat( tv_gold.getText().toString() );

                if (rb_1000.isChecked()) {
                    if (val >= 1000) {
                        Auszahlung backgroundTask = new Auszahlung(BottomSheetBasic.this);
                        backgroundTask.execute("loot", playername, "1000");
                        val = val -1000f;
                        String s = String.format(Locale.US, "%.2f",val);
                        tv_gold.setText(s);
                    } else {
                        Toast toast = Toast.makeText(BottomSheetBasic.this,getString(R.string.not_enough_gold),Toast.LENGTH_LONG);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                    }
                } else if (rb_2500.isChecked()) {
                    if (val >= 2400) {
                        Auszahlung backgroundTask = new Auszahlung(BottomSheetBasic.this);
                        backgroundTask.execute("loot", playername, "2400");
                        val = val -2400f;
                        String s = String.format(Locale.US, "%.2f",val);
                        tv_gold.setText(s);
                    } else {
                        Toast toast = Toast.makeText(BottomSheetBasic.this,getString(R.string.not_enough_gold),Toast.LENGTH_LONG);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                    }
                } else if (rb_5500.isChecked()) {
                    if (val >= 5200) {
                        Auszahlung backgroundTask = new Auszahlung(BottomSheetBasic.this);
                        backgroundTask.execute("loot", playername, "5200");
                        val = val -5200f;
                        String s = String.format(Locale.US, "%.2f",val);
                        tv_gold.setText(s);
                    } else {
                        Toast toast = Toast.makeText(BottomSheetBasic.this,getString(R.string.not_enough_gold),Toast.LENGTH_LONG);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                    }
                }
            }
        });

        mBottomSheetDialog = new BottomSheetDialog(this);
        mBottomSheetDialog.setContentView(view);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Objects.requireNonNull(mBottomSheetDialog.getWindow()).addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

        mBottomSheetDialog.show();
        mBottomSheetDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                mBottomSheetDialog = null;
            }
        });
    }

这是我的LogCat

这就是Google控制台给我的。不幸的是我没有更多。我也没有模仿这个错误

android.view.InflateException: 
  at android.view.LayoutInflater.inflate (LayoutInflater.java:539)
  at android.view.LayoutInflater.inflate (LayoutInflater.java:423)
  at android.view.LayoutInflater.inflate (LayoutInflater.java:374)
  at de.bluestarfish.wotgold_digger.activity.BottomSheetBasic.showBottomSheetDialog (BottomSheetBasic.java:144)
  at de.bluestarfish.wotgold_digger.activity.BottomSheetBasic.access$000 (BottomSheetBasic.java:49)
  at de.bluestarfish.wotgold_digger.activity.BottomSheetBasic$1.onClick (BottomSheetBasic.java:74)
  at android.view.View.performClick (View.java:5265)
  at android.view.View$PerformClick.run (View.java:21534)
  at android.os.Handler.handleCallback (Handler.java:815)
  at android.os.Handler.dispatchMessage (Handler.java:104)
  at android.os.Looper.loop (Looper.java:207)
  at android.app.ActivityThread.main (ActivityThread.java:5728)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:789)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:679)
Caused by: android.view.InflateException: 
  at android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:782)
  at android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:704)
  at android.view.LayoutInflater.rInflate (LayoutInflater.java:835)
  at android.view.LayoutInflater.rInflateChildren (LayoutInflater.java:798)
  at android.view.LayoutInflater.rInflate (LayoutInflater.java:838)
  at android.view.LayoutInflater.rInflateChildren (LayoutInflater.java:798)
  at android.view.LayoutInflater.inflate (LayoutInflater.java:515)
Caused by: android.content.res.Resources$NotFoundException: 
  at android.content.res.Resources.loadDrawableForCookie (Resources.java:2834)
  at android.content.res.Resources.loadDrawable (Resources.java:2756)
  at android.content.res.TypedArray.getDrawable (TypedArray.java:870)
  at android.view.View.<init> (View.java:4001)
  at android.widget.TextView.<init> (TextView.java:700)
  at android.widget.Button.<init> (Button.java:109)
  at android.widget.Button.<init> (Button.java:105)
  at android.support.v7.widget.AppCompatButton.<init> (AppCompatButton.java:71)
  at android.support.v7.widget.AppCompatButton.<init> (AppCompatButton.java:67)
  at android.support.v7.app.AppCompatViewInflater.createButton (AppCompatViewInflater.java:187)
  at android.support.v7.app.AppCompatViewInflater.createView (AppCompatViewInflater.java:110)
  at android.support.v7.app.AppCompatDelegateImpl.createView (AppCompatDelegateImpl.java:1266)
  at android.support.v7.app.AppCompatDelegateImpl.onCreateView (AppCompatDelegateImpl.java:1316)
  at android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:746)

我不能给你更多细节。

1 个答案:

答案 0 :(得分:1)

这看起来像stacktrace的重要部分:

Caused by: android.content.res.Resources$NotFoundException: 
  at android.content.res.Resources.loadDrawableForCookie (Resources.java:2834)
  at android.content.res.Resources.loadDrawable (Resources.java:2756)
  ...
  at android.support.v7.widget.AppCompatButton.<init> (AppCompatButton.java:67)
  at android.support.v7.app.AppCompatViewInflater.createButton (AppCompatViewInflater.java:187)

这对我说的是,您在某个<Button>标签处使用了无法找到的可绘制对象。通常,如果您使用例如android:drawableLeft属性和一个可绘制对象,即<vector>资源。

我在您发布的XML中看不到任何<Button>标签(FloatingActionButton是ImageView / ImageButton的子类,而不是Button的子类),这意味着它可能是您的sheet_auszahlung.xml布局文件中的标签

至于如何解决...

如果您使用的是androidx,则androidx.appcompat 1.1.0-alpha01版本将支持app:drawableLeftCompat之类的属性,这些属性可以“使用”矢量可绘制对象。

如果您不使用androidx,则必须使用Java创建一个Drawable对象并将其手动设置为视图:

Drawable d = AppCompatResources.getDrawable(context, R.drawable.whatever_you_need);
button.setCompoundDrawables(d, null, null, null);