android.webkit.WebView无法强制转换为android.widget.Button

时间:2013-05-09 17:33:55

标签: java android button casting android-webview

我正在使用我的应用内部的GUI。我原本看起来很糟糕,它只包含了测试我的java函数的所有基本内容。在我完成所有函数运行之后,我使用Eclipse中的图形编辑器重新编写了GUI。下次我运行我的应用程序时,我的android.webkit.WebView无法在我的LogCat中强制转换为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;
        }
    }
}

1 个答案:

答案 0 :(得分:9)

也许自动生成的R类搞砸了。尝试使用Project&gt;清理和重建项目。在Eclipse中清理......