随机XML布局文件的活动 [英] Random xml layouts files for an Activity

查看:137
本文介绍了随机XML布局文件的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有五个布局(XML文件),每一个都带有一个按钮它和两个活动,急症室; B.在开始按钮的的onClick 在一个动作,我会打开活动B和所有的故事开始出现:

在我的活动B,首先,我想说明这五个XML文件的一个按钮的的onclick 在全部五个XML文件,我想随意打开剩下的4 XML文件,直到返回按钮pssed $ P $。。有没有什么办法,我可以做到这一点,或我必须在每个5的活动有一个布局?

我的问题是:

只是一个XML文件中随机出现的活动B和按钮上的XML文件上没有响应,即不显示下一个XML文件。

下面是code我的活动B:

 公共类B扩展活动{
处理程序处理程序=新的处理程序();
按钮胜;

INT []布局= {R.layout.first,R.layout.second,R.layout.third,R.layout.fourth,R.layout.fifth};
@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);


    BT =(按钮)findViewById(R.id.button1);

    bt.setOnClickListener(新OnClickListener(){

        @覆盖
        公共无效的onClick(查看为arg0){
            // TODO自动生成方法存根

            handler.postDelayed(新的Runnable(){

                @覆盖
                公共无效的run(){
                    // TODO自动生成方法存根

                    的for(int i = 0; I< layouts.length;我++){


                        的setContentView(布局[兰特]);
                    }

                }
            },2000);

        }
    });


}
 

和所有的5 XML文件将走在相同的模式:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
工具:上下文=MainActivity。>

<的TextView
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:重力=中心
    机器人:文本=@字符串/参考hello world/>

<按钮
    机器人:ID =@ + ID / button5
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_gravity =中心
    机器人:layout_marginTop =10dp
    机器人:的onClick =showLayouts
    机器人:文本=5/>
 

我是新来的Java,所以Stucked这里,如果有任何简单的线索,也将帮助我,我搜索这个论坛,发现没有相关的解决方案。

@LuksProg,logcat的错误是这样的:

  ** 5月3日至11号:51:16.147:E / AndroidRuntime(1575):致命异常:主要
 五月三日至11日:51:16.147:E / AndroidRuntime(1575):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.SAI.wth / com.SAI.wth.ReceivingActivity}:显示java.lang.NullPointerException
 五月三日至11日:51:16.147:E / AndroidRuntime(1575):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
 五月三日至11日:51:16.147:E / AndroidRuntime(1575):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
 五月三日至11日:51:16.147:E / AndroidRuntime(1575):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)
 五月三日至11日:51:16.147:E / AndroidRuntime(1575):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)
 五月三日至11日:51:16.147:E / AndroidRuntime(1575):在android.os.Handler.dispatchMessage(Handler.java:99)
 五月三日至11日:51:16.147:E / AndroidRuntime(1575):在android.os.Looper.loop(Looper.java:137)
 五月三日至11日:51:16.147:E / AndroidRuntime(1575):在android.app.ActivityThread.main(ActivityThread.java:5039)
 五月三日至11日:51:16.147:E / AndroidRuntime(1575):在java.lang.reflect.Method.invokeNative(本机方法)
 五月三日至11日:51:16.147:E / AndroidRuntime(1575):在java.lang.reflect.Method.invoke(Method.java:511)
 五月三日至11日:51:16.147:E / AndroidRuntime(1575):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
 五月三日至11日:51:16.147:E / AndroidRuntime(1575):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
 五月三日至11日:51:16.147:E / AndroidRuntime(1575):在dalvik.system.NativeStart.main(本机方法)
 五月三日至11日:51:16.147:E / AndroidRuntime(1575):由:显示java.lang.NullPointerException
 五月三日至11日:51:16.147:E / AndroidRuntime(1575):在com.SAI.wth.ReceivingActivity.onCreate(ReceivingActivity.java:40)
 五月三日至11日:51:16.147:E / AndroidRuntime(1575):在android.app.Activity.performCreate(Activity.java:5104)
 五月三日至11日:51:16.147:E / AndroidRuntime(1575):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
 五月三日至11日:51:16.147:E / AndroidRuntime(1575):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)**
 

这一次,我想这样做,

 开关(mPosition){
                            案例R.layout.first:
                                Toast.makeText(getApplicationContext(),第一,Toast.LENGTH_SHORT).show();

                                打破;

                            案例R.layout.second:
                                Toast.makeText(getApplicationContext(),第二,Toast.LENGTH_SHORT).show();
//一些声音
                                打破;

                            案例R.layout.third:
                                Toast.makeText(getApplicationContext(),第三,Toast.LENGTH_SHORT).show();
    //一些媒体,不同的为每个布局..
                                打破;

                            案例R.layout.fourth:
                                Toast.makeText(getApplicationContext(),第四,Toast.LENGTH_SHORT).show();

                                打破;

                            案例R.layout.fifth:
                                Toast.makeText(getApplicationContext(),十五,Toast.LENGTH_SHORT).show();

                                打破;
                            默认:
                                打破;
                            }


                            Collections.shuffle(mLayouts);
                        }
                        mPosition = 0;
                    }
 

解决方案
  

只是一个XML文件中是一个出现在随机的活动B和;上的按钮   该XML文件没有响应,即不显示下一个XML文件中。

您会想是这样的:

 私人INT mPosition = 0;
私人处理程序mHandler =新的处理程序();
私人列表<整数GT; mLayouts;
私人按钮mButton;

私人OnClickListener mListener =新OnClickListener(){

    @覆盖
    公共无效的onClick(视图v){
        mHandler.postDelayed(新的Runnable(){

            @覆盖
            公共无效的run(){
                //为您发布此延迟可能要确保
                //用户没有再次点击此布顿在两秒的时间间隔
                如果(mPosition> = mLayouts.size()){//恢复列表
                    INT lastId = mLayouts.get(mLayouts.size() -  1);
                    Collections.shuffle(mLayouts);
                    //做到这一点,所以如果我们随机化布局,我们不结束
                    与在相同的布局洗牌后//起来
                    // 第一
                    //位置的最后位置
                    而(lastId == mLayouts.get(0)){
                        Collections.shuffle(mLayouts);
                    }
                    mPosition = 0;
                }
                的setContentView(mLayouts.get(mPosition));
                mPosition ++;
                mButton =(按钮)findViewById(R.id.btnId);
                mButton.setOnClickListener(mListener);
            }

        },2000);
    }

};
 

的onCreate 方法:

  mLayouts =新的ArrayList<整数GT;();
    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 =(按钮)findViewById(R.id.btnId);
    mButton.setOnClickListener(mListener);
    mPosition ++;
 

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;
                    }   

解决方案

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++;

这篇关于随机XML布局文件的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆