2017-03-15 70 views
0

我遇到了recyclerView問題。 這是XML定義:找不到RecyclerView和工具欄

<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中)」上 空對象引用 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) 在android.app.ActivityThread.access $ 1100(ActivityThread.java:222) at android.app.ActivityThread $ H.handleMessage(ActivityThre在android.app.ActivityThread.main(ActivityThread。)中的android.os.Looper.loop(Looper.java:158) 上的android.os.Handler.dispatchMessage(Handler.java:102) (ad.java:1795) 。 java:7229) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal。 os.ZygoteInit.main(ZygoteInit.java:1120) 引起:java.lang.NullPointerException:嘗試調用虛擬方法'void 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) at 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> 
+1

您的清單中的活動是怎樣的? (和你的風格?),因爲如果你已經在你的佈局中,你不需要再次在你的代碼中設置它。 – stamanuel

+0

用我的清單編輯。 – Asdemuertes

回答

1

你必須添加正確的佈局文件我認爲你把不正確的佈局文件名

setContentView(R.layout.activity_main); 
1

錯誤是:

setContentView(R.layout.activity_main); 

這是不正確的佈局設置。相反,它應該是prevoiusly顯示xml的佈局...