Note that there are some explanatory texts on larger screens.

plurals
  1. POandroid.webkit.WebView cannot be cast to android.widget.Button
    text
    copied!<p>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.</p> <pre><code>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 &lt;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" &gt; &lt;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" &gt; &lt;Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/Button1_name" android:drawableRight="@drawable/news"/&gt; &lt;Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/Button2_name" /&gt; &lt;Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/Button3_name" android:drawableRight="@drawable/cal"/&gt; &lt;Button android:id="@+id/button4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/Button4_name" android:drawableRight="@drawable/mail"/&gt; &lt;Button android:id="@+id/button5" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/Button5_name" android:drawableRight="@drawable/gallery" /&gt; &lt;Button android:id="@+id/button6" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/Button6_name" android:drawableRight="@drawable/report" /&gt; &lt;Button android:id="@+id/button7" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/Button7_name" android:drawableRight="@drawable/ep" /&gt; &lt;Button android:id="@+id/button8" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/Button8_name" android:drawableRight="@drawable/directory" /&gt; &lt;/LinearLayout&gt; &lt;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" &gt; &lt;/GridLayout&gt; &lt;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" /&gt;   &lt;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" &gt; &lt;LinearLayout android:layout_width="fill_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/feedtitle" android:layout_width="fill_parent" android:layout_height="wrap_content" /&gt; &lt;TextView android:id="@+id/feeddescribtion" android:layout_width="fill_parent" android:layout_height="wrap_content" /&gt; &lt;TextView android:id="@+id/feedpubdate" android:layout_width="fill_parent" android:layout_height="wrap_content" /&gt; &lt;TextView android:id="@+id/feedlink" android:layout_width="fill_parent" android:layout_height="wrap_content" android:autoLink="web" /&gt; &lt;ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="594dp" &gt; &lt;/ListView&gt; &lt;TextView android:id="@android:id/empty" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="waiting" /&gt; &lt;/LinearLayout&gt; &lt;/ScrollView&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;/GridLayout&gt; 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&lt;Void, Void, Void&gt; { @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&lt;RSSItem&gt; adapter = new ArrayAdapter&lt;RSSItem&gt;( 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; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload