android 4.2中的Butterknife Nullpointer异常

时间:2015-08-21 05:49:04

标签: android android-studio butterknife

在我目前的项目中使用Butterknife;它在Kitkat和棒棒糖上工作正常,但在软糖上引发了一个nullpointer异常(4.2)。

此行抛出异常(btnLogin为NULL)

btnLogin.setTypeface(FontsHelper.robotoRegular(本));

活动代码

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.access$600(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.access$600(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)

在棒棒糖和kitkat上没有任何问题,但调试语句在果冻豆中都打印为null。无法想到任何失败的原因,我们非常感谢任何帮助。

由于

编辑:

向butterknife添加调试选项给了我这个 未找到。尝试超类android.support.v7.app.ActionBarActivityd MISS:达到了框架类。放弃搜索。 一些在线搜索指向proguard,但我没有使用proguard,这仍然是一个调试版本。

3 个答案:

答案 0 :(得分:4)

猜猜我正在向下看错了洞;没有编程/语法/逻辑错误。正如我所提到的,我调查了proguard,并意识到我的gradle有 multiDexEnabled ;由于某种原因,Butterknife的功能被剥夺了。我所要做的就是设置我的应用程序以扩展MultiDexApplication,它就像一个魅力;我花了一天时间才弄明白。

public class App  extends MultiDexApplication {

我在这篇文章https://github.com/JakeWharton/butterknife/issues/311

中找到了领先者

答案 1 :(得分:1)

这个问题可以通过Butterknife在他们的gitgub上更新您的gradle文件来解决

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'
}

如需完整参考,请参阅链接https://github.com/JakeWharton/butterknife

答案 2 :(得分:0)

请公布您的字段,例如:

@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无法绑定私有变量,因此将其公开