Results 1 to 5 of 5
  1. #1
    KarneeKarnay is offline Member
    Join Date
    Apr 2014
    Posts
    5
    Rep Power
    0

    Default Edit Text Problem

    Hi guys, I'm trying to write a simple task, of taking the user's input when a button is clicked. For some reason I am getting this error when I try to.

    04-27 04:50:17.763: E/AndroidRuntime(912): FATAL EXCEPTION: main
    04-27 04:50:17.763: E/AndroidRuntime(912): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.KarneeKarnay.gamefinder/com.KarneeKarnay.gamefinder.MainActivity}: java.lang.ClassCastException: android.widget.EditText cannot be cast to android.widget.Button
    My code:

    Java Code:
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    
    import com.KarneeKarnay.gamefinder.UrlBuilder;
    
    public class MainActivity extends Activity {
    
       
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            final UrlBuilder url = new UrlBuilder();
            
            final Button button = (Button) findViewById(R.id.button1);
            final EditText searchQuery = (EditText) findViewById(R.id.searchBar);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // Perform action on click
                	String temp = searchQuery.getText().toString();
                	url.setSearch(temp);
                }
            });
            
           
        }
    
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
        
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Edit Text Problem

    java.lang.ClassCastException: android.widget.EditText cannot be cast to android.widget.Button
    Is that from this line:
    final Button button = (Button) findViewById(R.id.button1);

    Is button1 an EditText?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    KarneeKarnay is offline Member
    Join Date
    Apr 2014
    Posts
    5
    Rep Power
    0

    Default Re: Edit Text Problem

    Quote Originally Posted by Norm View Post
    Is that from this line:
    final Button button = (Button) findViewById(R.id.button1);

    Is button1 an EditText?
    I think so. button1 is just a button. What I want is that if someone clicks on button1 then the contents of searchQuery (The Edit Text field), is placed in a string variable called temp. For some reason that error appears when I do so.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Edit Text Problem

    what's in activity_main.xml?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    KarneeKarnay is offline Member
    Join Date
    Apr 2014
    Posts
    5
    Rep Power
    0

    Default Re: Edit Text Problem

    Quote Originally Posted by Norm View Post
    what's in activity_main.xml?
    Here it is.


    Java Code:
     <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
        
        <EditText
            android:id="@+id/searchBar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/button1"
            android:layout_toLeftOf="@+id/button1"
            android:hint="@string/SearchBar" />
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="@string/SearchButton" />
    
    </RelativeLayout>

Similar Threads

  1. Read, Edit, and Print text file
    By maddmike71 in forum New To Java
    Replies: 0
    Last Post: 02-23-2011, 02:18 AM
  2. Edit text in a program
    By roud9 in forum New To Java
    Replies: 2
    Last Post: 12-03-2010, 02:38 AM
  3. Read/edit/Write text file
    By Graupner1000 in forum New To Java
    Replies: 17
    Last Post: 08-25-2010, 03:04 PM
  4. Read and edit text file
    By VinTiger in forum New To Java
    Replies: 5
    Last Post: 05-14-2009, 02:18 AM
  5. Replies: 5
    Last Post: 08-11-2007, 04:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •