Reputation: 323
I'm having problems with a recyclerView. This is the definition in the 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>
I try to use it in the following class:
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);
}
}
I don't see what i'm doing worng, beacuse i keep getting this error :
FATAL EXCEPTION: main Process: com.example.usuari.myapplication3, PID: 26507 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.usuari.myapplication3/com.example.usuari.myapplication3.ViatgeDetall}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) at android.app.ActivityThread.access$1100(ActivityThread.java:222) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.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) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference at com.example.usuari.myapplication3.ViatgeDetall.onCreate(ViatgeDetall.java:37) at android.app.Activity.performCreate(Activity.java:6876) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) at android.app.ActivityThread.access$1100(ActivityThread.java:222) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.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)
Even if i comment the part about the toolbar i get a very similar error but with the RecyclerView. I'm working with sdk 25 if that helps...
This is my manifest:
<?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>
Upvotes: 0
Views: 90
Reputation: 323
The error was in :
setContentView(R.layout.activity_main);
That wasn't the correct layout to set.Instead it should had been the layout of the prevoiusly shown xml...
Upvotes: 1
Reputation: 763
You have to add correct layout file I think you put incorrect layout file name
setContentView(R.layout.activity_main);
Upvotes: 1