Butter Knife @bind in Activity错误

时间:2017-03-08 17:38:18

标签: android

执行活动后的应用程序错误。

  

错误:D / AndroidRuntime:关闭VM E / AndroidRuntime:致命   例外:主要                     处理:activity.rama.navigationtaskbartraining,PID:9385                     java.lang.RuntimeException:无法启动活动ComponentInfo {activity.rama.navigationtaskbartraining / activity.rama.navigationtaskbartraining.FoldableActivity}:   java.lang.RuntimeException:无法绑定视图   activity.rama.navigationtaskbartraining.FoldableActivity                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)                         在android.app.ActivityThread.-wrap12(ActivityThread.java)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460)                         在android.os.Handler.dispatchMessage(Handler.java:102)                         在android.os.Looper.loop(Looper.java:154)                         在android.app.ActivityThread.main(ActivityThread.java:6077)                         at java.lang.reflect.Method.invoke(Native Method)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)                      引起:java.lang.RuntimeException:无法绑定视图   activity.rama.navigationtaskbartraining.FoldableActivity                         在butterknife.ButterKnife.bind(ButterKnife.java:322)                         在butterknife.ButterKnife.bind(ButterKnife.java:237)                         at activity.rama.navigationtaskbartraining.FoldableActivity.onCreate(FoldableActivity.java:55)                         在android.app.Activity.performCreate(Activity.java:6662)                         在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)                         在android.app.ActivityThread.-wrap12(ActivityThread.java)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460)                         在android.os.Handler.dispatchMessage(Handler.java:102)                         在android.os.Looper.loop(Looper.java:154)                         在android.app.ActivityThread.main(ActivityThread.java:6077)                         at java.lang.reflect.Method.invoke(Native Method)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)                      引起:java.lang.IllegalStateException:对于字段ID为2131624097的必需视图'recycler_view'   找不到'mRecyclerView'。如果此视图是可选的添加   '@Nullable'注释。                         在butterknife.ButterKnife $ Finder.findRequiredView(ButterKnife.java:140)                         at activity.rama.navigationtaskbartraining.FoldableActivity $$ ViewBinder.bind(FoldableActivity $$ ViewBinder.java:11)                         at activity.rama.navigationtaskbartraining.FoldableActivity $$ ViewBinder.bind(FoldableActivity $$ ViewBinder.java:8)                         在butterknife.ButterKnife.bind(ButterKnife.java:319)                         在butterknife.ButterKnife.bind(ButterKnife.java:237)                         at activity.rama.navigationtaskbartraining.FoldableActivity.onCreate(FoldableActivity.java:55)                         在android.app.Activity.performCreate(Activity.java:6662)                         在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)                         在android.app.ActivityThread.-wrap12(ActivityThread.java)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460)                         在android.os.Handler.dispatchMessage(Handler.java:102)                         在android.os.Looper.loop(Looper.java:154)                         在android.app.ActivityThread.main(ActivityThread.java:6077)                         at java.lang.reflect.Method.invoke(Native Method)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

/*
 * Copyright 2015 Worldline.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package activity.rama.navigationtaskbartraining;

import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;

import java.io.IOException;

import butterknife.Bind;
import butterknife.ButterKnife;

public class FoldableActivity extends AppCompatActivity {

    @Bind(R.id.recycler_view)
    protected RecyclerView mRecyclerView;

    @Bind(R.id.toolbar)
    protected Toolbar mToolbar;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.foldable_layout);
        ButterKnife.bind(this);
        setSupportActionBar(mToolbar);

        String[] dataSet = null;
        try {
            dataSet = getAssets().list("demo-pictures");
        } catch (IOException e) {
            e.printStackTrace();
        }
        PhotoAdapter adapter = new PhotoAdapter(dataSet, this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = getResources().getDimensionPixelSize(R.dimen.activity_vertical_margin);
            }
        });
        mRecyclerView.setAdapter(adapter);
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    public Action getIndexApiAction() {
        Thing object = new Thing.Builder()
                .setName("Foldable Page") // TODO: Define a title for the content shown.
                // TODO: Make sure this auto-generated URL is correct.
                .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
                .build();
        return new Action.Builder(Action.TYPE_VIEW)
                .setObject(object)
                .setActionStatus(Action.STATUS_TYPE_COMPLETED)
                .build();
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        AppIndex.AppIndexApi.start(client, getIndexApiAction());
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        AppIndex.AppIndexApi.end(client, getIndexApiAction());
        client.disconnect();
    }
}

2 个答案:

答案 0 :(得分:0)

使用@BindView(),@绑定是旧注释。你用的是什么版本的奶油刀?应该像

compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'

话虽如此,ButterKnife告诉你的是

  

必填视图' recycler_view'字段ID为2131624097   ' mRecyclerView'没找到。

这意味着您的R.layout.foldable_layout不包含ID为R.id.recycler_view的RecyclerView小部件。

答案 1 :(得分:0)

我之所以回答这个问题,是因为也许有些人会因为做出简单的错误而看到此错误,并通过我的回答轻松解决,尽管在发布问题的时间和现在之间存在巨大的时间间隔。

可能您在视图R.id.recycler_view中忘记或打错了文字。

java.lang.IllegalStateException:具有ID的必需视图“ recycler_view” 找不到字段“ mRecyclerView”的2131624097。

此错误行显示了导致此错误的组件。检查您的XML文件和此componenet的ID并进行修复。就这样。同样如Martin所说,在黄油刀的更新版本中建议使用@BindView()