Hi,
Iam new to android and I really need help with accessing the xml
layout "objects" from .java. I have this code:

package cz.aurem.ot;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.widget.TextView;
import android.widget.ToggleButton;
import cz.aurem.ot.R;

public class ovladacOT extends Activity implements OnGestureListener {
    /** Called when the activity is first created. */
    private TextView viewA;
    public ToggleButton vypinac;

    private GestureDetector gestureScanner;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        gestureScanner = new GestureDetector(this);
        this.vypinac = (ToggleButton)this.findViewById
(R.id.toggle_button);
        viewA = (TextView)this.findViewById(R.id.label);



        try {
                vypinac.setTextOn("LOL");
        } catch (Exception ex) {
           System.out.print("Exception" + ex.toString());
        }

        setContentView(R.layout.main);
    }

    @Override
    public boolean onTouchEvent(MotionEvent me)
    {
     return gestureScanner.onTouchEvent(me);
    }

    @Override
    public boolean onDown(MotionEvent e)
    {
     viewA.setText("-" + "DOWN" + "-");
     return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float
velocityX, float velocityY)
    {
     viewA.setText("-" + "FLING" + "-");
     return true;
    }

    @Override
    public void onLongPress(MotionEvent e)
    {
     viewA.setText("-" + "LONG PRESS" + "-");
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float
distanceX, float distanceY)
    {
     viewA.setText("-" + "SCROLL" + "-");
     return true;
    }

    @Override
    public void onShowPress(MotionEvent e)
    {
     viewA.setText("-" + "SHOW PRESS" + "-");
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e)
    {
     viewA.setText("-" + "SINGLE TAP UP" + "-" + e.getX());
     return true;
    }
}


AND the main.xml:


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


<ToggleButton android:id="@+id/toggle_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:textOn="Zastavit"
        android:textOff="Vpřed"/>
    <TextView
        android:id="@+id/label"
        android:layout_width="200sp"
        android:layout_height="200sp"
        android:background="#9CEB00"/>

</RelativeLayout>


Everytime I make action (touch the display or so (call the viewA.* or
vypinac.set*)) it crashes due to java.lang.NullPointerException. So it
looks like the
this.vypinac = (ToggleButton)this.findViewById(R.id.toggle_button);
is not works. I dont know what I do bad, please let me know the way or
so.
Thanks in advance :-)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to