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.
Try adding the following code after creating the intent
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);