gsfd
gsfd

Reputation: 1070

Annoying NullPointerException

Here is my main class:

import android.graphics.Rect;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

public final class HomeScreen implements OnTouchListener
{
    public HomeScreen(Viewer view) 
    {
        this.view = view;
        RelativeLayout layout = (RelativeLayout)view.findViewById(R.id.home_screen_lay);
        layout.setOnTouchListener(this); // this is where the error points to
        network.setFocus(this);
        view.setContentView(R.layout.home);
    }

    public boolean onTouch(View v, MotionEvent e) 
    {
        switch(e.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                onPress(v,e);
                break;
            case MotionEvent.ACTION_MOVE:
                onMove(v,e);
                break;
            case MotionEvent.ACTION_UP:
                onRelease(v,e);
                break;
        }
        return true;
    }

    public void onPress(View v, MotionEvent e)
    {
        for(int x=0;x<buttons.length;x++)
        {
            if(buttons[x].contains((int)e.getX(),(int)e.getY()))
            {
                if(buttons[x]==exitRect)
                    view.finish();
                if(buttons[x]==newUserRect)
                    new NewUser(view,network);
                if(buttons[x]==loginRect)
                    new Login(view,network);
            }
        }
    }
    public void onMove(View v, MotionEvent e){}
    public void onRelease(View v, MotionEvent e){}

    public void handleMessage(String message){}
    private NetworkManager network;
    private Viewer view;
    private Rect loginRect;
    private Rect newUserRect;
    private Rect exitRect;
    private Rect[] buttons;
}

and heres home.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/home_screen_lay"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/home_screen">
</RelativeLayout>

The NullPointerExeption points to that line in my HomeScreen class, it says that layout is null. I have been looking at this for hours and i cant see whats wrong. Can someone else see what I'm missing? All relevant answers are welcome!

Upvotes: 2

Views: 334

Answers (3)

Rudra Saraswat
Rudra Saraswat

Reputation: 71

You have to put setContentView method at the beginning of the method named HomeScreen.

view.setContentView(R.layout.home);

Upvotes: 1

fireshadow52
fireshadow52

Reputation: 6516

I try to set all my attributes (i.e. layout) before adding any listeners. In that sense move:

layout.setOnTouchListener(this);

to the bottom of the method (sorry @mharper for a possible duplicate answer). this is generating the exception, so change this to HomeScreen in the statement above.

Upvotes: 1

mharper
mharper

Reputation: 3272

What if you move:

view.setContentView(R.layout.home);

to the top of the method before you try to get the layout object?

Upvotes: 5

Related Questions