简体   繁体   English

调用javascript函数时未定义

[英]Undefined when Calling the javascript function

I'm able to pass data from my android to javascript. 我能够将数据从我的android传递到javascript。 But the problem is when I call the function setValue() it become Undefined but when its on load it pass the right value. 但是问题是当我调用函数setValue()时,它变为未定义,但是在加载时,它传递了正确的值。 Here is my Codes below. 这是我的以下代码。

Here is my Java Class Codes 这是我的Java类代码

public class PaymentActivity extends Activity {

String amount1;


    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.payment);

         Bundle extras = getIntent().getExtras(); 
            if(extras !=null)
            {
                amount1 = extras.getString("amount1");

            }

        WebView web = (WebView) findViewById(R.id.web1);
        web.getSettings().setJavaScriptEnabled(true);
        web.loadUrl("javascript:window.onload = function(){setValue(\""+ amount1 +"\");};");
        web.loadUrl("file:///android_asset/www/index.html");


    }

}

and here is my Web Page Codes 这是我的网页代码

<!DOCTYPE html>
<html>
<head>
</head>
<body>

    Whats your Name?
    <input id="name" value="" />
    <input id="pass" value="" />
    <button onclick = "setValue()">Submit</button>

<script type="text/javascript"> 
    function setValue(amount1){
    myValue = amount1;
    document.getElementById("pass").value = myValue;

    }
</script>

</body>
</html>

You can not call a java script function directly from activity on load. 您不能直接从活动加载时调用Java脚本函数。 You need to use handler and thread and a hidden button 您需要使用处理程序和线程以及一个隐藏的按钮

public class AskUsActivity extends Activity{
private Button scriptButton;
private WebView webView;
private Handler handler;;

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

    webView = (WebView)findViewById(R.id.askUs_webView);
    scriptButton=(Button)findViewById(R.id.scriptButton);

    loadData();

    myJavaScriptInterface = new MyJavaScriptInterface(this);

    webView.loadUrl("file:///android_asset/AskUs.htm");

    webView.addJavascriptInterface(myJavaScriptInterface, "AndroidFunction");

    webView.getSettings().setJavaScriptEnabled(true);

    webView.setWebChromeClient(new WebChromeClient());

    handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            System.out.println("inside handler");
            scriptButton.performClick();
        }
    };

    scriptButton.setOnClickListener(new Button.OnClickListener(){

        @Override
        public void onClick(View arg0) {
            webView.loadUrl("javascript:setcontactInfo('1|*|nikhil|||');");
        }
    });

    //      String selectedImagePath="1|*|nikhil|||";

    //      webView.loadUrl("javascript:setInternational()");

    //      new Thread(){
    //          @Override
    //          public void run() {
    //              System.out.println("inside thread.................Start.......");
    //              webView.loadUrl("javascript:setcontactInfo('1|*|nikhil|||');");
    //              System.out.println("inside thread.................end.......");
    //          }
    //      }.start();

}

@Override
protected void onResume() {
    super.onResume();
    try {
        Thread.sleep(400);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    new Thread(){
        public void run() {
            System.out.println("inside thread................");
            handler.sendEmptyMessage(0);
        };
    }.start();
}


private void loadData(){
    try {

        String destPath = "/data/data/" + getPackageName()+ "/databases/kellypipe.sqlite";

        File f = new File(destPath);
        if(!f.exists()){
            Log.v("<<< TAG >>>","File Not Exist");
            InputStream in = getAssets().open("kellypipe.sqlite");
            OutputStream out = new FileOutputStream(destPath);

            byte[] buffer = new byte[1024];
            int length;
            while ((length = in.read(buffer)) > 0) {
                out.write(buffer, 0, length);
            }
            in.close();
            out.close();
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        Log.v("<<< TAG >>>","ioexeption");
        e.printStackTrace();
    }

    DBManager dbManager =  new DBManager(this);
    Log.v("<<< TAG >>>","Database is there with version: "+dbManager.getReadableDatabase().getVersion());
    //      String sql = "select * from CasingMetric";

    String sql= "SELECT ID,listname,name,address,contact,email from contactInfo order by ID";


    SQLiteDatabase db = dbManager.getReadableDatabase();
    Cursor cursor = db.rawQuery(sql, null);
    Log.v("<<< TAG >>>","Query Result:"+cursor);
    cursor.moveToFirst();
    int i=cursor.getColumnIndex("listname");
    int j=cursor.getColumnIndex("name");

    int x=0;
    while(!cursor.isAfterLast()){
        x++;
        System.out.println("Listname : "+cursor.getString(i)+ "  Name: "+cursor.getString(j)+" :: X is : "+x);
        cursor.moveToNext();
    }

    cursor.close();
    db.close();
    dbManager.close();
}

public class MyJavaScriptInterface {
    Context mContext;

    MyJavaScriptInterface(Context c) {
        mContext = c;
    }

    public void showToast(String toast){
        //Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
        webView.loadUrl("javascript:callFromActivity()");
    }

    public void openAndroidDialog(){
        AlertDialog.Builder myDialog = new AlertDialog.Builder(AskUsActivity.this);
        myDialog.setTitle("DANGER!");
        myDialog.setMessage("You can do what you want!");
        myDialog.setPositiveButton("ON", null);
        myDialog.show();
    }
}

}

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

相关问题 Javascript类-调用函数时未定义不是函数 - Javascript Class - undefined is not a function when calling function 调用javascript原型方法时未定义的函数 - undefined function when calling javascript prototype method 从另一个 function javascript 调用异步 function 时变得未定义 - Getting undefined when calling an async function from another function javascript 使用未定义的值调用javascript函数 - Calling javascript function with undefined value 调用函数时变得不确定 - Getting undefined when calling the function 调用函数时,返回未定义 - When calling function, it returns undefined 调用Javascript函数将返回具有未定义值的函数 - Calling Javascript function returns a function with undefined values 未捕获的TypeError:undefined不是函数调用JavaScript函数 - Uncaught TypeError: undefined is not a function Calling JavaScript Function Kendo grid - 调用javascript函数时出错:“javascript运行时错误:&#39;funcName&#39;未定义” - Kendo grid - error when calling javascript function: “javascript runtime error: 'funcName' is undefined” 从另一个JavaScript文件调用javascript函数时,为什么会得到未定义的属性? - Why I'am getting undefined property when calling a javascript function from another javascript file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM