android.webkit.WebView 無法轉換為 android.widget.Button (android.webkit.WebView cannot be cast to android.widget.Button)


問題描述

android.webkit.WebView 無法轉換為 android.widget.Button (android.webkit.WebView cannot be cast to android.widget.Button)

I was working on a GUI inside of my app. I originally had a crappy look to it that just contained all of the basic stuff to test my java functions in. After I had all the functions up and running I redisgned the GUI using the graphical editor in Eclipse. The next time I ran my app I had the android.webkit.WebView cannot be cast to android.widget.Button in my LogCat. I didn't change anything in the java and all the declarations in the GUI are the same as far as I can tell. I can easily revert the GUI back to the crappy version but that doesn't help me much. I hope someone on here can help me find my problem. I have posted the xml, java, and LogCat.

05‑09 13:25:19.756: E/AndroidRuntime(10424): FATAL EXCEPTION: main
05‑09 13:25:19.756: E/AndroidRuntime(10424): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dcc/com.example.dcc.ActionItem}: 
java.lang.ClassCastException: android.webkit.WebView cannot be cast to android.widget.Button
05‑09 13:25:19.756: E/AndroidRuntime(10424):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
05‑09 13:25:19.756: E/AndroidRuntime(10424):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
05‑09 13:25:19.756: E/AndroidRuntime(10424):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
05‑09 13:25:19.756: E/AndroidRuntime(10424):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
05‑09 13:25:19.756: E/AndroidRuntime(10424):    at android.os.Handler.dispatchMessage(Handler.java:99)
05‑09 13:25:19.756: E/AndroidRuntime(10424):    at android.os.Looper.loop(Looper.java:137)
05‑09 13:25:19.756: E/AndroidRuntime(10424):    at android.app.ActivityThread.main(ActivityThread.java:4745)
05‑09 13:25:19.756: E/AndroidRuntime(10424):    at java.lang.reflect.Method.invokeNative(Native Method)
05‑09 13:25:19.756: E/AndroidRuntime(10424):    at java.lang.reflect.Method.invoke(Method.java:511)
05‑09 13:25:19.756: E/AndroidRuntime(10424):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05‑09 13:25:19.756: E/AndroidRuntime(10424):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05‑09 13:25:19.756: E/AndroidRuntime(10424):    at dalvik.system.NativeStart.main(Native Method)
05‑09 13:25:19.756: E/AndroidRuntime(10424): Caused by: java.lang.ClassCastException: android.webkit.WebView cannot be cast to android.widget.Button
05‑09 13:25:19.756: E/AndroidRuntime(10424):    at com.example.dcc.ActionItem.onCreate(ActionItem.java:54)
05‑09 13:25:19.756: E/AndroidRuntime(10424):    at android.app.Activity.performCreate(Activity.java:5008)
05‑09 13:25:19.756: E/AndroidRuntime(10424):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
05‑09 13:25:19.756: E/AndroidRuntime(10424):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
05‑09 13:25:19.756: E/AndroidRuntime(10424):    ... 11 more



<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="4" >
<LinearLayout
    android:layout_width="212dp"
    android:layout_height="match_parent"
    android:layout_column="0"
    android:layout_columnSpan="3"
    android:layout_gravity="left"
    android:layout_row="0"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/Button1_name"
        android:drawableRight="@drawable/news"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/Button2_name" />
    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/Button3_name"
        android:drawableRight="@drawable/cal"/>
    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/Button4_name"
        android:drawableRight="@drawable/mail"/>
    <Button
        android:id="@+id/button5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/Button5_name"
        android:drawableRight="@drawable/gallery" />
    <Button
        android:id="@+id/button6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/Button6_name"
        android:drawableRight="@drawable/report" />
    <Button
        android:id="@+id/button7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/Button7_name"
        android:drawableRight="@drawable/ep" />
    <Button
        android:id="@+id/button8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/Button8_name"
        android:drawableRight="@drawable/directory" />
</LinearLayout>
<GridLayout
android:id="@+id/a"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_column="1"
android:layout_columnSpan="2"
android:layout_gravity="left"
android:layout_row="0"
android:columnCount="2" >
</GridLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="10dp"
android:layout_height="fill_parent"
android:layout_column="2"
android:layout_gravity="left"
android:layout_row="0"
android:background="@drawable/black"
tools:ignore="ContentDescription" />

 



<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="238dp"
    android:layout_height="fill_parent"
    android:layout_column="3"
    android:layout_gravity="left|top"
    android:layout_row="0"
    android:background="@android:color/black" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/feedtitle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/feeddescribtion"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/feedpubdate"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/feedlink"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:autoLink="web" />

        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="594dp" >
        </ListView>

        <TextView
            android:id="@android:id/empty"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="waiting" />
    </LinearLayout>
</ScrollView>

<WebView
    android:id="@+id/webView1"
    android:layout_width="830dp"
    android:layout_height="555dp"
    android:layout_column="3"
    android:layout_gravity="right|bottom"
    android:layout_row="0" />

<Button
    android:id="@+id/email_button1"
    android:layout_width="585dp"
    android:layout_height="114dp"
    android:layout_column="3"
    android:layout_gravity="center_horizontal|top"
    android:layout_row="0"
    android:text="Submit Action Item Now" />

<TextView
    android:id="@+id/actionView"
    android:layout_width="243dp"
    android:layout_height="113dp"
    android:layout_column="3"
    android:layout_gravity="right|top"
    android:layout_row="0"
    android:text="Loading" />


</GridLayout>





package com.example.dcc;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class ActionItem extends ListActivity implements OnClickListener {
    // Create buttons Globally so they are available to all methods
    public static RSSFeed myRssFeed = null;
    private Button btnEmail = null;
    static WebView webView;
    Button newsB;
    Button loginB;
    Button calB;
    Button mailB;
    Button photoB;
    Button reportB;
    Button actionB;
    Button directoryB;

    String currentItem;
    TextView thisAction;
    int currentLocation;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.action_item);

        btnEmail = (Button) findViewById(R.id.email_button1);
        newsB = (Button) findViewById(R.id.button1);
        loginB = (Button) findViewById(R.id.button2);
        calB = (Button) findViewById(R.id.button3);
        mailB = (Button) findViewById(R.id.button4);
        photoB = (Button) findViewById(R.id.button5);
        reportB = (Button) findViewById(R.id.button6);
        actionB = (Button) findViewById(R.id.button7);
        directoryB = (Button) findViewById(R.id.button8);
        thisAction = (TextView) findViewById(R.id.actionView);

        newsB.setOnClickListener(this);
        loginB.setOnClickListener(this);
        calB.setOnClickListener(this);
        mailB.setOnClickListener(this);
        photoB.setOnClickListener(this);
        reportB.setOnClickListener(this);
        actionB.setOnClickListener(this);
        directoryB.setOnClickListener(this);
        btnEmail.setOnClickListener(this);

        new MyTask().execute();

        // this portion of the onCreate is dedicated to running the webView
        // another webpage: http://creflodollarministries.org/
        // main webpage: http://www.virtualDiscoveryCenter.net
        webView = (WebView) findViewById(R.id.webView1);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("http://www.virtualDiscoveryCenter.net");
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        Uri data = getIntent().getData();

        if (savedInstanceState == null) {
            String url = getIntent().getDataString();
            // do something with this URL.
            webView.loadUrl(url);
        }

    }

    private class MyTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... arg0) {
            try {
                URL rssUrl = new URL(
                        "http://www.virtualdiscoverycenter.net/feed/");// http://www.virtualdiscoverycenter.net/feed/
                SAXParserFactory mySAXParserFactory = SAXParserFactory
                        .newInstance();
                SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
                XMLReader myXMLReader = mySAXParser.getXMLReader();
                RSSHandler myRSSHandler = new RSSHandler();
                myXMLReader.setContentHandler(myRSSHandler);
                InputSource myInputSource = new InputSource(rssUrl.openStream());
                myXMLReader.parse(myInputSource);

                myRssFeed = myRSSHandler.getFeed();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            if (myRssFeed != null) {
                TextView feedTitle = (TextView) findViewById(R.id.feedtitle);
                TextView feedDescribtion = (TextView) findViewById(R.id.feeddescribtion);
                TextView feedPubdate = (TextView) findViewById(R.id.feedpubdate);
                TextView feedLink = (TextView) findViewById(R.id.feedlink);
                feedTitle.setText(myRssFeed.getTitle());
                feedDescribtion.setText(myRssFeed.getDescription());
                feedPubdate.setText(myRssFeed.getPubdate());
                feedLink.setText(myRssFeed.getLink());

                ArrayAdapter<RSSItem> adapter = new ArrayAdapter<RSSItem>(
                        getApplicationContext(),
                        android.R.layout.simple_list_item_1,
                        myRssFeed.getList());
                setListAdapter(adapter);

                thisAction.setText(myRssFeed.getItem(0).getTitle());

            } else {

                TextView textEmpty = (TextView) findViewById(android.R.id.empty);
                textEmpty.setText("No Feed Found!");
            }

            super.onPostExecute(result);
        }

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        // Uri feedUri = Uri.parse(myRssFeed.getItem(position).getLink());
        // Intent myIntent = new Intent(Intent.ACTION_VIEW, feedUri);
        // startActivity(myIntent);
        String url = myRssFeed.getItem(position).getLink();
        currentItem = myRssFeed.getItem(position).getTitle();
        currentLocation = position;
        thisAction.setText(currentItem);
        webView.loadUrl(url);
    }

    public void sendEmail(View v) {
        // The following code is the implementation of Email client
        Intent i = new Intent(android.content.Intent.ACTION_SEND);
        i.setType("text/plain");
        String[] address = { "bevins2012@hotmail.com" };

        i.putExtra(android.content.Intent.EXTRA_EMAIL, address);
        i.putExtra(android.content.Intent.EXTRA_SUBJECT, myRssFeed.getItem(currentLocation).getDescription());
        i.putExtra(android.content.Intent.EXTRA_TEXT,
                "Action Item from DCC app.");
        startActivityForResult((Intent.createChooser(i, "Email")), 1);
    }

    public void onClick(View v) {
        // this switch listens for any and all click actions in the app
        // each case is a button in the menu.
        switch (v.getId()) {
        case R.id.button1:
            startActivity(new Intent(this, AndroidRssReader.class));
            break;
        case R.id.button2:
            startActivity(new Intent(this, MainActivity.class));
            break;
        case R.id.button3:
            startActivity(new Intent(this, MainActivity.class));
            break;
        case R.id.button4:
            startActivity(new Intent(this, EmailMain.class));
            break;
        case R.id.button5:
            startActivity(new Intent(this, CustomizedListView.class));
            break;
        case R.id.button6:
            startActivity(new Intent(this, LaunchActivity.class));
            break;
        case R.id.button7:
            startActivity(new Intent(this, ActionItem.class));
            break;
        case R.id.button8:
            startActivity(new Intent(this, MainActivity.class));
            finish();
            break;
        case R.id.email_button1:
            // Calling sendEmail from the activity class
            sendEmail(v);
            break;
        }
    }
}

參考解法

方法 1:

Perhaps the auto‑generated R class is messed up. Try to clean and rebuild your project by using Project > Clean... in Eclipse.

(by Sam BevinsRidcully)

參考文件

  1. android.webkit.WebView cannot be cast to android.widget.Button (CC BY‑SA 3.0/4.0)

#android-webview #java #casting #Android #button






相關問題

如何在我創建的 Android WebView 中播放動態 mp4 視頻 (How do I play dynamic mp4 video within Android WebView I created)

Phonegap 應用程序不滾動 (Phonegap Application Not Scrolling)

Android - 將本地圖像設置為 WebView 中的 img 元素 (Android - Set a Local Image to an img element in WebView)

使用 webView 的 loadDataWithBaseURL 不從 sdcard 加載圖像 (Using webView's loadDataWithBaseURL not loading images from sdcard)

WebView LoadURL dan GetView (WebView LoadURL and GetView)

Android WebView 在 android 4.0 + 上崩潰應用程序 (Android WebView crashes application on android 4.0 +)

Android: Chế độ xem hình ảnh chụm-thu phóng với Chế độ xem con được nối thêm (Android: Pinch-zooming ImageView with appended child Views)

android.webkit.WebView 無法轉換為 android.widget.Button (android.webkit.WebView cannot be cast to android.widget.Button)

使用 android studio 在 webview 中顯示谷歌表單 (display google forms in webview using android studio)

如何解決使用 WebView 時出現“無法打開數據庫文件”的錯誤? (How to solve error "unable to open database file" when using WebView?)

android webview 顯示其字體的默認字體系列是什麼? (What is the default font family taken by android webview to show its fonts?)

Android Studio - 視頻音量低 (Android Studio - Video Volume Low)







留言討論