简体   繁体   English

对于new Runnable(){}类型,未定义方法findViewById(int)

[英]The method findViewById(int) is undefined for the type new Runnable(){}

I'm working on making a Phonegap plugin for Android. 我正在为Android开发一个Phonegap插件。 When I add the findViewById method to this.ctx.runOnUiThread(new Runnable() I get an error as stated in the title. 当我将findViewById方法添加到this.ctx.runOnUiThread(new Runnable() ,出现标题中所述的错误。

Here is my code: 这是我的代码:

package com.company.msgbox;


import java.io.File;

import org.crossplatform.phonegap.trial.alternativeTo.R;
import org.json.JSONArray;
import org.json.JSONException;

import android.app.AlertDialog;
import android.graphics.Bitmap;
import android.view.View;

import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import com.phonegap.api.PluginResult.Status;

public class msgbox extends Plugin {

    private static final String SHOW = "show";
    private static final int MSG_INDEX = 0;
    private String msg;

    @Override
    public PluginResult execute(String arg0, final JSONArray arg1, String arg2) {
        if ( arg0.equals(SHOW) ) {
            this.ctx.runOnUiThread(new Runnable() {
                public void run() {
                    // try/catch generated by editor
                    try {
                        msg = arg1.getString(MSG_INDEX);
                    } catch (JSONException e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
                    }

                    AlertDialog alertDialog = new AlertDialog.Builder(ctx).create();
                    alertDialog.setTitle("Title");
                    alertDialog.setMessage(msg);
                    alertDialog.show();

                    View content = findViewById(R.id.layoutroot);
                    Bitmap bitmap = content.getDrawingCache();
                    File file = new File("/sdcard/test.png");
                }
            });
        }

        return new PluginResult(Status.OK);
    }

}

You need to call findViewById from a class that actually has the method. 您需要从实际具有该方法的类中调用findViewById。 The good practice is usually to pass around the Activity you're creating this class from. 好的做法通常是绕过您要从中创建此类的Activity。 Something like: 就像是:

public class msgbox extends Plugin {
    private static final String SHOW = "show";
    private static final int MSG_INDEX = 0;
    private String msg;
    private final Activity parent;

    // constructor
    public msgbox(Activity parent) {
        this.parent = parent;
    }

Then you can do a: 然后,您可以执行以下操作:

parent.findViewById(R.id.layoutroot)

You construct your msgbox from within an activity by using: 您可以使用以下方法在活动中构造msgbox:

msgbox myMsgBox = new msgbox(this);

Of course, to do that, the R.id.layoutroot component must be in the activity you passed. 当然,要做到这一点, R.id.layoutroot组件必须位于您传递的活动中。

If you're not in an activity when you construct the msgbox, you can replace the constructor injection with a setter: 如果构造msgbox时不在活动中,则可以用setter代替构造函数注入:

public void setParent(Activity parent) {
    this.parent = parent;
}

Although, to be able to use findViewById within your Runnable , parent needs to be final, so you'll have to copy it to a final variable (setter injection cannot be final, obviously) 尽管为了能够在Runnable使用findViewById,parent必须是final,所以您必须将其复制到final变量(显然,setter注入不能是final)

(NB: Also, your class doesn't use standard java naming conventions, it's confusing: call it MsgBox ) (注意:此外,您的类不使用标准的Java命名约定,这很令人困惑:将其称为MsgBox

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对于类型PostDetail(片段),未定义方法findViewById(int) - The method findViewById(int) is undefined for the type PostDetail (Fragment) 对于新的Runnable(){}类型,未定义方法getActivity() - The method getActivity() is undefined for the type new Runnable(){} 方法findViewById(int)未定义类型R.layout - The method findViewById(int) is undefined for the type R.layout 适配器的类型和控制器也未定义方法findViewById(int) - The method findViewById(int) is undefined for the type and the controller of the adapter too 对于Jsoup解析器,使用AsyncTask为类型定义了方法findViewById(int) - The method findViewById(int) is undefined for the type using AsyncTask for Jsoup Parser Android SDK:类型未定义的findViewById(int) - Android SDK: findViewById(int) undefined for type 对于 Runnable 类型,方法 start() 未定义 - The method start() is undefined for the type Runnable 对于ServerWorker类型,方法start()未定义...(java Runnable) - The method start() is undefined for the type ServerWorker… (java Runnable) Android webview:未定义类型new WebChromeClient(){}的方法setProgress(int) - Android webview: The method setProgress(int) is undefined for the type new WebChromeClient(){} 无法解析方法findViewById(“ int”) - Cannot resolve method findViewById(“int”)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM