java - 包含 WebView 的 Intent

标签 java android eclipse android-intent webview

如何调用包含 webview 的 Intent 我使用了以下代码 它与前两个 Intent 完美配合

package com.ambitionme;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class ambition extends Activity implements OnClickListener,
        OnItemSelectedListener {
    EditText url;
    WebView ourBrow;
    private Spinner spinner;
    private static final String[] paths = { "item 1", "item 2", "item 3" };

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

        ourBrow = (WebView) findViewById(R.id.wvBrowser);
        ourBrow.getSettings().setJavaScriptEnabled(true);
        ourBrow.getSettings().setLoadWithOverviewMode(true);
        ourBrow.getSettings().setUseWideViewPort(true);
        ourBrow.setWebViewClient(new ourViewClient());
        try {
            ourBrow.loadUrl("http://www.ambitionme.com");
        } catch (Exception e) {
            e.printStackTrace();
        }

        spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(ambition.this,
                android.R.layout.simple_spinner_item, paths);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);

    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View v, int position,
            long id) {

        // TODO Auto-generated method stub
        switch (position) {
        case 0:
            // What ever you want to happen when item 1 selected
            break;
        case 1:
            // What ever you want to happen when item 2 selected
            break;
        case 2:
            // What ever you want to happen when item 3 selected
            break;

        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

    public boolean onCreateOptionsMenu(android.view.Menu menu) {
        // TODO Auto-generated method stub
        super.onCreateOptionsMenu(menu);

        MenuInflater blowUp = getMenuInflater();
        blowUp.inflate(R.menu.coolmenu, menu);

        return true;

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId()) {

        case R.id.aboutus:
            Intent i = new Intent("com.ambitionme.ABOUT");
            startActivity(i);

            break;

        case R.id.preferences:
            Intent p = new Intent("com.ambitionme.PREFS");
            startActivity(p);
            break;

        case R.id.exit:
            finish();
            break;

        case R.id.login:

            Intent k = new Intent("com.ambitionme.LOGIN");
            startActivity(k);


            break;

        case R.id.contactus:
            break;
        }

        return true;
    }

}

Intent类如下

package com.ambitionme;

import android.app.Activity;
import android.os.Bundle;

import android.webkit.WebView;

public class Login extends Activity {

    WebView ourBrow1;

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

        ourBrow1 = (WebView) findViewById(R.id.wvLogin);
        ourBrow1.getSettings().setJavaScriptEnabled(true);
        ourBrow1.getSettings().setLoadWithOverviewMode(true);
        ourBrow1.getSettings().setUseWideViewPort(true);
        ourBrow1.setWebViewClient(new ourViewClient());
        try {
            ourBrow1.loadUrl("http://www.ambitionme.com/login");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

显示以下错误

AndroidRuntime(31416): FATAL EXCEPTION: main

AndroidRuntime(31416): Process: com.ambitionme, PID: 31416

AndroidRuntime(31416): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ambitionme/com.ambitionme.Login}:

android.view.InflateException: Binary XML file line #7: Error inflating class Webview

AndroidRuntime(31416): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)

AndroidRuntime(31416): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)

AndroidRuntime(31416): at android.app.ActivityThread.access$800(ActivityThread.java:135)

AndroidRuntime(31416): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)

AndroidRuntime(31416): at android.os.Handler.dispatchMessage(Handler.java:102)

AndroidRuntime(31416): at android.os.Looper.loop(Looper.java:136)

AndroidRuntime(31416): at android.app.ActivityThread.main(ActivityThread.java:5017)

AndroidRuntime(31416): at java.lang.reflect.Method.invokeNative(Native Method)

AndroidRuntime(31416): at java.lang.reflect.Method.invoke(Method.java:515) AndroidRuntime(31416): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) AndroidRuntime(31416): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

AndroidRuntime(31416): at dalvik.system.NativeStart.main(Native Method)

AndroidRuntime(31416): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class Webview

AndroidRuntime(31416): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)

AndroidRuntime(31416): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)

AndroidRuntime(31416): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)

AndroidRuntime(31416): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)

AndroidRuntime(31416): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)

AndroidRuntime(31416): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)

AndroidRuntime(31416): at android.app.Activity.setContentView(Activity.java:1929)

AndroidRuntime(31416): at com.ambitionme.Login.onCreate(Login.java:16)

AndroidRuntime(31416): at android.app.Activity.performCreate(Activity.java:5231)

AndroidRuntime(31416): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)

AndroidRuntime(31416): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

AndroidRuntime(31416): ... 11 more

AndroidRuntime(31416): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.Webview" on path: DexPathList[[zip file "/data/app/com.ambitionme-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.ambitionme-1, /vendor/lib, /system/lib]]

AndroidRuntime(31416): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)

AndroidRuntime(31416): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)

AndroidRuntime(31416): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)

AndroidRuntime(31416): at android.view.LayoutInflater.createView(LayoutInflater.java:559) AndroidRuntime(31416): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:652)

AndroidRuntime(31416): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)

AndroidRuntime(31416): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)

AndroidRuntime(31416): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)

AndroidRuntime(31416): ... 21 more

最佳答案

在你的login.xml中,尽量写WebView而不是Webview,它区分大小写。

关于java - 包含 WebView 的 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24312804/

相关文章:

android - 抽屉中的 SwipeRefreshLayout 手势检测

java - 如何获取我的 android 项目中文件夹的相对路径?

java - svn 导致 eclipse 中的构建错误

java - 在java中创建一个带有2个键(索引和键)的映射

android - 如何在Android中设计EditText和Button

java - java中字符串相乘的方法

android - 未创建sqlite数据库

eclipse - 如何导出具有依赖项的 Eclipse 插件?

java - 调用KeyFactory.createKey是不是很耗资源?

java - MySql Java 自增