Tha.Poox
Tha.Poox

Reputation: 119

Android slider layout and NullPointerException

ok so I have the main layout with a button and a listview, what I want to do is to when the button is tapped, some others stuff with show up on top on top of it (some other buttons and textviews...), so technecally, the button will make the Listview and itself sliding down so the other elements will show up. So I've created a class called Slider, extending the LinearLayout class, in which I wrote the method that'll make the slide, and i've used this class in the XML file to display the layout.

First, here's the code:

Slider.java:

public class Slider extends LinearLayout{

    //The layout that'll show up when the slide is done: 

    public RelativeLayout Show= (RelativeLayout)findViewById(R.id.Show);

    public Slider(Context context) {
        super(context);
    }

    //Method that'll make the slide
    public void toggle(){
        TranslateAnimation animation;
        animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, -Show.getHeight());

        animation.setDuration(2000);
        animation.setInterpolator(new AccelerateInterpolator());
        startAnimation(animation);
    }
    public String toString(){
        return "THE TOSTRING....";
    }

}

Slider.xml:

<com.example.andtestbdd3.Slider
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/slider"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:padding="5dip">

    <RelativeLayout
        android:id="@+id/Show"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/bouton1">

        <EditText
            android:id="@+id/Edit1"
            android:layout_height="wrap_content"
            android:layout_width="130dp"
            android:hint="Nom"
            android:paddingTop="1dp"
            android:paddingBottom="1dp"
            android:background="#C2BCBC"
            android:maxLength="25"
            android:layout_marginRight="20dp"
            android:layout_marginBottom="3dp"/>

        <EditText
            android:id="@+id/Edit2"
            android:layout_height="wrap_content"
            android:layout_width="130dp"
            android:hint="Prenom"
            android:paddingTop="1dp"
            android:paddingBottom="1dp"
            android:layout_toRightOf="@id/Edit1"
            android:background="#C2BCBC"
            android:maxLength="25"/>

    </RelativeLayout>

</com.example.andtestbdd3.Slider>

MaintActivity.java:

public class MainActivity extends Activity {

    Slider slider = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
           //Getting the slider from the XML file which uses the Slider class (Slider.java)
        slider = (Slider)findViewById(R.id.slider);
        //everything is ok till here, When I try to use some stuff from the Slider,                      //the NullPointerEXCEPTION 
        //Will show up, for example; if I call the toString methode:
    Toast.makeText(this,"Le text: "+slider.toString(), Toast.LENGTH_LONG).show();
    }

}

Log:

02-12 09:07:20.870: D/AndroidRuntime(933): Shutting down VM
02-12 09:07:20.870: W/dalvikvm(933): threadid=1: thread exiting with uncaught exception (group=0xb4aaaba8)
02-12 09:07:20.890: E/AndroidRuntime(933): FATAL EXCEPTION: main
02-12 09:07:20.890: E/AndroidRuntime(933): Process: com.example.andtestbdd3, PID: 933
02-12 09:07:20.890: E/AndroidRuntime(933): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.andtestbdd3/com.example.andtestbdd3.MainActivity}: java.lang.NullPointerException
02-12 09:07:20.890: E/AndroidRuntime(933):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
02-12 09:07:20.890: E/AndroidRuntime(933):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-12 09:07:20.890: E/AndroidRuntime(933):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-12 09:07:20.890: E/AndroidRuntime(933):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-12 09:07:20.890: E/AndroidRuntime(933):  at android.os.Handler.dispatchMessage(Handler.java:102)
02-12 09:07:20.890: E/AndroidRuntime(933):  at android.os.Looper.loop(Looper.java:136)
02-12 09:07:20.890: E/AndroidRuntime(933):  at android.app.ActivityThread.main(ActivityThread.java:5017)
02-12 09:07:20.890: E/AndroidRuntime(933):  at java.lang.reflect.Method.invokeNative(Native Method)
02-12 09:07:20.890: E/AndroidRuntime(933):  at java.lang.reflect.Method.invoke(Method.java:515)
02-12 09:07:20.890: E/AndroidRuntime(933):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-12 09:07:20.890: E/AndroidRuntime(933):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-12 09:07:20.890: E/AndroidRuntime(933):  at dalvik.system.NativeStart.main(Native Method)
02-12 09:07:20.890: E/AndroidRuntime(933): Caused by: java.lang.NullPointerException
02-12 09:07:20.890: E/AndroidRuntime(933):  at com.example.andtestbdd3.MainActivity.onCreate(MainActivity.java:53)
02-12 09:07:20.890: E/AndroidRuntime(933):  at android.app.Activity.performCreate(Activity.java:5231)
02-12 09:07:20.890: E/AndroidRuntime(933):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-12 09:07:20.890: E/AndroidRuntime(933):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-12 09:07:20.890: E/AndroidRuntime(933):  ... 11 more

Activity_main.xml:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#000000"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/bouton1"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:background="#595959"
        android:gravity="center"
        android:paddingBottom="2dp"
        android:text="Ajouter" />



<LinearLayout
    android:id="@+id/layout"
    android:layout_width="250dp" 
    android:layout_height="wrap_content"
    android:layout_marginTop="70dp"
    android:layout_marginLeft="17dp"
    android:padding="2dp"
    android:background="#595959">

    <ListView
        android:id="@+id/lista"
        android:layout_width="250dp"
        android:layout_height="fill_parent"
        android:background="#7E7E7E"
        android:choiceMode="singleChoice"
        android:divider="#6F6F6F"
        android:dividerHeight="2dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        />

</LinearLayout>

</RelativeLayout>

So when I get the Slider from the xml file, no problem. but when I try to use some methodes from the class file, I get the NullPointerException ! I hope that my problem is clear. If you need more informations just tell me, I'll update as soon as possible. Thanks

Upvotes: 1

Views: 698

Answers (2)

donfuxx
donfuxx

Reputation: 11321

Without logcat output I am shooting a bit in the dark, but after a quick overview I see that you using activity_main.xml in setContentView method. Would be nice to see the activity_main layout code. I assume your error might be here:

        slider = (Slider)findViewById(R.id.slider);

because findViewById(R.id.slider) will look for a view element with id "slider" inside the activity_main.xml that you previously set, it is not looking inside your slider.xml! ...And will return null if it is not found!

Edit: Ok, with logcat output now it is now confirmed that slider is set to null.

Edit 2: So now I can see your activity_main layout: Let me explain step-by-step what is happening:

  1. You are setting the activity content from your layout ressource "activity_main" here: setContentView(R.layout.activity_main);

  2. Then you use findViewById(id) to look up a view with R.id.slider inside the activity content!

  3. findViewById returned null because there is nothing with R.id.slider in the activity_main.xml

  4. slider points to null

  5. in your Toast you try to call method toString(), but your slider == null!

==> NPE !

suggested solution:

Maybe merge your slider.xml code into your activity main layout! If you don't want it to be visible from the beginning then you could just set the visibility of the view to GONE. For example like this:

android:visibility="gone"

later you can just set visibily to visible again programatically.

Really hope this helps!

Upvotes: 0

thepoosh
thepoosh

Reputation: 12587

it seems to me like your Slider object was not found and was null when you tried to use slider.toString() as can be seen from your logcat line:

com.example.andtestbdd3.MainActivity.onCreate(MainActivity.java:53)

Upvotes: 1

Related Questions