如何在内部静态类中使用 Intent 启动活动? [英] How start an activity using Intent inside a inner static class?

查看:30
本文介绍了如何在内部静态类中使用 Intent 启动活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Activityclass 中有一个 staticinner 类.我需要使用 Intent(context,nameOfTheNewActivity.class) 调用 Activity,因为当来自 RecyclerView.ViewHolderIntent代码>被点击.我必须覆盖 onClick 以获取使用 getLayoutPosition() 单击的项目的位置(此 getLayoutPosition() 工作正常).

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).

现在当我尝试使用 Intent 时出现错误:

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

非静态方法不能被静态上下文引用.

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

我从 Stackoverflow 中阅读了另一个链接,例如 this.在这种静态上下文和内部类内部的情况下,如何调用 Intent,即,如何获取内部类内部的上下文,以及如何解决 ** 基本 ** 错误以不调用非静态来自静态类的类?

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?

在问这里之前,我尝试了以下方法:

  1. 使用 v.context 从视图获取上下文,但我继续解决这个问题 - 并且仍然从静态上下文调用非静态方法.

  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.

静态"startActivity(Intent) 方法?

从我的内部类中删除 static 一词,但没有解决,应用程序崩溃.

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

我的代码:

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//
            }
        });
    }
}

推荐答案

尝试使用 Activity 的引用.

Try using the reference of the Activity.

ActivityOne.this.startActivity(intent);

如果这不起作用,那么要知道 startActivity 是任何 Context 的方法.

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);
            }
        });

    }

参考.如何在 recyclerView item onclick 上打开不同的活动

关于

从我的内部类中删除static这个词,但没有解决,应用程序崩溃

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

您可能更接近于移除 static 的解决方案.实际构建的应用程序.应用程序崩溃意味着您应该阅读 logcat 并实施正确的解决方案.

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.

这篇关于如何在内部静态类中使用 Intent 启动活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆