How can I fix this? Save category p back

Asked

Viewed 55 times

1

Guys, I’m gonna try to express myself briefly. I’m venturing into a wallpaper app, but since I don’t have much experience, I can’t deal with this problem.

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

In that attempt, I stumbled upon a fatal mistake:

D/Androidruntime: Shutting down VM AND/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’t 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’t 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;
    }
}

LINE OF ERROR:

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

Then I realized I need to define which category to go to, if it’s not going to be null. Hence, I need to save which category the user was before being downloaded from gif.

Ex as it is done in the category, when it is taken the name, but being 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 that, I need to save what category I was in. How do I do that?

  • Try replacing the line final String category = intent.getStringExtra("category"); for final String category = intent.getExtras().getString("category");

  • Hello, Rosary.Thank you, but unfortunately it didn’t work. The error persists! . Child(Category); I need to identify the category before starting Activity. In case, I need to save the wallpaper category that the user was before startar the gif Activity

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.