Android自定义组件在运行时工作正常,但在设计时不起作用

时间:2022-07-14 21:09:20

I am doing my first steps in Android development. In the meantime I am trying to create a custom component (for testing purposes) that will later be used on an Activity.

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

Here's the code for ClearableEditText.java:

这是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");
    }    
}

And here's its layout XML file:

这是它的布局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>

This is the XML layout code for the activity that will integrate the component:

这是将集成组件的活动的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>

When I run the app, the component works fine. Nonetheless, as soon as I switch to the Graphical Layout of my Activity, I get the following error message:

当我运行应用程序时,该组件工作正常。尽管如此,只要切换到我的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)

I have no clue about what's going on in here. Can anyone help me out?

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

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

这是我的主要活动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="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

我没有任何例外。检查你的包裹名称

#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

这是我的主要活动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="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

我没有任何例外。检查你的包裹名称