4

我正在迈出 Android 开发的第一步。与此同时,我正在尝试创建一个稍后将在 Activity 上使用的自定义组件(用于测试目的)。

这是 ClearableEditText.java 的代码:

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class ClearableEditText extends LinearLayout {

    private EditText textField;
    private Button button;

    public ClearableEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        LayoutInflater.from(this.getContext()).inflate(R.layout.clearable_text,  this);

        setupViewItems();
    }

    private void setupViewItems() {
        // TODO Auto-generated constructor stub


        this.textField = (EditText) findViewById(R.id.editText1);
        this.button = (Button) findViewById(R.id.button1);

        this.textField.setText("Hello World 123");
    }    
}

这是它的布局 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"        
        android:text="Button" />

</LinearLayout>

这是将集成组件的活动的 XML 布局代码:

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/ativity2_text" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="33dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <com.example.aplicacaoteste.ClearableEditText
        android:id="@+id/newComponent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="33dp"
        android:ems="10" >
    </com.example.aplicacaoteste.ClearableEditText>


</RelativeLayout>

当我运行该应用程序时,该组件工作正常。尽管如此,一旦我切换到我的 Activity 的图形布局,我就会收到以下错误消息:

java.lang.NullPointerException
Exception details are logged in Window > Show View > Error Log

java.lang.NullPointerException
    at com.example.aplicacaoteste.ClearableEditText.setupViewItems(ClearableEditText.java:36)
    at com.example.aplicacaoteste.ClearableEditText.onFinishInflate(ClearableEditText.java:26)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:754)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:718)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:749)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:718)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:372)

我不知道这里发生了什么。谁能帮我吗?

4

1 回答 1

0

This source code has working perfectly. This my source code

package com.testapplication.customtools;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class ClearableEditText  extends LinearLayout{

    private EditText textField;
    private Button button;

    public ClearableEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        LayoutInflater.from(this.getContext()).inflate(R.layout.clearable_text,  this);

        setupViewItems();
    }

    private void setupViewItems() {
        // TODO Auto-generated constructor stub


        this.textField = (EditText) findViewById(R.id.editText1);
        this.button = (Button) findViewById(R.id.button1);

        this.textField.setText("Hello World 123");
    }    
}

This is my Main activity xml Files

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="test" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="33dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <com.testapplication.customtools.ClearableEditText
        android:id="@+id/newComponent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="33dp"
        android:ems="10" >
    </com.testapplication.customtools.ClearableEditText>


</RelativeLayout>

I am not getting any exception. check your package name

于 2014-08-25T09:30:53.410 回答