0

Here, in my Custom Adapter class, while i am tracing the error which i have found after pressing on the image, it goes through the onClick method successfully, attain the intent, but it crashes when it attempts to start the second Activity.

package com.example.prof_mohamed.movieapp;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

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

/**
 * Created by Prof-Mohamed on 8/3/2016.
 */

public class ImagesAdapter extends         RecyclerView.Adapter<ImagesAdapter.ViewHOlder> {
    private List<MovieEntity> feedItemList;

    private Context mContext;


    public ImagesAdapter(Context context, ArrayList<MovieEntity>     feedItemList) {
        this.feedItemList=feedItemList;
        this.mContext= context;
    }

    @Override
    public ImagesAdapter.ViewHOlder onCreateViewHolder(ViewGroup parent,     int i) {
        View view =     LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, null);

        RecyclerView.ViewHolder viewHolder=new ViewHOlder(view);
        return (ViewHOlder) viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHOlder customViewholder, final int     i) {

        final MovieEntity feedItem=feedItemList.get(i);
            Picasso.with(mContext).load(feedItem.getPOSTER_PATH_STRING()).into(customView    holder.one_img);

        customViewholder.one_text.setText(feedItem.getTITLE_STRING());
        customViewholder.one_img.setOnClickListener(new     View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext,DetailActivity.class)
                        .putExtra(Intent.EXTRA_TEXT,feedItem);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return (null!=feedItemList?feedItemList.size():0);
    }

    public class ViewHOlder extends RecyclerView.ViewHolder {
        protected ImageView one_img;
        protected TextView one_text;

        public ViewHOlder(View converview) {
            super(converview);

            this.one_img = (ImageView)     converview.findViewById(R.id.img_view);
            this.one_text = (TextView)     converview.findViewById(R.id.txt_poster_title);
        }
    }
}

Here are my second Activity, which i have to send my data from the first Activity to it :

package com.example.prof_mohamed.movieapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class DetailActivity extends AppCompatActivity {

    private static String mMovieStr;

        TextView txtTitle=(TextView) findViewById(R.id.txt_title);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        Intent intent = getIntent();

        if(intent != null&&intent.hasExtra(intent.EXTRA_TEXT)){
        mMovieStr=intent.getStringExtra(intent.EXTRA_TEXT);
        txtTitle.setText(mMovieStr);
    }
}
}

This is my error result :

E/AndroidRuntime: FATAL EXCEPTION: main android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? at android.app.ContextImpl.startActivity(ContextImpl.java:1278) at android.app.ContextImpl.startActivity(ContextImpl.java:1265) at com.example.prof_mohamed.movieapp.ImagesAdapter$1.onClick(ImagesAdapter.java:52) at android.view.View.performClick(View.java:4432) at android.view.View$PerformClick.run(View.java:18339) at android.os.Handler.handleCallback(Handler.java:725) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5283) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) at dalvik.system.NativeStart.main(Native Method)

and after editing these lines inside the onClick method

@Override
public void onClick(View view) {
Intent intent = new Intent(mContext,DetailActivity.class)
                    .putExtra(Intent.EXTRA_TEXT,feedItem);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(intent);
        }

it gave me a Null pointer Exception of the txtTitle (TextView) in the DetailActivity.java class.

Can anybody help me to solve this problem. your response will be appreciated.

Mohamed Atef
  • 69
  • 13
  • Can you check the error logs and see if doing something on txtTitle is giving null pointer exception ? – AmanSinghal Aug 13 '16 at 15:01
  • @AmanSinghal I have edited my post to include the error logs results, please review it in the above post – Mohamed Atef Aug 13 '16 at 15:09
  • you pass a data object into intent, then getString on the other side? won't that cause problems? shouldn't you be passing `feedItem.getTITLE_STRING()`? – TWL Aug 13 '16 at 15:12
  • i think passing this data object her efeedItem.getTITLE_STRING() is true, but it didn't stop here it stops in this line: mContext.startActivity(intent); why ! – Mohamed Atef Aug 13 '16 at 15:19
  • you can start you activity before add flag like intend.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) – Farmer Aug 13 '16 at 15:26
  • after using this line, it gave me for a first time java.lang.NullPointerException .... what is the null value here !? – Mohamed Atef Aug 13 '16 at 15:41
  • @AmanSinghal Dear, it found the txtTitle as a null value as u expected, what should i do ? – Mohamed Atef Aug 13 '16 at 15:46

5 Answers5

0

try this:

Intent intent = new Intent(mContext,DetailActivity.class)
                       .putExtra(Intent.EXTRA_TEXT,feedItem);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
dwqdq13213
  • 317
  • 1
  • 10
0

Try adding the following code after creating the intent

myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
AmanSinghal
  • 2,404
  • 21
  • 22
0

In your adapter class you are passing object,but in second activity you are trying to getStringExtra()

Read this it will be helpful

How to send an object from one Android Activity to another using Intents?

Community
  • 1
  • 1
shahid17june
  • 1,441
  • 1
  • 10
  • 15
-1

Please check you defined DetailActivity in manifest file.

Ramit
  • 416
  • 3
  • 8
-1

feedItem is an object. You are passing this with intent and trying to get it from second activity as String, and that is the problem.