Android OnClick показывает элементы пользовательского интерфейса - PullRequest
0 голосов
/ 09 августа 2011

В моем приложении я смахиваю изображения, и мне нужно найти способ показать некоторые кнопки и стрелки, когда я нажимаю на изображение. Я использую этот код:

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

    }

}

И мне нужно сделать что-то вроде этого: http://www.photoswipe.com/latest/examples/jquery-mobile.html. Для отображения следующей, предыдущей кнопки со стрелкой и двух кнопок в верхней части изображения.

Исключение 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

Ответы [ 2 ]

1 голос
/ 09 августа 2011

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>

public int position;

public void onCreate(){

    HorizontalPager 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);

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

    //Add buttons to ll2 and edit them as you want
    setContentView(R.layout.main);
}
0 голосов
/ 09 августа 2011

Сначала определите макет:

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

это идет к onCreate ():

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();
        }
    });
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...