App crashes when trying to set `ImageBackground`

0

I have two fragments: RadlerKarte and DeckFragment. In RadlerKarte you can click a button choose. If you press that button, the fragment changes to DeckFragment. There you can click 9 different ImageView: deckKarte1, deckKarte2, deckKarte3, etc. If you press deckKarte1, the Image of deckKarte1 should change to radler_klein_level_1, but only if you pressed choose in RadlerKarte before (if you are in DeckFragment from the beginning I want to have a different reaction by clicking deckKarte1).

Here is the code of 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;
    }
}

And here from 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);
    }
}

The Problem is: When I click choose my app crashes, and I don't know why...

If it helps, here is my error code:


01/31 14:41:30: Launching 'app' on OnePlus ONEPLUS A6003.
$ adb shell am start -n "com.example.bottles/com.example.bottles.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 31111 on device 'oneplus-oneplus_a6003-2beda810'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/Perf: Connecting to perf service.
I/example.bottle: [GL_OOM] ClampGrowthLimit 268435456
V/Font: Change font:2
    Default family:android.graphics.Typeface@1b512b59
E/Perf: Fail to get file list com.example.bottles
    getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
    Fail to get file list com.example.bottles
E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
W/example.bottle: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
W/example.bottle: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
V/ViewRootImpl: The specified message queue synchronization  barrier token has not been posted or has already been removed
D/DecorView: onWindowFocusChangedFromViewRoot hasFocus: true, DecorView@45d1a26[MainActivity]
I/AdrenoGLES: QUALCOMM build                   : 35556ba, I9ca166462c
    Build Date                       : 08/07/19
    OpenGL ES Shader Compiler Version: EV031.27.02.00
    Local Branch                     : 
    Remote Branch                    : 
    Remote Branch                    : 
    Reconstruct Branch               : 
    Build Config                     : S P 8.0.8 AArch64
I/AdrenoGLES: PFP: 0x016ee187, ME: 0x00000000
W/Gralloc3: mapper 3.x is not supported
D/: Successfully load libgui-plugin.so, this=0x798f030050
D/OnePlusJankManager:  Chor uploadMDM JANK_TYPE_ONCE mViewTitle = com.example.bottles/com.example.bottles.MainActivity--- jank level = 1
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.bottles, PID: 31111
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.bottles.RadlerKarte$1.onClick(RadlerKarte.java:34)
        at android.view.View.performClick(View.java:7201)
        at android.view.View.performClickInternal(View.java:7170)
        at android.view.View.access$3500(View.java:806)
        at android.view.View$PerformClick.run(View.java:27562)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7682)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
I/Process: Sending signal. PID: 31111 SIG: 9

Can anyone help me?

java
android-studio
android-fragments
crash
imageview
asked on Stack Overflow Jan 31, 2021 by gi pi • edited Jan 31, 2021 by gi pi

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0