android 4.2中的Butterknife空指针异常
在我当前的项目中使用Butterknife;它在Kitkat和棒棒糖上运行良好,但在jellybean(4.2)上抛出一个nullpointer异常 在此行引发异常(btnLogin为NULL) 设置字体(FontsHelper.robotoRegular(this)) 活动代码android 4.2中的Butterknife空指针异常,android,android-studio,butterknife,Android,Android Studio,Butterknife,在我当前的项目中使用Butterknife;它在Kitkat和棒棒糖上运行良好,但在jellybean(4.2)上抛出一个nullpointer异常 在此行引发异常(btnLogin为NULL) 设置字体(FontsHelper.robotoRegular(this)) 活动代码 public class Login extends Activity { @Bind(R.id.btnLogin) FButton btnLogin; @Bind(R.id.btnRegis
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>
在棒棒糖和kitkat上没有任何问题,但是调试语句都在jelly bean中打印null。想不出任何失败的原因,我们非常感谢您的帮助
谢谢
编辑:
向butterknife添加调试选项给了我这个
没有找到。正在尝试超类android.support.v7.app.ActionBarActivityd
小姐:到达了框架类。放弃搜索。
一些在线搜索指向proguard,但我没有使用proguard,这仍然是一个调试版本。请公开您的字段,即:
@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无法绑定私有变量,因此将其公开我想我找错了方向;没有编程/语法/逻辑错误。正如我所提到的,我调查了proguard,发现我的gradle已经启用了多索引功能;出于某种原因,巴特刀的功能被剥离了。我所要做的就是将我的应用程序设置为扩展Multidex应用程序,它就像一个魅力;我花了一天时间才弄明白
public class App extends MultiDexApplication {
我在这篇文章中找到了线索这个问题可以通过更新你的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'
}
要获得准确的参考,您可以参考link嘿,老板,我正在使用android studio。抛出的异常是哪一行?更新了问题;异常在btnLogin.setTypeface(FontsHelper.robotregular(this))抛出。btnLogin为空。您可以发布日志猫中得到的堆栈跟踪吗?为什么不使用Log.d或Log.e Log.i等来记录而不是system.out,printlnu shd打印stacktrace..我知道这是无效的,但可能是jelly bean仍然使用injectview..你可以尝试..怀疑kabindra是这样的,它对kitkat和棒棒糖有效。为了你的建议,我试过了,但还是很高兴它也帮助了其他人:)为我工作……谢谢@Veeru……但我仍然不明白为什么会发生。。。
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'
}