Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javaScriptEnable(true);android上出现空指针异常错误_Javascript_Android_Html_Webview_Runtime Error - Fatal编程技术网

javaScriptEnable(true);android上出现空指针异常错误

javaScriptEnable(true);android上出现空指针异常错误,javascript,android,html,webview,runtime-error,Javascript,Android,Html,Webview,Runtime Error,我想写一个简单的计算器。我有一个SimpleActivity类,如下所示: package org.muzzy.calc; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.webkit.WebChromeClient; import android.webkit.Web

我想写一个简单的计算器。我有一个SimpleActivity类,如下所示:

package org.muzzy.calc;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;

public class SimpleActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private WebView numer;
private Button guzik;
private StringBuilder wys;

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

    numer = (WebView) findViewById(R.id.webview);
    numer.getSettings().setJavaScriptEnabled(true);
    numer.setWebChromeClient(new WebChromeClient());

    guzik = (Button) findViewById(R.id.button0);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.button1);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.button2);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.button3);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.button4);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.button5);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.button6);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.button7);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.button8);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.button9);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.plus);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.minus);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.multi);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.divide);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.leftP);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.rightP);
    guzik.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    StringBuilder wyr = new StringBuilder();

    switch (v.getId()) {
    case R.id.bs:
        if (wyr.length() > 0)
            wyr.deleteCharAt(wyr.length() - 1);
        break;
    case R.id.ac:
        if (wyr.length() > 0)
            wyr.delete(0, wyr.length());
        break;
    default:
        wyr.append(((Button) v).getText());
    }

    wys.append("<html><body>");
    wys.append("<script type=\"text/javascript\">document.write('");
    wys.append(wyr.toString());
    wys.append("');");
    wys.append("document.write('<br />=' + eval(\"");
    wys.append(wyr.toString());
    wys.append("\"));</script>");
    wys.append("</body></html>");

    numer.loadData(wys.toString(), "application/xhtml", "UTF-8");
}

}
Intent simpleIntent = new Intent(this, SimpleActivity.class);
            startActivity(simpleIntent);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<WebView
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    android:visibility="visible" />

<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="0,1,2,3" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button1"
            android:text="@string/b1" />

        <Button
            android:id="@+id/button2"
            android:text="@string/b2" />

        <Button
            android:id="@+id/button3"
            android:text="@string/b3" />

        <Button
            android:id="@+id/plus"
            android:text="@string/plus" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button4"
            android:text="@string/b4" />

        <Button
            android:id="@+id/button5"
            android:text="@string/b5" />

        <Button
            android:id="@+id/button6"
            android:text="@string/b6" />

        <Button
            android:id="@+id/minus"
            android:text="@string/minus" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button7"
            android:text="@string/b7" />

        <Button
            android:id="@+id/button8"
            android:text="@string/b8" />

        <Button
            android:id="@+id/button9"
            android:text="@string/b9" />

        <Button
            android:id="@+id/multi"
            android:text="@string/multi" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/leftP"
            android:text="@string/leftParen" />

        <Button
            android:id="@+id/button0"
            android:text="@string/b0" />

        <Button
            android:id="@+id/rightP"
            android:text="@string/rightParen" />

        <Button
            android:id="@+id/divide"
            android:text="@string/divide" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/bs"
            android:text="@string/backspace" />

        <Button
            android:id="@+id/dot"
            android:text="@string/dot" />

        <Button
            android:id="@+id/ac"
            android:text="@string/allclear" />

        <Button
            android:id="@+id/equals"
            android:text="@string/equals" />
    </TableRow>
</TableLayout>

</LinearLayout>
当我试图在VM Android 2.3.3上运行该项目时,我遇到以下错误:

03-07 21:52:36.624: D/AndroidRuntime(411): Shutting down VM
03-07 21:52:36.624: W/dalvikvm(411): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-07 21:52:36.634: E/AndroidRuntime(411): FATAL EXCEPTION: main
03-07 21:52:36.634: E/AndroidRuntime(411): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.muzzy.calc/org.muzzy.calc.SimpleActivity}: java.lang.NullPointerException
03-07 21:52:36.634: E/AndroidRuntime(411):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-07 21:52:36.634: E/AndroidRuntime(411):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-07 21:52:36.634: E/AndroidRuntime(411):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-07 21:52:36.634: E/AndroidRuntime(411):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-07 21:52:36.634: E/AndroidRuntime(411):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-07 21:52:36.634: E/AndroidRuntime(411):  at android.os.Looper.loop(Looper.java:123)
03-07 21:52:36.634: E/AndroidRuntime(411):  at android.app.ActivityThread.main(ActivityThread.java:3683)
03-07 21:52:36.634: E/AndroidRuntime(411):  at java.lang.reflect.Method.invokeNative(Native Method)
03-07 21:52:36.634: E/AndroidRuntime(411):  at java.lang.reflect.Method.invoke(Method.java:507)
03-07 21:52:36.634: E/AndroidRuntime(411):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-07 21:52:36.634: E/AndroidRuntime(411):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-07 21:52:36.634: E/AndroidRuntime(411):  at dalvik.system.NativeStart.main(Native Method)
03-07 21:52:36.634: E/AndroidRuntime(411): Caused by: java.lang.NullPointerException
03-07 21:52:36.634: E/AndroidRuntime(411):  at org.muzzy.calc.SimpleActivity.onCreate(SimpleActivity.java:25)
03-07 21:52:36.634: E/AndroidRuntime(411):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-07 21:52:36.634: E/AndroidRuntime(411):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-07 21:52:36.634: E/AndroidRuntime(411):  ... 11 more
我的simpleActivity xml文件如下所示:

package org.muzzy.calc;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;

public class SimpleActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private WebView numer;
private Button guzik;
private StringBuilder wys;

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

    numer = (WebView) findViewById(R.id.webview);
    numer.getSettings().setJavaScriptEnabled(true);
    numer.setWebChromeClient(new WebChromeClient());

    guzik = (Button) findViewById(R.id.button0);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.button1);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.button2);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.button3);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.button4);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.button5);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.button6);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.button7);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.button8);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.button9);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.plus);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.minus);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.multi);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.divide);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.leftP);
    guzik.setOnClickListener(this);
    guzik = (Button) findViewById(R.id.rightP);
    guzik.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    StringBuilder wyr = new StringBuilder();

    switch (v.getId()) {
    case R.id.bs:
        if (wyr.length() > 0)
            wyr.deleteCharAt(wyr.length() - 1);
        break;
    case R.id.ac:
        if (wyr.length() > 0)
            wyr.delete(0, wyr.length());
        break;
    default:
        wyr.append(((Button) v).getText());
    }

    wys.append("<html><body>");
    wys.append("<script type=\"text/javascript\">document.write('");
    wys.append(wyr.toString());
    wys.append("');");
    wys.append("document.write('<br />=' + eval(\"");
    wys.append(wyr.toString());
    wys.append("\"));</script>");
    wys.append("</body></html>");

    numer.loadData(wys.toString(), "application/xhtml", "UTF-8");
}

}
Intent simpleIntent = new Intent(this, SimpleActivity.class);
            startActivity(simpleIntent);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<WebView
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    android:visibility="visible" />

<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="0,1,2,3" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button1"
            android:text="@string/b1" />

        <Button
            android:id="@+id/button2"
            android:text="@string/b2" />

        <Button
            android:id="@+id/button3"
            android:text="@string/b3" />

        <Button
            android:id="@+id/plus"
            android:text="@string/plus" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button4"
            android:text="@string/b4" />

        <Button
            android:id="@+id/button5"
            android:text="@string/b5" />

        <Button
            android:id="@+id/button6"
            android:text="@string/b6" />

        <Button
            android:id="@+id/minus"
            android:text="@string/minus" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button7"
            android:text="@string/b7" />

        <Button
            android:id="@+id/button8"
            android:text="@string/b8" />

        <Button
            android:id="@+id/button9"
            android:text="@string/b9" />

        <Button
            android:id="@+id/multi"
            android:text="@string/multi" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/leftP"
            android:text="@string/leftParen" />

        <Button
            android:id="@+id/button0"
            android:text="@string/b0" />

        <Button
            android:id="@+id/rightP"
            android:text="@string/rightParen" />

        <Button
            android:id="@+id/divide"
            android:text="@string/divide" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/bs"
            android:text="@string/backspace" />

        <Button
            android:id="@+id/dot"
            android:text="@string/dot" />

        <Button
            android:id="@+id/ac"
            android:text="@string/allclear" />

        <Button
            android:id="@+id/equals"
            android:text="@string/equals" />
    </TableRow>
</TableLayout>

</LinearLayout>


我在很多论坛上读了很多教程和帖子,但仍然一无所获。我做错了什么???

双击该行的Log Cat窗口: 03-07 21:52:36.634:E/AndroidRuntime(411):在org.muzzy.calc.SimpleActivity.onCreate(SimpleActivity.java:25)上

这将使您看到导致问题的源代码行

而且你忘了一份声明 wys=新的StringBuilder()


忘了用一个新的按钮来清除它,就像在大多数情况下,这是一个简单的错误。我的SimpleActivity布局不是主要的,而是简单的。主布局中没有webview,因此它无法工作,并返回空指针异常。

是否将新活动(.SimpleActivity)添加到清单中?看起来很奇怪。尝试一个项目->清理。编辑器中的第25行是什么?我在清单中添加了如下活动:
,导致问题的行是:
numer.getSettings().setJavaScriptEnabled(true)清理项目不起作用。谢谢回答。当我发布我的问题时,我很累,我忘了写导致问题的行是:
numer.getSettings().setJavaScriptEnabled(true)