0

Can you see if there is anything wrong with my code? I tried to create dialog box when the button is pressed. I don't see something wrong here, But the logcat shows java.lang.NullPointerException in this line "agree.setOnClickListener(new OnClickListener() {"

 package com.sociyo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); //Lock Orientation
        requestWindowFeature(Window.FEATURE_NO_TITLE); //Hide Action menu
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); //Load main activity

        //viewPager default page
        ViewPagerAdapter adapter = new ViewPagerAdapter();
        ViewPager myPager = (ViewPager) findViewById(R.id.pager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(0);

        //textView clickable
        Button agree = (Button)findViewById(R.id.btnTerms);

        agree.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                AlertDialog.Builder tpDialog = new AlertDialog.Builder(null);

                tpDialog.setTitle("Terms and Policy");

                tpDialog.setMessage(R.string.action_settings)
                        .setCancelable(false)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        MainActivity.this.finish();
                    }
                });
            }
        });



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



}

And this is the complete log cat :

    E/AndroidRuntime(13076): FATAL EXCEPTION: main

    E/AndroidRuntime(13076): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sociyo/com.sociyo.MainActivity}: java.lang.NullPointerException

    E/AndroidRuntime(13076):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2070)

    E/AndroidRuntime(13076):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2095)

    E/AndroidRuntime(13076):    at android.app.ActivityThread.access$600(ActivityThread.java:137)

    E/AndroidRuntime(13076):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)

    E/AndroidRuntime(13076):    at android.os.Handler.dispatchMessage(Handler.java:99)

    E/AndroidRuntime(13076):    at android.os.Looper.loop(Looper.java:213)

    E/AndroidRuntime(13076):    at android.app.ActivityThread.main(ActivityThread.java:4793)

    E/AndroidRuntime(13076):    at java.lang.reflect.Method.invokeNative(Native Method)

    E/AndroidRuntime(13076):    at java.lang.reflect.Method.invoke(Method.java:511)

    E/AndroidRuntime(13076):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)

    E/AndroidRuntime(13076):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)

    E/AndroidRuntime(13076):    at dalvik.system.NativeStart.main(Native Method)

    **E/AndroidRuntime(13076): Caused by: java.lang.NullPointerException**

    E/AndroidRuntime(13076):    at com.sociyo.MainActivity.onCreate(MainActivity.java:34)

    E/AndroidRuntime(13076):    at android.app.Activity.performCreate(Activity.java:5008)

    E/AndroidRuntime(13076):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)

    E/AndroidRuntime(13076):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034)

    E/AndroidRuntime(13076):    ... 11 more

My activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@drawable/background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" >
    </android.support.v4.view.ViewPager>

</RelativeLayout>

And my activity_register.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@android:color/transparent"
    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:orientation="vertical" >

    <TextView
        android:id="@+id/tvRegisterTitle"
        style="@style/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/register_text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/etNameReg"
        style="@style/textfield"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/spMlmList"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:ems="10"
        android:hint="@string/name_hint"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/etEmailReg"
        style="@style/textfield"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/etNameReg"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:ems="10"
        android:hint="@string/email_hint"
        android:inputType="textEmailAddress" />

    <EditText
        android:id="@+id/etPasswordReg"
        style="@style/textfield"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/etEmailReg"
        android:layout_below="@+id/etEmailReg"
        android:layout_centerVertical="true"
        android:layout_marginTop="15dp"
        android:ems="10"
        android:hint="@string/password_hint"
        android:inputType="textPassword" />

    <Spinner
        android:id="@+id/spMlmList"
        style="@style/textfield"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvRegisterTitle"
        android:layout_below="@+id/tvRegisterTitle"
        android:layout_marginTop="15dp"
        android:entries="@array/mlm_list" />

    <Button
        android:id="@+id/btnRegister"
        style="@style/buttonBlue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/etPasswordReg"
        android:layout_below="@+id/chkAgree"
        android:layout_marginTop="15dp"
        android:text="@string/register_text" />

    <CheckBox
        android:id="@+id/chkAgree"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/etPasswordReg"
        android:text="@string/agree_text"
        android:layout_marginTop="15dp" 
        style="@style/checkbox"/>

    <Button
        android:id="@+id/btnTerms"
        android:background="@android:color/transparent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/chkAgree"
        android:layout_alignBottom="@+id/chkAgree"
        android:layout_toRightOf="@+id/tvRegisterTitle"
        android:layout_marginLeft="10dp"
        android:text="@string/terms_policy" 
        style="@style/smallLink"/>

</RelativeLayout>
10
  • please show your xml code. Commented Sep 18, 2013 at 10:11
  • what code is in your line number 34..? Commented Sep 18, 2013 at 10:11
  • Hi, it's "agree.setOnClickListener(new OnClickListener()" { on line 34 Commented Sep 18, 2013 at 10:12
  • 1
    use this agree.setOnClickListener(new View.OnClickListener() { Commented Sep 18, 2013 at 10:17
  • 1
    yes, its got to do something with the viewpager, you cant set onclick on button directly to viewpager. also check this stackoverflow.com/questions/10691628/… edit-gave the wrong link. but updated the new one Commented Sep 18, 2013 at 10:27

3 Answers 3

3

Yes the button doesnt exist at that point.

Your R.id.btnTermsR.id.btnTerms is defined in activity_register.xml, but that layout is never inflated. or not accessible at that point :)

Its the button that has the Problem, not the OnClickListener. A nullpointer within the Listener would appear the moment you click the button, not while attaching it.

Sign up to request clarification or add additional context in comments.

6 Comments

Hi, Daniel, I put the activity_register.xml in a viewPager along with other xml. And put the viewPager in my ActivityMain. Is that why I can't access the button?
you are asking the activity for the button, you would have to ask the specific View which actually contains it. Try a if (agree == null) sysout("button null") - i guess it will appear in logcat :)
Yes, I can't seems to find the btnTerms id. Is there a way to access that button from viewPager activity?
You can use viewpager.findViewById() to find your activity_register.xml and on that view call view.findViewById to retrieve your button
I used myPager.findViewById() which doesn't generate an error in eclipse. But when I compile it, I got the same java null exception too. It's weird...
|
1
// Before Replace
AlertDialog.Builder tpDialog = new AlertDialog.Builder(null);

// After Replace
 AlertDialog.Builder tpDialog = new AlertDialog.Builder(MainActivity.this);

4 Comments

AlertDialog.Builder tpDialog = new AlertDialog.Builder(null); you set null so it's gave you null pointer exception at run time
one more problem i found your code is activity_main.xml is not contain any btnTerms Button but you use "Button agree = (Button)findViewById(R.id.btnTerms);" so it also gave you run time exception
Yes, the btnTerms is in other activity which use viewPager.
So you have define this button in viewPager layout and check your code run well...
0
AlertDialog.Builder tpDialog = new AlertDialog.Builder(null);

change this line too

 AlertDialog.Builder tpDialog = new AlertDialog.Builder(MainActivity.this);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.