ArrayIndexOutOfBoundsException在Android中启动活动时发生
我刚刚开始学习Android,我正在尝试一个简单的应用程序,只显示一个按钮,当点击按钮时,会显示一个弹出窗口 下面是我的模块ArrayIndexOutOfBoundsException在Android中启动活动时发生,android,android-intent,Android,Android Intent,我刚刚开始学习Android,我正在尝试一个简单的应用程序,只显示一个按钮,当点击按钮时,会显示一个弹出窗口 下面是我的模块 strings.xml <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Click me!</string> <string name="button1">Click me!</string
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>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="background">#045FB4</color>
</resources>
<?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>
<?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>
<?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>
popup.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>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="background">#045FB4</color>
</resources>
<?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>
<?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>
<?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>
AndroidManifest.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>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="background">#045FB4</color>
</resources>
<?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>
<?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>
<?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>
popup.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;
}
}
}
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);
}
}
单击按钮时,我得到一个数组索引OutofBoundsException
。我不太明白我错在哪里。请指出它
谢谢。从xml文件获取按钮时,将视图强制转换为按钮。使用以下命令,
按钮clickmeButton=(按钮)findViewById(R.id.clickme\u按钮)
而不是
查看clickmeButton=findViewById(R.id.clickme\u按钮)
你的意思是什么。是否要显示一个对话框?因为您的代码只会启动另一个活动。要获得弹出窗口,请使用对话框。在此处阅读关于AlertDialog:。从xml文件获取按钮时,将视图强制转换为按钮。使用以下命令,
按钮clickmeButton=(按钮)findViewById(R.id.clickme\u按钮)
而不是
查看clickmeButton=findViewById(R.id.clickme\u按钮) 你的意思是什么。是否要显示一个对话框?因为您的代码只会启动另一个活动。要获得弹出窗口,请使用对话框。在此处阅读有关AlertDialog的信息: 弹出式活动不需要主题
弹出式活动不需要主题。您可以从logcat发布stacktrace吗?按钮clickmeButton=(按钮)findViewById(R.id.clickme_按钮);您可以从logcat发布stacktrace吗?按钮clickmeButton=(按钮)findViewById(R.id.clickme_按钮);我真的很好奇。您能否进一步澄清ArrayIndexOutOfBoundsException发生在何处,以及指定视图实现如何修复它?谢谢@b必须首先像clickmeButton一样设置Id。设置Id(1);另外,在popup.xml中,为textview提供布局宽度和布局高度参数。我真的很好奇这一点。您能否进一步澄清ArrayIndexOutOfBoundsException发生在何处,以及指定视图实现如何修复它?谢谢@b必须首先像clickmeButton一样设置Id。设置Id(1);同样在popup.xml中,为textview提供布局宽度和布局高度参数。