我刚开始学习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
。我无法弄清楚我哪里出错了。请指出
感谢。
答案 0 :(得分:1)
从xml文件获取按钮时,将视图转换为Button。使用以下,
按钮clickmeButton =(Button)findViewById(R.id.clickme_button);
而不是
查看clickmeButton = findViewById(R.id.clickme_button);
通过popup你是什么意思。你想要显示对话吗?因为您的代码只会启动另一项活动。要获得弹出窗口,请使用Dialogs。在这里阅读AlertDialog:http://developer.android.com/guide/topics/ui/dialogs.html。
答案 1 :(得分:-1)
Button clickmeButton = (Button) findViewById(R.id.clickme_button);
不需要PopUP活动的主题。