How can I solve this? Save category p back

1

Well, I'll try to express myself briefly. I'm venturing into a wallpaper app, but since I'm not very experienced, I can not deal with this problem.

In the download adapter, set background and share etc ... I put p when the user set / download the wallpaper, it is transferred to an activity of an animated gif of 3 seconds. In this gif activity, I put p when the time was up, it was transferred back to the category where it was.

In this attempt, I ran into a fatal error:

  

D / AndroidRuntime: Shutting down VM E / AndroidRuntime: FATAL   EXCEPTION: main Process: org.serie.wallpaper, PID: 10289   java.lang.RuntimeException: Unable to start activity   ComponentInfo {org.serie.wallpaper / org.serie.wallpaper.activities.WallpapersActivity}:   java.lang.NullPointerException: Can not pass null for argument   'pathString' in child () at   android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2659)   at   android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2724)   at android.app.ActivityThread.-wrap12 (ActivityThread.java) at   android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1473)   at android.os.Handler.dispatchMessage (Handler.java:102) at   android.os.Looper.loop (Looper.java:154) at   android.app.ActivityThread.main (ActivityThread.java:6123) at   java.lang.reflect.Method.invoke (Native Method) at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:867)   at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:757) Caused   by: java.lang.NullPointerException: Can not pass null for argument   'pathString' in child () at   com.google.firebase.database.DatabaseReference.child (Unknown Source)   at   org.serie.wallpaper.activities.WallpapersActivity.onCreate (WallpapersActivity.java:75)   at android.app.Activity.performCreate (Activity.java:6672) at   android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1140)   at   android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2612)   at   android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2724)   at android.app.ActivityThread.-wrap12 (ActivityThread.java) at   android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1473)   at android.os.Handler.dispatchMessage (Handler.java:102) at   android.os.Looper.loop (Looper.java:154) at   android.app.ActivityThread.main (ActivityThread.java:6123) at   java.lang.reflect.Method.invoke (Native Method) at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:867)   at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:757)

package org.serie.wallpaper.activities;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import org.serie.wallpaper.R;
import org.serie.wallpaper.adapters.WallpapersAdapter;
import org.serie.wallpaper.models.Wallpaper;

import java.util.ArrayList;
import java.util.List;

public class WallpapersActivity extends AppCompatActivity {


    List<Wallpaper> wallpaperList;
    List<Wallpaper> favList;
    RecyclerView recyclerView;
    WallpapersAdapter adapter;

    DatabaseReference dbWallpapers, dbFavs;
    ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wallpapers);

        Intent intent = getIntent();
        final String category = intent.getStringExtra("category");

        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle(category);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new         Intent(getApplicationContext(),HomeActivity.class));
            }
        });

        favList = new ArrayList<>();
        wallpaperList = new ArrayList<>();
        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new WallpapersAdapter(this, wallpaperList);

        recyclerView.setAdapter(adapter);

        progressBar = findViewById(R.id.progressbar);

            dbWallpapers = FirebaseDatabase.getInstance().getReference("images")
                    .child(category);

        if (FirebaseAuth.getInstance().getCurrentUser() != null) {
            dbFavs = FirebaseDatabase.getInstance().getReference("users")
                    .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                    .child("favourites")
                    .child(category);
            fetchFavWallpapers(category);
        } else {
            fetchWallpapers(category);
        }

    }

    private void fetchFavWallpapers(final String category) {
        progressBar.setVisibility(View.VISIBLE);
        dbFavs.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                progressBar.setVisibility(View.GONE);
                if (dataSnapshot.exists()) {
                    for (DataSnapshot wallpaperSnapshot : dataSnapshot.getChildren()) {

                        String id = wallpaperSnapshot.getKey();
                        String title = wallpaperSnapshot.child("title").getValue(String.class);
                        String desc = wallpaperSnapshot.child("desc").getValue(String.class);
                        String url = wallpaperSnapshot.child("url").getValue(String.class);

                        Wallpaper w = new Wallpaper(id, title, desc, url, category);
                        favList.add(w);
                    }
                }
                fetchWallpapers(category);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

    private void fetchWallpapers(final String category) {
        progressBar.setVisibility(View.VISIBLE);
        dbWallpapers.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                progressBar.setVisibility(View.GONE);
                if (dataSnapshot.exists()) {
                    for (DataSnapshot wallpaperSnapshot : dataSnapshot.getChildren()) {

                        String id = wallpaperSnapshot.getKey();
                        String title = wallpaperSnapshot.child("title").getValue(String.class);
                        String desc = wallpaperSnapshot.child("desc").getValue(String.class);
                        String url = wallpaperSnapshot.child("url").getValue(String.class);

                        Wallpaper w = new Wallpaper(id, title, desc, url, category);

                        if (isFavourite(w)) {
                            w.isFavourite = true;
                        }

                        wallpaperList.add(w);
                    }
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

    private boolean isFavourite(Wallpaper w) {
        for (Wallpaper f : favList) {
            if (f.id.equals(w.id)) {
                return true;
            }
        }
        return false;
    }
}

ERROR LINE:

 FirebaseDatabase.getInstance().getReference("images")
 .child(category);

So I realized that I need to define which category should go, if not 'null'. P this, I need to save what category the user was before being transferred to the activity of the gif.

Ex as done in the CategorieAdapter, when the name is taken, but done when the user clicks on the category.

 @Override
        public void onClick(View view) {

            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
            } else {
                mInterstitialAd.loadAd(new AdRequest.Builder().build());
            }

            int p = getAdapterPosition();
            Category c = categoryList.get(p);

            Intent intent = new Intent(mCtx, WallpapersActivity.class);
            intent.putExtra("category", c.name);

            mCtx.startActivity(intent);
        }

But for this, I need to save which category it was. How do I do this?

    
asked by anonymous 25.05.2018 / 10:55

0 answers