Android setonclicklistener崩溃了

时间:2016-07-03 09:21:17

标签: android nullpointerexception setonerrorlistener

我尝试在触摸线性布局或后退按钮后返回到上一个活动但是我得到了nullpointer异常错误甚至很难我在onCreate方法中初始化了按钮和布局

  

java.lang.NullPointerException:尝试调用虚方法' void   android.widget.LinearLayout.setOnClickListener(android.view.View $ OnClickListener)'   在空对象引用上

我的Java文件

package com.profile;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.R;

public class AboutActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
        LinearLayout backlayout=(LinearLayout) findViewById(R.id.back_button_linear_layout);
        ImageButton backButton=(ImageButton) findViewById(R.id.back_profile_btn);
        backlayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

    }

} 

我的XML文件

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context="com.profile.AboutActivity">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@color/colorPrimaryDark">

        <LinearLayout
            android:id="@+id/back_profile_layout"
            android:layout_width="55dp"
            android:layout_height="match_parent"
            android:gravity="start|center_vertical"
            android:orientation="vertical">

            <ImageButton
                android:id="@+id/back_profile_btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/activity_horizontal_margin"
                android:adjustViewBounds="true"
                android:background="@color/transparentBackgroundPrimaryDark"
                android:maxHeight="20dp"
                android:maxWidth="20dp"
                android:scaleType="centerInside"
                android:src="@drawable/back_100px" />
        </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="About"
            android:textAllCaps="true"
            android:textColor="@color/colorPrimary"
            android:textSize="20sp"
            android:textStyle="bold" />
    </RelativeLayout>
    <android.support.v4.widget.Space
        android:layout_width="10sp"
        android:layout_height="20sp" />
    <Button
        android:id="@+id/Terms"
        android:layout_width="345dp"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded_button_primary"
        android:drawable="@color/colorPrimary"
        android:clickable="true"
        android:textAllCaps="false"
        android:layout_gravity="center"
        android:text="Terms and Conditions"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="18sp"
        />
    <android.support.v4.widget.Space
        android:layout_width="10sp"
        android:layout_height="20sp" />
    <Button
        android:id="@+id/Data_policy"
        android:layout_width="345dp"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded_button_primary"
        android:color="@color/colorPrimary"
        android:clickable="true"
        android:textAllCaps="false"
        android:layout_gravity="center"
        android:text="Data policy"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="18sp"
        />
    <android.support.v4.widget.Space
        android:layout_width="10sp"
        android:layout_height="20sp" />
    <Button
        android:id="@+id/Licences"
        android:layout_width="345dp"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded_button_primary"
        android:drawable="@color/colorPrimary"
        android:clickable="true"
        android:textAllCaps="false"
        android:layout_gravity="center"
        android:text="Licences"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="18sp"
        />
    <android.support.v4.widget.Space
        android:layout_width="10sp"
        android:layout_height="20sp" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="209dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Version 1.1.1000"
        android:gravity="center"
        android:textColor="@color/colorAccent"
        android:textSize="16sp"
        android:textStyle="bold"
        android:layout_weight="0.87" />

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

更改此行

LinearLayout backlayout=(LinearLayout) findViewById(R.id.back_button_linear_layout);

到这个

LinearLayout backlayout=(LinearLayout) findViewById(R.id.back_profile_layout);

因为您向id分配了错误的LinearLayout,这就是为什么它会为您提供NullPointerExecption

并且还不需要删除此import com.R;