eclipse 如何修复方法 startActivity(Intent) 未定义为类型 new View.OnClickListener() 语法错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17505969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 20:51:46  来源:igfitidea点击:

How to fix The method startActivity(Intent) is undefined for the type new View.OnClickListener() syntax error

androideclipseandroid-emulator

提问by Akari

I have a syntax errors with my code , in the "getView" I want to make a listener for the button "update" to move to another activity :

我的代码有语法错误,在“getView”中,我想为按钮“更新”创建一个侦听器以移动到另一个活动:

enter image description here

在此处输入图片说明

@Override
    public View getView(int position, View convertView, ViewGroup parent) {


        LayoutInflater l = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        View rowView = l.inflate(R.layout.temp, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.textView1);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView1);
        Button update = (Button) rowView.findViewById(R.id.button1);

//      update.setOnClickListener(new View.OnClickListener() {
//          
//          @Override
//          public void onClick(View v) {
//
//                 Intent redirect = new Intent(getApplicationContext(),Update.class);
//                 startActivity(redirect);
//              
//          }
//      });




        textView.setText(sa.get(position));


        return rowView;
    }

I've tried to fix these errors about "Intent" but I failed :(

我试图修复这些关于“意图”的错误,但我失败了:(

  1. The method startActivity(Intent) is undefined for the type new View.OnClickListener()

  2. The method getApplicationContext() is undefined for the type new View.OnClickListener()

  1. 方法 startActivity(Intent) 未定义为 new View.OnClickListener() 类型

  2. 方法 getApplicationContext() 对于 new View.OnClickListener() 类型未定义

and even whene I moved these statements from "onClick" method the problem didn't change !! I imported "Intent" library , how to solve that ????

甚至当我从“onClick”方法中移动这些语句时,问题也没有改变!!我导入了“意图”库,如何解决????

回答by Raghunandan

If your adapter is in a different file you will need activity context.

如果您的适配器在不同的文件中,您将需要活动上下文。

You need to pass the activity context from your activity class to your adapter constructor.

您需要将活动上下文从您的活动类传递给您的适配器构造函数。

http://developer.android.com/reference/android/content/Context.html#startActivity(android.content.Intent)

http://developer.android.com/reference/android/content/Context.html#startActivity(android.content.Intent)

startActivityis a method of your activity class. So you need activity context to start the activity.

startActivity是您的活动类的方法。所以你需要活动上下文来启动活动。

Also instead of getApplicationContextuse activity context.

也不要getApplicationContext使用活动上下文。

When to call activity context OR application context?

何时调用活动上下文或应用程序上下文?

Check the answer by commonsware.

检查commonsware的答案。

     update.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {

            Intent redirect = new Intent(context,Update.class);
            context.startActivity(redirect);    
      }
      });

回答by Wand Maker

Try

尝试

final Activity thisActivity = this;

update.setOnClickListener(new View.OnClickListener() {

          @Override
          public void onClick(View v) {

                 Intent redirect = new Intent(thisActivity,Update.class);
                 thisActivity.startActivity(redirect);

          }
      });

回答by hadi

for similar problem for me just this helped me :

对于我的类似问题,这对我有帮助:

  @Override
  public void onClick(View v) {
                    Intent intent = new Intent(G.context, SecondActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    G.context.startActivity(intent);
  }
  });

add context in G Class :

在 G 类中添加上下文:

public class G extends Activity {

    public static Context context;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        context = getApplicationContext();

    }
}

回答by AAnkit

You just need to call context.startActivityinstead, can follow below steps

你只需要打电话context.startActivity,可以按照以下步骤

Three simple steps

三个简单的步骤

1) Declare local Variable Context mCtx

1) 声明局部变量 Context mCtx

2) intialize it with Activity context either by passing a parameter to constructor/method, or in the onCreate method if you are using above code in activity.

2)通过将参数传递给构造函数/方法或在 onCreate 方法中(如果您在活动中使用上述代码)使用 Activity 上下文初始化它。

3) call mCtx.startActivity(intent);

3) 打电话 mCtx.startActivity(intent);

or

或者

you can call ActivityName.this.startActivity()

你可以打电话 ActivityName.this.startActivity()

Edit:- As par dmon comment, You simply can use your local context instance

编辑:- 作为 par dmon 评论,您只需使用本地上下文实例

回答by ArgyrisChatzaras

The root of your problem is that you cannot start an Activity from an application Context (only from an activity context you have the right to do that). The reason for that is that android bundles all activities of your application as a group (task) in order to allow multitasking between different apps. When you launch a new app, or press the home button for example, you are application (together with its activities backstack) is going to the background and leaves the scene to a new task (with a different activity backstack and so on and so forth). For more info on this mechanism you can take a look here (http://developer.android.com/guide/components/tasks-and-back-stack.html).

您问题的根源在于您无法从应用程序上下文启动 Activity(只有从您有权执行此操作的 Activity 上下文)。这样做的原因是 android 将应用程序的所有活动捆绑为一个组(任务),以允许不同应用程序之间进行多任务处理。例如,当您启动一个新应用程序或按下主页按钮时,您的应用程序(连同其活动后台堆栈)将转到后台并将场景留给新任务(具有不同的活动后台堆栈等等) )。有关此机制的更多信息,您可以查看此处(http://developer.android.com/guide/components/tasks-and-back-stack.html)。

In order to gain a context that is ok to lauch a new activity from, you can always get the context of the parent of the view you are inflating, by calling viewgroup.getContext() - eventhough you will have to declare the viewgroup as final, which is ok since you shouldn't be touching the parent.

为了获得可以从中启动新活动的上下文,您始终可以通过调用 viewgroup.getContext() 获取正在膨胀的视图的父级的上下文 - 即使您必须将视图组声明为最终,这没关系,因为你不应该接触父母。

a draft of your getView:

你的 getView 草稿:

@Override
public View getView(int position, View convertView, final ViewGroup parent) {


    LayoutInflater l = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    View rowView = l.inflate(R.layout.temp, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.textView1);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView1);
    Button update = (Button) rowView.findViewById(R.id.button1);

  update.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
             Intent redirect = new Intent(parent.getContext(),Update.class);
             startActivity(redirect);
      }
  });




    textView.setText(sa.get(position));


    return rowView;
}