onClickListener上的java.lang.NullPointerException

时间:2013-09-18 10:09:29

标签: java android nullpointerexception

你能看看我的代码有什么问题吗? 我按下按钮时尝试创建对话框。 我没有看到错误,但是logcat在这一行中显示了java.lang.NullPointerException“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;
    }



}

这是完整的日志猫:

    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>

我的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>

3 个答案:

答案 0 :(得分:3)

是的,此时按钮不存在。

您的R.id.btnTermsR.id.btnTerms在activity_register.xml中定义,但该布局永远不会膨胀。或者在那时无法访问:)

它是具有问题的按钮,而不是OnClickListener。监听器中的nullpointer将在您单击按钮时出现,而不是在连接时出现。

答案 1 :(得分:1)

// Before Replace
AlertDialog.Builder tpDialog = new AlertDialog.Builder(null);

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

答案 2 :(得分:0)

AlertDialog.Builder tpDialog = new AlertDialog.Builder(null);

change this line too

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