我创建了一个自定义按钮。但是当我尝试使用它时,我在布局的图形视图中得到了这些异常:
java.lang.NullPointerException
at com.aspa.warehousing.CirculeButton.setText(CirculeButton.java:36)
at com.aspa.warehousing.CirculeButton.<init>(CirculeButton.java:32)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0( at sun.reflect.NativeConstructorAccessorImpl.newInstance( at sun.reflect.DelegatingConstructorAccessorImpl.newInstance( at java.lang.reflect.Constructor.newInstance( at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.instantiateClass(ProjectCallback.java:437)
at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.loadView(ProjectCallback.java:189)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:135)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:755)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:727)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:373)
这是我的按钮布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/LinearLayout">
<com.aspa.warehousing.SquareLinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@drawable/round_button_unselect">
<ImageView
android:id="@+id/button_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerInside"/>
</com.aspa.warehousing.SquareLinearLayout>
<TextView
android:layout_marginTop="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:id="@+id/button_caption"/>
</LinearLayout>
这是按钮类:
package com.aspa.warehousing;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class CirculeButton extends LinearLayout {
ImageView buttonImg;
TextView buttonCaption;
public CirculeButton(Context context) {
super(context);
}
public CirculeButton(Context context, AttributeSet attrs) {
super(context,attrs);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.circle_button, this, true);
buttonImg = (ImageView) findViewById(R.id.button_image);
buttonCaption = (TextView) findViewById(R.id.button_caption);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CirculeButton);
String caption = a.getString(R.styleable.CirculeButton_buttonText);
Drawable image = a.getDrawable(R.styleable.CirculeButton_buttonImg);
a.recycle();
setText(caption);
setImage(image);
}
public void setText(String caption) {
buttonCaption.setText(caption);
}
private void setImage(Drawable image) {
buttonImg.setImageDrawable(image);
}
}
setText
和setImage
方法引发的扩展。似乎buttonImg
和buttonCaption
是null
,但我无法修复它!有什么建议吗?
已编辑:这就是我使用它的方式:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.aspa.warehousing"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<com.aspa.warehousing.CirculeButton
android:id="@+id/CirculeButton1"
android:layout_width="60dp"
android:layout_height="60dp"
app:buttonImg="@drawable/raw_materials"
app:buttonText="@string/raw_materials" />
</LinearLayout>
此处抛出的异常。
答案 0 :(得分:0)
这只是一个日食错误:|
关闭日食并再次打开它:D
太容易了!