我的博客应用程序已崩溃

时间:2014-10-15 15:02:48

标签: java android

我有一个问题。我已经制作了我的博客应用。但是,当我运行这个应用程序时,它突然崩溃了。这是我的源代码

    package blog.app;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity
{
    private WebView myWebView;
    private WebSettings myWebSettings;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        WebView myWebView=(WebView) findViewById(R.id.webview);
        myWebView.loadUrl("http://teknorats.blogspot.com");
        WebSettings myWebSettings=myWebView.getSettings();
        myWebSettings.getJavaScriptEnabled();
        myWebView.setWebViewClient(new WebVewClient());

    }

    private static class WebVewClient extends WebViewClient {

        public WebVewClient() {
        }
    }

    public class WebAppInterface{
        Context mContext;
        WebAppInterface(Context c) {
            mContext=c;
        }

        private WebAppInterface() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
         public void showToast(String toast) {
            Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();

         WebView myWebView = (WebView) findViewById(R.id.webview);
         myWebView.addJavascriptInterface(new WebAppInterface(),"Android");

    }
    }

    @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
        myWebView.goBack();
        return true;
    }
    // If it wasn't the Back key or there's no web page history, bubble up to the default
    // system behavior (probably exit the activity)
    return super.onKeyDown(keyCode, event);
}
}

这是xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/webview"
    android:src="@drawable/tr_launcher"
    >
</LinearLayout>

请,我需要您使用该代码进行更正。

如果你问我在哪里得到这段代码,我从这里得到了这段代码http://developer.android.com/guide/webapps/webview.html

1 个答案:

答案 0 :(得分:3)

似乎在您的xml文件中LinearLayout标记的标识为webview而您正试图获取 将LinearLayout投射到WebView

所以改变

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/webview"
    android:src="@drawable/tr_launcher"
    >
</LinearLayout>

<Webview xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/webview">
</Webview>

并且您已将myWebView定义为类变量,因此请更改

WebView myWebView=(WebView) findViewById(R.id.webview);

myWebView=(WebView) findViewById(R.id.webview);