Reputation: 558
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;
}
Upvotes: 0
Views: 1169
Reputation: 87064
just one Xml file is Appearing in random on Activity B & the button on that xml file is not responding, i.e, not showing next xml file.
You'll want something like this:
private int mPosition = 0;
private Handler mHandler = new Handler();
private List<Integer> mLayouts;
private Button mButton;
private OnClickListener mListener = new OnClickListener() {
@Override
public void onClick(View v) {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
// as you're posting this delayed you may want to make sure the
// user doesn't click this Buton again in the two seconds interval
if (mPosition >= mLayouts.size()) { // revert the list
int lastId = mLayouts.get(mLayouts.size() - 1);
Collections.shuffle(mLayouts);
// do this so if we randomize the layouts we don't end
// up after the shuffle with the same layout on the
// first
// position as the last position
while (lastId == mLayouts.get(0)) {
Collections.shuffle(mLayouts);
}
mPosition = 0;
}
setContentView(mLayouts.get(mPosition));
mPosition++;
mButton = (Button) findViewById(R.id.btnId);
mButton.setOnClickListener(mListener);
}
}, 2000);
}
};
and in the onCreate
method:
mLayouts = new ArrayList<Integer>();
mLayouts.add(R.layout.layout1);
mLayouts.add(R.layout.layout2);
mLayouts.add(R.layout.layout3);
mLayouts.add(R.layout.layout4);
mLayouts.add(R.layout.layout5);
Collections.shuffle(mLayouts);
setContentView(mLayouts.get(mPosition));
mButton = (Button) findViewById(R.id.btnId);
mButton.setOnClickListener(mListener);
mPosition++;
Upvotes: 1
Reputation: 14000
Seems to work just fine when I do this:
public class MainActivity extends Activity implements OnClickListener {
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.first);
bt = (Button) findViewById(R.id.button5);
bt.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.button5){
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
int rand = (int) Math.floor(Math.random() * layouts.length);
setContentView(layouts[rand]);
bt = (Button) findViewById(R.id.button5);
bt.setOnClickListener(MainActivity.this);
}
}, 2000);
}
}
}
Upvotes: 2