开发者

ArrayIndexOutOfBoundsException whie starting an activity in Android

开发者 https://www.devze.com 2023-03-06 16:10 出处:网络
I have just started studying Android and I am trying out a simple application to just display a button which when clicked, to show a popup.

I have just started studying Android and I am trying out a simple application to just display a button which when clicked, to show a popup.

My modules below

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Click me!</string>
    <string name="button1">Click me!</string>
    <string name="popup_text">This is a text</string>
    <string name="popup_title">Hi...</string>
</resources>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="background">#045FB4</color>
</resources>

popup.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <TextView
    android:text="@string/popup_text"
    />
</ScrollView>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="@color/background"
    android:padding="30dip">
    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="wrap_content">
        <Button android:text="@string/button1" android:id="@+id/clickme_button"
            android:layout_width="wrap_content" android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.googlecode.cowbullgame" android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name"
        android:debuggable="true">
        <activity android:name=".ButtonTest" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Popup" an开发者_JAVA百科droid:label="@string/popup_title"
            android:theme="@android:style/Theme.Dialog">
        </activity>

    </application>
</manifest>

ButtonTest.java

package com.googlecode.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class ButtonTest extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        View clickmeButton = findViewById(R.id.clickme_button);
        clickmeButton.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch(v.getId()) {
        case R.id.clickme_button:
            startActivity(new Intent(this, Popup.class));
            break;
        }
    }
}

popup.java

package com.googlecode.test;

import android.app.Activity;
import android.os.Bundle;

public class Popup extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popup);
    }

}

While clicking the button, I get an ArrayIndexOutOfBoundsException. I couldn't quite figure out where I am going wrong. Please point it

Thanks.


When getting the button from the xml file, cast the view to Button. Use the following,
Button clickmeButton = (Button) findViewById(R.id.clickme_button);
instead of
View clickmeButton = findViewById(R.id.clickme_button);

And by popup what do you mean. Do you want to show a Dialog? Because your code will just start another activity. to get a popup, use Dialogs. Read about AlertDialog here: http://developer.android.com/guide/topics/ui/dialogs.html .


Button clickmeButton = (Button) findViewById(R.id.clickme_button);

no need of theme for PopUP activity.

0

精彩评论

暂无评论...
验证码 换一张
取 消