zoukankan      html  css  js  c++  java
  • Android布局文件使用<include>标签出现NullPointerException问题

    出现问题

    在布局文件中使用标签大家肯定都不陌生,能减少冗余的代码。之前写项目中也一直在用到,但是有一次用的时候直接出现了NullPointerException的问题,下面是出现问题的代码

    Activity中的代码

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textView = (TextView) findViewById(R.id.text_include);
    
        RelativeLayout rl_include = (RelativeLayout) findViewById(R.id.rl_layout);
        rl_include.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "text", Toast.LENGTH_SHORT).show();
            }
        });
    
    }
    

    activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        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"
        tools:context="com.example.administrator.mytestapp.MainActivity">
    
        	<include
       	     android:id="@+id/include"
        		layout="@layout/layout_inclue" />
        
        	<TextView
    		    android:id="@+id/textView"
    		    android:layout_width="wrap_content"
    		    android:layout_height="wrap_content"
    		    android:layout_centerHorizontal="true"
    		    android:layout_marginTop="135dp"
    		    android:text="Hello World!" />
    </RelativeLayout>
    

    layout_inclue.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    	android:id="@+id/rl_layout"
    	android:layout_width="match_parent"
    	android:layout_height="45dp"
    	android:background="@android:color/darker_gray"
    	android:orientation="vertical">
    
        <TextView
        	android:id="@+id/text_include"
        	android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:layout_centerInParent="true"
        	android:text="INCLUDE_TITLE"
        	android:textColor="@android:color/holo_red_dark" />
    
    </RelativeLayout>
    

    出错的地方

    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

    是在设置layout_inclue布局中相对布局点击事件出现空指针的问题,最后是发现是activity_main.xml中标签的id覆盖了layout_inclue.xml中根布局的id,导致的在Activity中获取layout_inclue.xml根布局的为null。

    提供两个解决方法

    1.删除activity_main.xml中标签的id,程序即可正常运行
    2.使用activity_main.xml中标签的id
     RelativeLayout rl_include = (RelativeLayout) findViewById(R.id.include);
        rl_include.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "text", Toast.LENGTH_SHORT).show();
            }
        });
    

    相关链接

    Android Layout nullpointerException

  • 相关阅读:
    poj 2349 Arctic Network
    hdu 1596 find the safest road
    Codeforces 768B. Code For 1
    Codeforces 448C. Painting Fence
    Problem D. Ice Cream Tower(2016 China-Final)
    poj 2785 4 Values whose Sum is 0
    Codeforces 797C. Minimal string
    Codeforces 264A. Escape from Stones
    乌龟棋(noip2010)
    noip2018模拟题(类背包+贪心)
  • 原文地址:https://www.cnblogs.com/cloud9527/p/13129494.html
Copyright © 2011-2022 走看看