我有五個佈局(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