未找到RecyclerView和工具栏

时间:2017-03-15 23:34:36

标签: android android-recyclerview toolbar

我遇到了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:无法启动活动   ComponentInfo {com.example.usuari.myapplication3 / com.example.usuari.myapplication3.ViatgeDetall}:   java.lang.NullPointerException:尝试调用虚方法'void   android.support.v7.app.ActionBar.setTitle(java.lang.CharSequence)'on   空对象引用                                                                                          在   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)                                                                                          at java.lang.reflect.Method.invoke(Native Method)                                                                                          在   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)'on   空对象引用                                                                                          在   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)                                                                                          at java.lang.reflect.Method.invoke(Native Method)                                                                                          在   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>

2 个答案:

答案 0 :(得分:1)

错误发生在:

Ractive.DEBUG = false;

var data = [{
  type: "Person",
  Name: "John",
  worksFor: [
    "1",
    "2"
  ]
}, {
  type: "department",
  id: "1",
  Name: "Sales"
}, {
  type: "department",
  id: "2",
  Name: "Marketing"
}, {
  type: "department",
  id: "3",
  Name: "Accounting"
}];

new Ractive({
  el: '#list',
  template: '#template',
  data: {
    selectedDepartments: [],
    myData: data
  },
  computed: {
    DepartmentList() {
      return this.get('myData').filter(v => v.type === 'department');
    },
    PersonsList() {
      return this.get('myData').filter(v => v.type === 'Person');
    }
  }
});

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

答案 1 :(得分:1)

您必须添加正确的布局文件我认为您放置了错误的布局文件名

setContentView(R.layout.activity_main);