我有五个布局(XML文件),每个布局都有一个Button
和两个活动,A& B.在A的开始按钮的onClick
动作中,我打开活动B,所有故事从那里开始:
在我的活动B中,首先,我想在五个XML文件中的每个文件中显示onclick
的这五个xml文件中的一个和Button
,我想随机打开剩余的4个xml文件,直到按下“返回”按钮。。有什么方法可以做到这一点,或者我必须有5个活动,每个活动有一个布局?
我的问题是:
活动B上只有一个Xml文件随机出现,该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;
}
答案 0 :(得分:2)
当我这样做时似乎工作得很好:
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);
}
}
}
答案 1 :(得分:1)
只有一个Xml文件在Activity B&amp;中随机出现按钮打开 该xml文件没有响应,即没有显示下一个xml文件。
你会想要这样的东西:
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);
}
};
并在onCreate
方法中:
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++;