【问题标题】:RecyclerView and Toolbar not found找不到 RecyclerView 和工具栏
【发布时间】:2017-03-15 23:34:36
【问题描述】:

我在使用 recyclerView 时遇到问题。 这是 xml 中的定义:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.Toolbar
        android:id="@+id/tlbMenuDetalle"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        />


    <android.support.v7.widget.RecyclerView
        android:id="@+id/rcyViatgesDetall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

我尝试在以下课程中使用它:

public class ViatgeDetall extends AppCompatActivity {
    private RecyclerView mRcyViatgesDetall;
    private AdapterViatgesDetall mAdapterViatgesDetall;
    private ViatgesManager.Viatge mViatge;

    private ArrayList<ViatgesManager.Viatge> viatges;

    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

      Toolbar myToolbar = (Toolbar) findViewById(R.id.tlbMenuDetalle);
       setSupportActionBar(myToolbar);
      getSupportActionBar().setTitle("TravelApp");
       getSupportActionBar().setHomeButtonEnabled(true);
       getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        Log.d("myTag", "Detalle OnCreate" );
        mRcyViatgesDetall = (RecyclerView) findViewById(R.id.rcyViatgesDetall);


        Bundle extras = getIntent().getExtras();
        if(extras != null)
        {
            viatges  = ViatgesManager.ITEMS;
            int f = getIntent().getIntExtra("viatge",0);
            mViatge = viatges.get(f-1);
            Log.d("myTag", "Detalle OnCreate" + mViatge.getName());
        }
        ArrayList<ViatgesManager.Stop> stops =  mViatge.getStops();


        LinearLayoutManager llm = new LinearLayoutManager(this);
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        GridLayoutManager glm = new GridLayoutManager(this,2);
        glm.setOrientation(GridLayoutManager.HORIZONTAL);
        mRcyViatgesDetall.setLayoutManager(llm);

        mAdapterViatgesDetall = new AdapterViatgesDetall(stops, this);
        mRcyViatgesDetall.setAdapter(mAdapterViatgesDetall);



    }
}

我看不到我在做什么,因为我一直收到这个错误:

致命异常:主要 进程:com.example.usuari.myapplication3,PID:26507 java.lang.RuntimeException:无法启动活动 组件信息{com.example.usuari.myapplication3/com.example.usuari.myapplication3.ViatgeDetall}: java.lang.NullPointerException:尝试调用虚拟方法'void android.support.v7.app.ActionBar.setTitle(java.lang.CharSequence)' 开启 空对象引用 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) 在 android.app.ActivityThread.access$1100(ActivityThread.java:222) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:158) 在 android.app.ActivityThread.main(ActivityThread.java:7229) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 引起:java.lang.NullPointerException:尝试调用虚拟 方法'无效 android.support.v7.app.ActionBar.setTitle(java.lang.CharSequence)' 开启 空对象引用 在 com.example.usuari.myapplication3.ViatgeDetall.onCreate(ViatgeDetall.java:37) 在 android.app.Activity.performCreate(Activity.java:6876) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) 在 android.app.ActivityThread.access$1100(ActivityThread.java:222) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:158) 在 android.app.ActivityThread.main(ActivityThread.java:7229) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

即使我评论有关工具栏的部分,我也会收到非常相似的错误,但使用 RecyclerView。如果有帮助,我正在使用 sdk 25...

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.usuari.myapplication3">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".EditViatge"
            android:label="EditVIatge"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ViatgeDetall"
            android:label="ViatgeDetall"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

【问题讨论】:

  • 清单中的活动看起来如何? (并且以您的风格?)因为如果您已经在布局中拥有它,则无需再次在代码中设置它。
  • 用我的清单编辑。

标签: android android-recyclerview toolbar


【解决方案1】:

你必须添加正确的布局文件我认为你输入了错误的布局文件名

setContentView(R.layout.activity_main);

【讨论】:

    【解决方案2】:

    错误出现在:

    setContentView(R.layout.activity_main);
    

    这不是要设置的正确布局。相反,它应该是之前显示的 xml 的布局...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-16
      • 2017-10-31
      相关资源
      最近更新 更多