Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

Monday, November 21, 2011

java.lang.ClassCastException: android.widget.Button problem

 

 

While trying to create a clickable ImageView, I received this error:

 

11-21 15:35:15.940: E/AndroidRuntime(7607): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mctexpert.android.ishunt/com.mctexpert.android.ishunt.MenuScreen}: java.lang.ClassCastException: android.widget.Button

 

The problem was it thought that I was trying to work with a button as opposed to the image.  I ended up deleting the ImageView from the layout and then recreating it.  That took care of this problem.

Thursday, November 10, 2011

Lock a screen in portrait mode without losing data

 

As I continue to progress in my Android education, I’m finding out how to make my apps work the way I want them to do.  In this case, I needed to prevent the screen from rotating.  I also forgot that Android calls the onCreatemethod every time you do this.  Here is how I fixed that,

 

I opened the AndroidManifest.xml file.  I added two lines of code:

android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">

The first tells Android to keep the screen in portrait mode. 

The second tells Android that in the event the device is rotated or the keyboard is pulled out, do not execute the onCreate method.  Instead, keep going with what you currently have open. 

Below is how the code in my AndroidManifest XML file looks.  Notice that these elements are called on a per activity bases.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="
http://schemas.android.com/apk/res/android"
    package="com.mctexpert.android.orientation"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".OrientationActivity"
            android:screenOrientation="portrait"
            android:configChanges="keyboardHidden|orientation">
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Saturday, October 29, 2011

Displaying an integer in a TextView on Android

This one took me for a ride for a few hours.  I went all over the internet with little help.  Here is the problem.  I have a layout created with TextView, but I want to display an integer value in that field. 

 

I declare my integer as:

int score = 1;

I also have a string value called scoreText.

I have a TextView with an ID of score.  Here is how I display the integer in the TextView

TextView text = (TextView)findViewById(R.id.score);

text.setText(scoreText.valueOf(score));

Setting up a button click on Android

In this example, I have a button in my layout XML file that I called bulb1_button.  I want to set up a click event for it.  Below is my code to do this inside the OnCreate method.

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
      // Button 1 clicked event. 
      final Button button1 = (Button)findViewById(R.id.bulb1_button);
      button1.setOnClickListener(new View.OnClickListener()
      {
       
        @Override
        public void onClick(View v)
        {
            // TODO: Actions to take when the button is clicked.           
        }
      });
      // End of Button 1 clicked event.

 

We first define a variable the will hold the button.  I call this one button1 which is of type Button.  Next I set up an event listener to handle the click.  Inside the listener, I create the code to handle the click and place inside of it what happens when the button is clicked.