我有五个布局(XML文件),每个布局有一个Button
和两个活动,A & B.在A的开始按钮的onClick
操作中,我将打开活动B和所有的故事开始出现:活动的随机xml布局文件
在我的活动B,首先,我想说明这五个XML文件和Button
在每五个XML文件的onclick
之一,我想随意打开剩下的4个xml文件,直到按下返回按钮。。有什么办法可以做到这一点,或者我必须有5个活动,每个有一个布局?
我的问题是:
只是一个XML文件上的活动者B随机出现,并在该XML文件Button
没有响应,即,没有出现下一个XML文件。
这里是我的活动B中的代码:
public class B extends Activity {
Handler handler = new Handler();
Button bt;
int[] layouts = {R.layout.first,R.layout.second,R.layout.third,R.layout.fourth,R.layout.fifth};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt = (Button)findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < layouts.length; i++) {
setContentView(layouts[rand]);
}
}
}, 2000);
}
});
}
而且所有的5 XML文件将走在相同的模式:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/hello_world" />
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:onClick="showLayouts"
android:text="5th" />
我是新来的Java如此简单的线索也会帮助我。我搜索了这个论坛并没有找到相关的解决方案。
@LuksProg,logcat的错误是这样的:
**03-11 05:51:16.147: E/AndroidRuntime(1575): FATAL EXCEPTION: main
03-11 05:51:16.147: E/AndroidRuntime(1575): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.SAI.wth/com.SAI.wth.ReceivingActivity}: java.lang.NullPointerException
03-11 05:51:16.147: E/AndroidRuntime(1575): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-11 05:51:16.147: E/AndroidRuntime(1575): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-11 05:51:16.147: E/AndroidRuntime(1575): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-11 05:51:16.147: E/AndroidRuntime(1575): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-11 05:51:16.147: E/AndroidRuntime(1575): at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 05:51:16.147: E/AndroidRuntime(1575): at android.os.Looper.loop(Looper.java:137)
03-11 05:51:16.147: E/AndroidRuntime(1575): at android.app.ActivityThread.main(ActivityThread.java:5039)
03-11 05:51:16.147: E/AndroidRuntime(1575): at java.lang.reflect.Method.invokeNative(Native Method)
03-11 05:51:16.147: E/AndroidRuntime(1575): at java.lang.reflect.Method.invoke(Method.java:511)
03-11 05:51:16.147: E/AndroidRuntime(1575): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-11 05:51:16.147: E/AndroidRuntime(1575): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-11 05:51:16.147: E/AndroidRuntime(1575): at dalvik.system.NativeStart.main(Native Method)
03-11 05:51:16.147: E/AndroidRuntime(1575): Caused by: java.lang.NullPointerException
03-11 05:51:16.147: E/AndroidRuntime(1575): at com.SAI.wth.ReceivingActivity.onCreate(ReceivingActivity.java:40)
03-11 05:51:16.147: E/AndroidRuntime(1575): at android.app.Activity.performCreate(Activity.java:5104)
03-11 05:51:16.147: E/AndroidRuntime(1575): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-11 05:51:16.147: E/AndroidRuntime(1575): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)**
这一次,我想这样做,
switch (mPosition) {
case R.layout.first:
Toast.makeText(getApplicationContext(), "First", Toast.LENGTH_SHORT).show();
break;
case R.layout.second:
Toast.makeText(getApplicationContext(), "second", Toast.LENGTH_SHORT).show();
//Some sound
break;
case R.layout.third:
Toast.makeText(getApplicationContext(), "third", Toast.LENGTH_SHORT).show();
//Some media , Different for each Layout..
break;
case R.layout.fourth:
Toast.makeText(getApplicationContext(), "fourth", Toast.LENGTH_SHORT).show();
break;
case R.layout.fifth:
Toast.makeText(getApplicationContext(), "fifth", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
Collections.shuffle(mLayouts);
}
mPosition = 0;
}
那么当你运行这段代码时会发生什么? – Catherine 2013-03-09 07:57:00
只有一个Xml文件随机出现,该xml文件上的按钮没有响应,即没有显示下一个xml文件。 – sai 2013-03-09 07:59:45