Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement StartActivityForResult in RecyclerView

I am trying to use an startActivityForResult in my RecyclerView and don't know how... tried this way and i get a NullPointExeption from Activity, is there any way to solve it.

This is Activity:

    RecyclerView recyclerView=findViewById(R.id.todo_Recyclerview);
    Todo_Adapter adapter=new Todo_Adapter(this,Todo_DataGenerator.getData(this));
    recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
    recyclerView.setAdapter(adapter);
}

TextView title=findViewById(R.id.todo_txt_Titleitem);
TextView content=findViewById(R.id.todo_txt_Contentitem);


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);


    if (requestCode== Request &&
            resultCode==RESULT_OK &&
            data !=null){

        String Title=data.getStringExtra(Todo_saveActivity.Todo_Title);
        String Content=data.getStringExtra(Todo_saveActivity.Todo_Content);

        title.setText(Title);
        content.setText(Content);

This is Adapter:

public class Todo_Adapter extends RecyclerView.Adapter<Todo_Adapter.Todo_itemViewHolder> {
public static final int Request = 1002;

private Context context;
private List<Todo_SampleClass> Notes;



public Todo_Adapter(Context context, List<Todo_SampleClass> Notes) {

    this.context = context;
    this.Notes = Notes;

}

@Override
public Todo_itemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater layoutInflater = LayoutInflater.from(context);
    View view = layoutInflater.inflate(R.layout.todo_rv_item, parent, false);
    return new Todo_itemViewHolder(view);

}


@Override
public void onBindViewHolder(Todo_itemViewHolder holder, int position) {
    holder.bindNotes(Notes.get(position));
    holder.Add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent=new Intent(context, Todo_saveActivity.class);
            ((Activity) context).startActivityForResult(intent,Request);

        }
    });

}


@Override
public int getItemCount() {
    return Notes.size();
}

public class Todo_itemViewHolder extends RecyclerView.ViewHolder {

    private TextView Title;
    private TextView Content;
    private ImageButton edit;
    private ImageButton remove;
    private Button Add;

    public Todo_itemViewHolder(View itemView) {
        super(itemView);
        Title = itemView.findViewById(R.id.todo_txt_Titleitem);
        Content = itemView.findViewById(R.id.todo_txt_Contentitem);
        edit = itemView.findViewById(R.id.todo_btn_edit);
        remove = itemView.findViewById(R.id.todo_btn_remove);
        Add = itemView.findViewById(R.id.todo_btn_add);

    }


    public void bindNotes(Todo_SampleClass Notes) {
        Title.setText(Notes.getTitle());
        Content.setText(Notes.getContent());
        edit.setImageDrawable(Notes.getImgbtn_edit());

        remove.setImageDrawable(Notes.getImgbtn_remove());
    }

    }

}

This is second Activity that i am trying to go:

public class Todo_saveActivity extends AppCompatActivity {


public static final String Todo_Title ="result";
public static final String Todo_Content="content";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_todo_save);


    final EditText Title=findViewById(R.id.todo_name);
   final EditText Content=findViewById(R.id.todo_family);

This is logcat:

  java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.expert2.expert2/com.example.expert2.expert2.rv_Todo.Todo_rv}: java.lang.NullPointerException
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
                  at android.app.ActivityThread.access$600(ActivityThread.java:141)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
                  at android.os.Handler.dispatchMessage(Handler.java:99)
                  at android.os.Looper.loop(Looper.java:137)
                  at android.app.ActivityThread.main(ActivityThread.java:5103)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:525)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                  at dalvik.system.NativeStart.main(Native Method)
               Caused by: java.lang.NullPointerException
                  at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:117)
                  at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:149)
                  at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:29)
                  at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:54)
                  at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:202)
                  at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
                  at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
                  at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
                  at com.example.expert2.expert2.rv_Todo.Todo_rv.<init>(Todo_rv.java:29)
                  at java.lang.Class.newInstanceImpl(Native Method)
                  at java.lang.Class.newInstance(Class.java:1130)
                  at android.app.Instrumentation.newActivity(Instrumentation.java:1061)

Thanks.------------------------------------------------------------------

like image 563
Mohsen Avatar asked Sep 10 '25 20:09

Mohsen


1 Answers

When looking at this code:

public static final int Request = 1002;

 @Override
public void onBindViewHolder(Todo_itemViewHolder holder, int position) {
    holder.bindNotes(Notes.get(position));
    holder.Add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent=new Intent(context, Todo_saveActivity.class);
            ((Activity) context).startActivityForResult(intent,Request);

        }
    });

}

It looks to me like ((Activity) context) seems to be the problem. You are trying to call startActivityForResult(...) with an Activity object that is null. Depending on whether your RecyclerView is in an Activity or Fragment, make sure that you are setting context equal to a valid instance of your parent Activity's context, like:

Context context = getContext(); //if in fragment
Context context = (Context)this; //if in parent activity

and then later on you should be able to call

((Activity) context).startActivityForResult(intent,Request);

without any issues.

like image 156
Shane Sepac Avatar answered Sep 13 '25 10:09

Shane Sepac