gpt4 book ai didi

android - 在 Android 中启动 Activity 时出现 ArrayIndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-11-29 14:07:16 25 4
gpt4 key购买 nike

我刚刚开始学习 Android,我正在尝试一个简单的应用程序来显示一个按钮,单击该按钮会显示一个弹出窗口。

下面是我的模块

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" android: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);
}

}

单击按钮时,我得到一个 ArrayIndexOutOfBoundsException。我不太清楚我哪里出错了。请指点一下

谢谢。

最佳答案

从 xml 文件中获取按钮时,将 View 转换到 Button。使用以下内容,
按钮 clickmeButton = (Button) findViewById(R.id.clickme_button);
代替
查看clickmeButton = findViewById(R.id.clickme_button);

弹出窗口是什么意思。你想显示一个对话框吗?因为您的代码只会启动另一个 Activity 。要获得弹出窗口,请使用对话框。在此处阅读有关 AlertDialog 的信息:http://developer.android.com/guide/topics/ui/dialogs.html .

关于android - 在 Android 中启动 Activity 时出现 ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6048527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com