尝试设置“ImageBackground”时应用程序崩溃

时间:2021-01-31 13:54:17

标签: java android-studio android-fragments crash imageview

我有两个片段:RadlerKarteDeckFragment。在 RadlerKarte 中,您可以单击按钮 choose。如果您按下该按钮,片段将更改为 DeckFragment。在那里您可以点击 9 个不同的 ImageViewdeckKarte1deckKarte2deckKarte3 等。如果您按 deckKarte1deckKarte1 的图像应该更改为 radler_klein_level_1,但前提是您之前在 choose 中按了 RadlerKarte(如果您从一开始就在 DeckFragment 我想通过点击 {{1 }}).

这是deckKarte1的代码:

RadlerKarte

来自package com.example.bottles; import android.os.Bundle; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentTransaction; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageButton; import android.widget.ImageView; public class RadlerKarte extends Fragment { Button radlerWahl; ImageView deckKarte1; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View v = inflater.inflate(R.layout.fragment_radler_karte, container, false); radlerWahl = v.findViewById(R.id.radlerKarteWahlen); deckKarte1 = v.findViewById(R.id.deck_karte_1); radlerWahl.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { DeckFragment deckFragment = new DeckFragment(); FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.replace(R.id.fragment_container, deckFragment); transaction.commit(); deckKarte1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { deckKarte1.setBackgroundResource(R.drawable.radler_klein_level_1); } }); } }); return v; } }

DeckFragment

问题是:当我点击 package com.example.bottles; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; public class DeckFragment extends Fragment { @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_deck, container, false); } } 时,我的应用程序崩溃了,我不知道为什么...

如果有帮助,这是我的错误代码:

choose

有人可以帮我吗?

0 个答案:

没有答案