android onclick显示ui元素

android onclick显示ui元素,android,image,onclick,Android,Image,Onclick,在我的应用程序中,我滑动图像,我需要找到一种方法,在单击图像时如何显示一些按钮和箭头。我使用以下代码: Edit1: package com.stampii.stampii.cards; import com.stampii.stampii.cards.HorizontalPager; import com.stampii.stampii.cards.HorizontalPager.OnScreenSwitchListener; import com.stampii.stampii.R; im

在我的应用程序中,我滑动图像,我需要找到一种方法,在单击图像时如何显示一些按钮和箭头。我使用以下代码:

Edit1:

package com.stampii.stampii.cards;

import com.stampii.stampii.cards.HorizontalPager;
import com.stampii.stampii.cards.HorizontalPager.OnScreenSwitchListener;
import com.stampii.stampii.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class Cards extends Activity {
    public int position;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        HorizontalPager realViewSwitcher = new HorizontalPager(getApplicationContext());

        ImageView img1 = new ImageView(getApplicationContext());
        ImageView img2 = new ImageView(getApplicationContext());
        ImageView img3 = new ImageView(getApplicationContext());
        ImageView img4 = new ImageView(getApplicationContext());
        ImageView img5 = new ImageView(getApplicationContext());
        ImageView img6 = new ImageView(getApplicationContext());

        img1.setImageResource(R.drawable.one);
        img2.setImageResource(R.drawable.two);
        img3.setImageResource(R.drawable.three);
        img4.setImageResource(R.drawable.four);
        img5.setImageResource(R.drawable.five);
        img6.setImageResource(R.drawable.six);

        realViewSwitcher.addView(img1);
        realViewSwitcher.addView(img2);
        realViewSwitcher.addView(img3);
        realViewSwitcher.addView(img4);
        realViewSwitcher.addView(img5);
        realViewSwitcher.addView(img6);

        realViewSwitcher.setOnScreenSwitchListener(new OnScreenSwitchListener(){
            public void onScreenSwitched(int screen){
                    position = screen;
            }
            });
            LinearLayout ll1 = (LinearLayout) findViewById(R.id.ll1);
            LinearLayout ll2 = (LinearLayout) findViewById(R.id.ll2);
            //add realViewSwitcher to ll1
            ll1.addView(realViewSwitcher);

            //Add buttons to ll2 and edit them as u want
            setContentView(R.layout.cards);

    }

}
我需要这样做:。要显示下一个、上一个箭头按钮,请单击,并在图像顶部显示两个按钮

LogCat异常:

08-09 14:33:54.656: ERROR/AndroidRuntime(1562): Uncaught handler: thread main exiting due to uncaught exception
08-09 14:33:54.705: ERROR/AndroidRuntime(1562): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stampii.stampii/com.stampii.stampii.cards.Cards}: java.lang.NullPointerException
08-09 14:33:54.705: ERROR/AndroidRuntime(1562):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
08-09 14:33:54.705: ERROR/AndroidRuntime(1562):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
08-09 14:33:54.705: ERROR/AndroidRuntime(1562):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
08-09 14:33:54.705: ERROR/AndroidRuntime(1562):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
08-09 14:33:54.705: ERROR/AndroidRuntime(1562):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-09 14:33:54.705: ERROR/AndroidRuntime(1562):     at android.os.Looper.loop(Looper.java:123)
08-09 14:33:54.705: ERROR/AndroidRuntime(1562):     at android.app.ActivityThread.main(ActivityThread.java:4363)
08-09 14:33:54.705: ERROR/AndroidRuntime(1562):     at java.lang.reflect.Method.invokeNative(Native Method)
08-09 14:33:54.705: ERROR/AndroidRuntime(1562):     at java.lang.reflect.Method.invoke(Method.java:521)
08-09 14:33:54.705: ERROR/AndroidRuntime(1562):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-09 14:33:54.705: ERROR/AndroidRuntime(1562):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-09 14:33:54.705: ERROR/AndroidRuntime(1562):     at dalvik.system.NativeStart.main(Native Method)
08-09 14:33:54.705: ERROR/AndroidRuntime(1562): Caused by: java.lang.NullPointerException
08-09 14:33:54.705: ERROR/AndroidRuntime(1562):     at com.stampii.stampii.cards.Cards.onCreate(Cards.java:54)
08-09 14:33:54.705: ERROR/AndroidRuntime(1562):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-09 14:33:54.705: ERROR/AndroidRuntime(1562):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
08-09 14:33:54.705: ERROR/AndroidRuntime(1562):     ... 11 more
main.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/ll1">
    </LinearLayout>
    <LinearLayout
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom"
    android:orientation="horizontal"
    android:paddingTop="8dip"
    android:gravity="center_horizontal"
    android:background="#AA000000"
    android:id="@+id/ll2">
    </LinearLayout>

</merge>
首先定义布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:id="@+id/images_holder"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingBottom="50dip"/>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:orientation="horizontal"
        android:gravity="center_horizontal"
        android:layout_alignParentBottom="true">
        <Button
            android:id="@+id/previous"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:layout_margin="5dip"
            android:text="Previous"/>
        <Button
            android:id="@+id/next"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:layout_margin="5dip"
            android:text="Next"/>
    </LinearLayout>
</RelativeLayout>

这将转到onCreate():


那么我需要在哪里添加这个xml文件,以及当我单击图像时如何调用它?我仍然没有得到它part@Bombastic .. 对不起,我在上班,有点忙。。告诉我你现在在哪里?有什么进展吗?我没有得到的是,我应该在我的类中添加xml作为视图,因为实际上我使用的是:setContentView(realViewSwitcher);以及如何控制图像点击状态。使用setContentView(R.layout.yourlayout),然后将viewSwitcher添加到您的布局中-正如我在上面发布的,我只是用我正在使用的代码和LogCat中的异常编辑问题。谢谢帮助!实际上它不工作。它给我一个异常:08-09 11:15:35.316:ERROR/AndroidRuntime(1116):java.lang.RuntimeException:无法启动活动组件信息{com.stampii.stampii/com.stampii.stampii.cards.cards}:android.util.AndroidRuntimeException:requestFeature()必须在添加内容之前调用抱歉,但您必须提供更详细的例外情况
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:id="@+id/images_holder"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingBottom="50dip"/>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:orientation="horizontal"
        android:gravity="center_horizontal"
        android:layout_alignParentBottom="true">
        <Button
            android:id="@+id/previous"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:layout_margin="5dip"
            android:text="Previous"/>
        <Button
            android:id="@+id/next"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:layout_margin="5dip"
            android:text="Next"/>
    </LinearLayout>
</RelativeLayout>
HorizontalPager realViewSwitcher;
setContentView(R.layout.main);

    realViewSwitcher = new HorizontalPager(getApplicationContext());

    ImageView img1 = new ImageView(getApplicationContext());
    ImageView img2 = new ImageView(getApplicationContext());
    ImageView img3 = new ImageView(getApplicationContext());

    img1.setImageResource(R.drawable.one);
    img2.setImageResource(R.drawable.two);
    img3.setImageResource(R.drawable.three);

    realViewSwitcher.addView(img1);
    realViewSwitcher.addView(img2);
    realViewSwitcher.addView(img3);

    //Add viewSwitcher to the layout
    ((LinearLayout)findViewById(R.id.images_holder)).addView(realViewSwitcher);

    //Initialise Next & Previous buttons
    Button previous = (Button)findViewById(R.id.previous);
    Button next = (Button)findViewById(R.id.next);

    //Add listeners to those buttons
    previous.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            realViewSwitcher.showPrevious();
        }
    });

    next.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            realViewSwitcher.showNext();
        }
    });