How start an activity using Intent inside a inner

2019-09-06 13:59发布

问题:

I have a static and inner class inside my Activityclass. I need to call an Activity using Intent(context,nameOfTheNewActivity.class) because I am trying to call an Intent when an item from a RecyclerView.ViewHolder is clicked. I had to Override the onClick to get the position of the item was clicked using getLayoutPosition() (this getLayoutPosition() worked fine).

Now when I try to use Intent I have the error:

Non-static method cannot be referenced by a static context.

I read another links from Stackoverflow like this. How do I call an Intent in this case of static context and inside an inner class, I.e, how do I get the context inside an inner class, and how do I solve the **fundamental ** error to do not call a non static class from an static class?

I tried the following before ask here:

  1. Get the context from the View using v.context but I continue with the problem - and still calling a non static method from a static context.

  2. "Static" startActivity(Intent) method?

  3. Delete the word static form my inner class, but did not solve and the app crashes.

My code:

public class ActivityOne extends AppCompatActivity implements
GoogleApiClient.OnConnectionFailedListener {

    public static class MessageViewHolderOfFriend extends RecyclerView.ViewHolder {   public  MessageViewHolderOfFriend(View v) {
        super(v);
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ;

                Intent intent = new Intent(v.getContext(),NewActivityToRun.class);
                startActivity(intent);//Error in this Line//
            }
        });
    }
}

回答1:

Try using the reference of the Activity.

ActivityOne.this.startActivity(intent);

If that doesn't work, then know that startActivity is a method of any Context.

class MessageViewHolderOfFriend extends RecyclerView.ViewHolder {

    private final Context context;

    public  MessageViewHolderOfFriend(View v) {
        super(v);
        context = v.getContext();

        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,NewActivityToRun.class);
                context.startActivity(intent);
            }
        });

    }

Ref. How to open a different activity on recyclerView item onclick

Regarding

delete the word static from my inner class, but did not solve and the app crashes

You probably were closer to the solution with the removal of static. The app actually built. The app crashing means you should read the logcat and implement the proper solution.



回答2:

You need use the Context to start SecondActivity, when you are using context.startActivity(intent) and you should add intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) before startActivity(intent)

MainActivity.java :

public class MainActivity extends AppCompatActivity {

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

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // method 1:
            MyInnerClass innerClass = new MyInnerClass();
            innerClass.firstLauncher(MainActivity.this);

           // method 2:
           // MyInnerClass.secondLauncher(MainActivity.this);
        }
    });
}


static class MyInnerClass {

    /**
     * member method
     */
    private void firstLauncher(Context context) {
        Intent intent = new Intent(context, SecondActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("flag_activity", "I'm 1th");
        context.startActivity(intent);
    }

    /**
     * static method
     */
    private static void secondLauncher(Context context) {
        Intent intent = new Intent(context, SecondActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("flag_activity", "I'm 2th");
        context.startActivity(intent);
    }

}
}

SecondActivity.java :

public class SecondActivity extends Activity {

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

    TextView textView = (TextView) findViewById(R.id.tv_msg);

    String msg = getIntent().getStringExtra("flag_activity");
    textView.setText(msg);
}
}

All the implements are in the ContextImpl.java file.