I have five layouts(XML files), each one with a Button
on it and two activities, A & B. In the onClick
action of start Button in A, I'll open the activity B and all the story begins there:
In my activity B, first, I want to show one of these five xml files and onclick
of the Button
IN each of the five XML files, I want to randomly open the remaining 4 xml files, until Back button is pressed.. Is there any way I could do this or I must have 5 activities each with one layout?
My problem is:
Just one Xml file is appearing randomly on activity B and the Button
on that xml file is not responding, i.e, not showing next xml file.
Here is the code for my activity 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);
}
});
}
And all the 5 Xml Files Will go in the same paTTERN:
<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" />
I am new to Java, So Stucked Here, If any simple clue also will help me.I have searched this forum And found no relevant solution.
@LuksProg, Logcat Error is Like this:
**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)**
This time I want to do this,
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;
}