java - android.webkit.WebView 无法转换为 android.widget.Button

标签 java android button casting android-webview

我正在我的应用程序内部开发 GUI。我最初对它的看法很糟糕,它只包含了测试我的 java 函数的所有基本内容。在我启动并运行所有函数后,我使用 Eclipse 中的图形编辑器重新设计了 GUI。下次运行我的应用程序时,我的 LogCat 中的 android.webkit.WebView 无法转换为 android.widget.Button。我没有更改 java 中的任何内容,并且据我所知,GUI 中的所有声明都是相同的。我可以轻松地将 GUI 恢复到蹩脚版本,但这对我没有多大帮助。我希望这里有人能帮助我找到我的问题。我已经发布了 xml、java 和 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;
        }
    }
}

最佳答案

也许自动生成的 R 类被搞乱了。尝试使用 Eclipse 中的“项目”>“清理...”来清理并重建项目。

关于java - android.webkit.WebView 无法转换为 android.widget.Button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16467692/

相关文章:

Android:如何将ListView中的文本大小更改为变大?

java - 非数据库应用事务

java - 如何将自定义类型作为可调用语句中的输入参数

android - 在 Android 中屏幕锁定时启动 NFC

Android - XML 按钮丢失了吗?

javascript - 未检测到数组中值的更改

java - Android 中的按钮不会禁用

java - Cucumber-jvm功能文件和步骤定义文件实现问题

java - s.nextLine() 在 do 循环中被跳过

android - 当 DrawerLayout 可见时不要调暗主要内容?