简体   繁体   English

onClickListener上的java.lang.NullPointerException

[英]java.lang.NullPointerException on onClickListener

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() {" 我在这里看不到任何问题,但是logcat在这一行“ agree.setOnClickListener(new OnClickListener(){”中显示java.lang.NullPointerException

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

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. 您的R.id.btnTermsR.id.btnTerms是在activity_register.xml中定义的,但是该布局永远不会膨胀。 or not accessible at that point :) 或当时无法访问:)

Its the button that has the Problem, not the OnClickListener. 它是有问题的按钮,而不是OnClickListener。 A nullpointer within the Listener would appear the moment you click the button, not while attaching it. 单击该按钮时,而不是附加按钮时,侦听器中的空指针就会出现。

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

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

change this line too

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM