android 4.2 中的 Butterknife Nullpointer 异常
Butterknife Nullpointer exception in android 4.2
在我当前的项目中使用 Butterknife;它在 Kitkat 和 lollipop 上运行良好,但在 jellybean (4.2) 上抛出空指针异常。
这一行抛出异常(btnLogin 为 NULL)
btnLogin.setTypeface(FontsHelper.robotoRegular(这个));
Activity代码
public class Login extends Activity {
@Bind(R.id.btnLogin)
FButton btnLogin;
@Bind(R.id.btnRegister)
FButton btnRegister;
@Bind(R.id.edtEmail)
EditText edtEmail;
@Bind(R.id.edtPassword)
EditText edtPassword;
@Bind(R.id.rlProgress)
RelativeLayout rlProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
System.out.println("Binding password" + edtPassword); //prints null
System.out.println("Binding "+btnLogin); //prints null
btnLogin.setTypeface(FontsHelper.robotoRegular(this));
}
}
布局....
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="vertical"
android:gravity="center">
<ImageView android:id="@+id/imageView1" android:layout_width="wrap_content"
android:layout_height="0dp" android:src="@drawable/lm_logo_with_text"
android:layout_weight="0.5" android:paddingLeft="@dimen/pad_40dp"
android:paddingRight="@dimen/pad_40dp"
android:gravity="center_horizontal|center_vertical"/>
<LinearLayout android:id="@+id/llContent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_weight="0.5"
android:orientation="vertical" android:paddingLeft="@dimen/pad_40dp" android:paddingRight="@dimen/pad_40dp">
<EditText android:id="@+id/edtEmail" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:hint="Email"
android:drawableLeft="@drawable/ic_email" android:inputType="textEmailAddress" style="@style/txt_white">
<requestFocus />
</EditText>
<View android:background="@color/gray"
android:layout_width="fill_parent" android:layout_height="@dimen/divider" android:layout_marginTop="@dimen/pad_16dp" />
<EditText android:id="@+id/edtPassword" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:hint="Password"
android:drawableLeft="@drawable/ic_pwd" android:inputType="textPassword" style="@style/txt_white" />
<View android:background="@color/gray"
android:layout_width="fill_parent" android:layout_height="@dimen/divider" />
<View android:background="@color/gray"
android:layout_width="fill_parent" android:layout_height="@dimen/divider" android:layout_marginTop="@dimen/pad_16dp"
android:visibility="gone"/>
<EditText android:id="@+id/edtEmailUnused" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:hint="Email address"
android:drawableLeft="@drawable/ic_pwd" android:inputType="textEmailAddress" style="@style/txt_white"
android:visibility="gone"/>
<info.hoang8f.widget.FButton
android:layout_width="fill_parent" android:layout_height="50dp"
android:id="@+id/btnLogin"
android:textColor="@color/white"
fbutton:buttonColor="@color/picton_blue"
fbutton:shadowColor="@color/san_marino"
fbutton:shadowEnabled="true"
fbutton:shadowHeight="2dp"
fbutton:cornerRadius="2dp"
android:text="Login"
android:layout_marginBottom="@dimen/pad_10dp"
android:layout_marginTop="@dimen/pad_40dp"
android:padding="@dimen/pad_15dp"
/>
<info.hoang8f.widget.FButton
android:layout_width="fill_parent" android:layout_height="50dp"
android:id="@+id/btnRegister"
android:textColor="@color/white"
fbutton:buttonColor="@color/niagara"
fbutton:shadowColor="@color/san_marino"
fbutton:shadowEnabled="true"
fbutton:shadowHeight="2dp"
fbutton:cornerRadius="2dp"
android:text="Register"
android:layout_marginBottom="@dimen/pad_40dp"
android:layout_marginTop="@dimen/pad_5dp"
android:padding="@dimen/pad_15dp"
android:onClick="gotoHome"
/>
<Button android:id="@+id/btnFb" android:background="@drawable/btn_fb"
android:layout_width="fill_parent" android:layout_height="wrap_content" style="@style/btn" android:visibility="gone"/>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:id="@+id/rlProgress"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white_45_percent"
android:clickable="true"
android:animateLayoutChanges="true"
android:visibility="gone"
xmlns:wheel="http://schemas.android.com/apk/res-auto">
<com.pnikosis.materialishprogress.ProgressWheel
android:id="@+id/progress_wheel"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
wheel:matProg_barColor="#555588FF"
wheel:matProg_progressIndeterminate="true" />
</RelativeLayout>
堆栈跟踪
java.lang.RuntimeException: Unable to start activity ComponentInfo{lifemedico.array.com.lifemedico/lifemedico.array.com.lifemedico.ui.login.Login}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at lifemedico.array.com.lifemedico.ui.login.Login.onCreate(Login.java:52)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
... 11 more
java.lang.NullPointerException
at lifemedico.array.com.lifemedico.ui.login.Login.onCreate(Login.java:52)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
在 lollipop 和 kitkat 上没有任何问题,但调试语句都在 jelly bean 中打印 null。想不出任何失败的原因,非常感谢任何帮助。
谢谢
编辑:
向 butterknife 添加调试选项给了我这个
未找到。尝试超级class android.support.v7.app.ActionBarActivityd
小姐:已达到框架 class。放弃搜索。
一些网上搜索指向混淆器,但我没有使用混淆器,这仍然是一个调试版本。
请填写您的字段 public 即:
@Bind(R.id.btnLogin)
public FButton btnLogin;
@Bind(R.id.btnRegister)
public FButton btnRegister;
@Bind(R.id.edtEmail)
public EditText edtEmail;
@Bind(R.id.edtPassword)
public EditText edtPassword;
@Bind(R.id.rlProgress)
public RelativeLayout rlProgress;
Butterknife 不能绑定私有变量,所以要public
我猜我看错洞了;没有 programming/syntax/logic 错误。正如我提到的,我查看了混淆器并意识到我的 gradle 有 multiDexEnabled;出于某种原因,Butterknife 函数被删除了。我所要做的就是将我的应用程序设置为扩展 MultiDexApplication,它就像一个魅力;花了我一天的时间才弄明白。
public class App extends MultiDexApplication {
我在这个 post https://github.com/JakeWharton/butterknife/issues/311
中找到了线索
这个问题可以通过更新你的 gradle 文件来解决,正如 Butterknife 在他们的 gitgub
上提到的
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
apply plugin: 'com.neenbedankt.android-apt'
dependencies {
compile 'com.jakewharton:butterknife:8.0.1'
apt 'com.jakewharton:butterknife-compiler:8.0.1'
}
具体参考可以参考linkhttps://github.com/JakeWharton/butterknife
在我当前的项目中使用 Butterknife;它在 Kitkat 和 lollipop 上运行良好,但在 jellybean (4.2) 上抛出空指针异常。
这一行抛出异常(btnLogin 为 NULL)
btnLogin.setTypeface(FontsHelper.robotoRegular(这个));
Activity代码
public class Login extends Activity {
@Bind(R.id.btnLogin)
FButton btnLogin;
@Bind(R.id.btnRegister)
FButton btnRegister;
@Bind(R.id.edtEmail)
EditText edtEmail;
@Bind(R.id.edtPassword)
EditText edtPassword;
@Bind(R.id.rlProgress)
RelativeLayout rlProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
System.out.println("Binding password" + edtPassword); //prints null
System.out.println("Binding "+btnLogin); //prints null
btnLogin.setTypeface(FontsHelper.robotoRegular(this));
}
}
布局....
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="vertical"
android:gravity="center">
<ImageView android:id="@+id/imageView1" android:layout_width="wrap_content"
android:layout_height="0dp" android:src="@drawable/lm_logo_with_text"
android:layout_weight="0.5" android:paddingLeft="@dimen/pad_40dp"
android:paddingRight="@dimen/pad_40dp"
android:gravity="center_horizontal|center_vertical"/>
<LinearLayout android:id="@+id/llContent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_weight="0.5"
android:orientation="vertical" android:paddingLeft="@dimen/pad_40dp" android:paddingRight="@dimen/pad_40dp">
<EditText android:id="@+id/edtEmail" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:hint="Email"
android:drawableLeft="@drawable/ic_email" android:inputType="textEmailAddress" style="@style/txt_white">
<requestFocus />
</EditText>
<View android:background="@color/gray"
android:layout_width="fill_parent" android:layout_height="@dimen/divider" android:layout_marginTop="@dimen/pad_16dp" />
<EditText android:id="@+id/edtPassword" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:hint="Password"
android:drawableLeft="@drawable/ic_pwd" android:inputType="textPassword" style="@style/txt_white" />
<View android:background="@color/gray"
android:layout_width="fill_parent" android:layout_height="@dimen/divider" />
<View android:background="@color/gray"
android:layout_width="fill_parent" android:layout_height="@dimen/divider" android:layout_marginTop="@dimen/pad_16dp"
android:visibility="gone"/>
<EditText android:id="@+id/edtEmailUnused" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:hint="Email address"
android:drawableLeft="@drawable/ic_pwd" android:inputType="textEmailAddress" style="@style/txt_white"
android:visibility="gone"/>
<info.hoang8f.widget.FButton
android:layout_width="fill_parent" android:layout_height="50dp"
android:id="@+id/btnLogin"
android:textColor="@color/white"
fbutton:buttonColor="@color/picton_blue"
fbutton:shadowColor="@color/san_marino"
fbutton:shadowEnabled="true"
fbutton:shadowHeight="2dp"
fbutton:cornerRadius="2dp"
android:text="Login"
android:layout_marginBottom="@dimen/pad_10dp"
android:layout_marginTop="@dimen/pad_40dp"
android:padding="@dimen/pad_15dp"
/>
<info.hoang8f.widget.FButton
android:layout_width="fill_parent" android:layout_height="50dp"
android:id="@+id/btnRegister"
android:textColor="@color/white"
fbutton:buttonColor="@color/niagara"
fbutton:shadowColor="@color/san_marino"
fbutton:shadowEnabled="true"
fbutton:shadowHeight="2dp"
fbutton:cornerRadius="2dp"
android:text="Register"
android:layout_marginBottom="@dimen/pad_40dp"
android:layout_marginTop="@dimen/pad_5dp"
android:padding="@dimen/pad_15dp"
android:onClick="gotoHome"
/>
<Button android:id="@+id/btnFb" android:background="@drawable/btn_fb"
android:layout_width="fill_parent" android:layout_height="wrap_content" style="@style/btn" android:visibility="gone"/>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:id="@+id/rlProgress"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white_45_percent"
android:clickable="true"
android:animateLayoutChanges="true"
android:visibility="gone"
xmlns:wheel="http://schemas.android.com/apk/res-auto">
<com.pnikosis.materialishprogress.ProgressWheel
android:id="@+id/progress_wheel"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
wheel:matProg_barColor="#555588FF"
wheel:matProg_progressIndeterminate="true" />
</RelativeLayout>
堆栈跟踪
java.lang.RuntimeException: Unable to start activity ComponentInfo{lifemedico.array.com.lifemedico/lifemedico.array.com.lifemedico.ui.login.Login}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at lifemedico.array.com.lifemedico.ui.login.Login.onCreate(Login.java:52)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
... 11 more
java.lang.NullPointerException
at lifemedico.array.com.lifemedico.ui.login.Login.onCreate(Login.java:52)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
在 lollipop 和 kitkat 上没有任何问题,但调试语句都在 jelly bean 中打印 null。想不出任何失败的原因,非常感谢任何帮助。
谢谢
编辑:
向 butterknife 添加调试选项给了我这个 未找到。尝试超级class android.support.v7.app.ActionBarActivityd 小姐:已达到框架 class。放弃搜索。 一些网上搜索指向混淆器,但我没有使用混淆器,这仍然是一个调试版本。
请填写您的字段 public 即:
@Bind(R.id.btnLogin)
public FButton btnLogin;
@Bind(R.id.btnRegister)
public FButton btnRegister;
@Bind(R.id.edtEmail)
public EditText edtEmail;
@Bind(R.id.edtPassword)
public EditText edtPassword;
@Bind(R.id.rlProgress)
public RelativeLayout rlProgress;
Butterknife 不能绑定私有变量,所以要public
我猜我看错洞了;没有 programming/syntax/logic 错误。正如我提到的,我查看了混淆器并意识到我的 gradle 有 multiDexEnabled;出于某种原因,Butterknife 函数被删除了。我所要做的就是将我的应用程序设置为扩展 MultiDexApplication,它就像一个魅力;花了我一天的时间才弄明白。
public class App extends MultiDexApplication {
我在这个 post https://github.com/JakeWharton/butterknife/issues/311
中找到了线索这个问题可以通过更新你的 gradle 文件来解决,正如 Butterknife 在他们的 gitgub
上提到的buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
apply plugin: 'com.neenbedankt.android-apt'
dependencies {
compile 'com.jakewharton:butterknife:8.0.1'
apt 'com.jakewharton:butterknife-compiler:8.0.1'
}
具体参考可以参考linkhttps://github.com/JakeWharton/butterknife