Issue trying to use Recycler View - java

i'm trying to follow a tutorial for RecyclerView.
But when i finish and try to test my code i got this message:
09-18 16:01:03.486 2521-2521/com.example.gon.recycleview E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.gon.recycleview, PID: 2521
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gon.recycleview/com.example.gon.recycleview.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
at com.example.gon.recycleview.MainActivity.onCreate(MainActivity.java:22)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
I am a begginer and i don't understand this message. It seems that i have an error on this code with my setHasFixedSize() function
public class MainActivity extends Activity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private String[] myDataset = {"Lundi", "Mardi", "Mercredi", "Vendredi", "Samedi", "Dimanche"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recycler_view);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager=new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter=new RecyclerViewAdapter(myDataset);
mRecyclerView.setAdapter(mAdapter);
}
}
can you help me please to understand what's wrong.
my_recycler_view is my view which contain this :
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android.id="#+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
and I have an other view which describe lines of my list. And finally I have a class named RecyclerViewAdapter.

In your XML layout, you have this line:
android.id="#+id/my_recycler_view"
change it to this instead:
android:id="#+id/my_recycler_view"
Because you've written android.id instead of android:id, the view isn't getting an id. Since it doesn't have an id, findViewById() can't find it, so it returns null (leading to your error).

Related

attempt to invoke virtual method on null object reference while using Recycler View

I am beginner app dev trying to create chat app. The app starts smoothly but when I want to display all the registered users in a Recycler View app crashes . It doesnt show any error still the app crashes and when commented out the error just goes to the next line. Any help would be much appreciated.
Logcat
2020-11-23 10:38:39.697 4907-4907/com.example.decide_o E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.decide_o, PID: 4907
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.decide_o/com.example.decide_o.AllUsers}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
at com.example.decide_o.AllUsers.onCreate(AllUsers.java:38)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6541) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
All Users
public class AllUsers extends AppCompatActivity {
private RecyclerView nuserlist;
private DatabaseReference nUserDatabase;
private LinearLayoutManager nlayoutmanager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
nUserDatabase=FirebaseDatabase.getInstance().getReference().child("Users");
nlayoutmanager=new LinearLayoutManager(this);
nuserlist = (RecyclerView) findViewById(R.id.userlist);
nuserlist.setHasFixedSize(true);
nuserlist.setLayoutManager(nlayoutmanager);
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Users,UsersViewHolder> firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Users, UsersViewHolder>(
Users.class,
R.layout.single_users,
UsersViewHolder.class,
nUserDatabase
) {
#Override
protected void populateViewHolder(UsersViewHolder usersViewHolder, Users users, int i) {
usersViewHolder.setName(users.getName());
}
};
nuserlist.setAdapter(firebaseRecyclerAdapter);
}
public static class UsersViewHolder extends RecyclerView.ViewHolder{
View nView;
public UsersViewHolder(#NonNull View itemView) {
super(itemView);
nView=itemView;
}
public void setName(String name) {
TextView nameview=(TextView)nView.findViewById(R.id.single_username);
nameview.setText(name);
}
}
}
xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AllUsers">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/userlist"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
the issue here is you haven't set your content view.
Add this line in you onCreate method :-
setContentView(R.layout.your_activity_xml);
after your super.oncreate is called.
If you don't assign your content view then it won't be identified that from where to fetch your recyclerView :- nuserlist = (RecyclerView) findViewById(R.id.userlist);.

Getting "on a null object reference" [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
So i'm getting " on a null object reference" on my l1 and have no idea how to fix it after googling. I've tried multiple coding discords but they say it's because of the depreciated code but when I googled it shows that the depreciated code still works and the youtube tutorial I was following for it worked fine .
MainActivity -
package com.getcontacts;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class MainActivity extends AppCompatActivity {
ListView l1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l1 = findViewById(R.id.listView);
}
public void get(View v) {
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
startManagingCursor(cursor);
String[] from = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone._ID};
int[] to = {android.R.id.text1,android.R.id.text2};
SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,cursor,from,to,1);
l1.setAdapter(simpleCursorAdapter);
l1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
}
and this is the error -
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.getcontacts, PID: 599
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:5205)
at android.view.View$PerformClick.run(View.java:21164)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:5205) 
at android.view.View$PerformClick.run(View.java:21164) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.getcontacts.MainActivity.get(MainActivity.java:42)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
at android.view.View.performClick(View.java:5205) 
at android.view.View$PerformClick.run(View.java:21164) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 
apparently this : findViewById(R.id.listView); returns null please double check your tags and wether this element actually exists at the time of the call
Check the l1 you are trying to reference in your layout file to ensure you provided the id and spelled it exactly as you are calling it in the Java.

Android: Can't set an image once a button is clicked (NullpointerException/IllegalStateException)

I'm getting an IllegalStateException and a NullPointerException once the imageview is clicked (App crashes). It is supposed to pass the imageview's image to another imageview. I am also using a fragment for my tabbed activity.
onCreate() and variables
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
PhotoViewAttacher xAttach;
ImageView tessst;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_whole_details);
tessst = (ImageView) findViewById(R.id.fillZoom);
what = (ImageView) findViewById(R.id.img1);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
the Imageview click class
public void clickZoom(View v){
if(v.getId() == R.id.img1){
Drawable bb = getResources().getDrawable(R.drawable.maincolor_btn);
tessst.setImageDrawable(bb);
xAttach = new PhotoViewAttacher(tessst);
}
}
the logcat
09-06 13:12:45.436 31388-31388/com.example.testapp.coloranalysis E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.testapp.coloranalysis, PID: 31388
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4463)
at android.view.View$PerformClick.run(View.java:18770)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5323)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4463) 
at android.view.View$PerformClick.run(View.java:18770) 
at android.os.Handler.handleCallback(Handler.java:808) 
at android.os.Handler.dispatchMessage(Handler.java:103) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:5323) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException
at com.example.testapp.coloranalysis.WholeDetails.clickZoom(WholeDetails.java:59)
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:4463) 
at android.view.View$PerformClick.run(View.java:18770) 
at android.os.Handler.handleCallback(Handler.java:808) 
at android.os.Handler.dispatchMessage(Handler.java:103) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:5323) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 
at dalvik.system.NativeStart.main(Native Method) 
maybe tessst is null.
and thus R.id.fillZoom may not in R.layout.activity_whole_details
you can move tessst = (ImageView) findViewById(R.id.fillZoom); to the onCreateView in fragment, of course codes associated with tessst also have to move.
I assume you are adding the onClick listener in XML, with the attribute android: onClick="clickZoom". Now to do that, the clickZoom function, must be part of the Activity class, and it sounds like it is not. What you have to do, is remove the XML attribute, and set the onClick listener in code, by having your class implement View.OnClickListener interface, and calling setOnClickListener on the view.

Android findViewById() returning null for custom view

So I've checked all of the other threads on this topic repeatedly but none have yielded the solution. Here's the code:
The XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.turingtechnologies.materialscrollbar.DragScrollBar
android:layout_width="wrap_content"
android:id="#+id/dragScrollBar"
android:layout_height="match_parent"/>
</RelativeLayout>
The Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Stuff
recyclerView = (RecylerView)findViewById(R.id.recyclerView); //I've verified that this works properly.
DragScrollBar dragScrollBar = (DragScrollBar)findViewById(R.id.dragScrollBar);
if (dragScrollBar == null) { Log.d("d", "fail"); }
dragScrollBar.bindRecyclerView(recyclerView);
}
DragScrollBar.java
public class DragScrollBar extends MaterialScrollBar<DragScrollBar>{
public DragScrollBar(Context context, AttributeSet attrs){
super(context, attrs);
}
}
MaterialScrollBar.java
abstract class MaterialScrollBar<T> extends RelativeLayout {
MaterialScrollBar(Context context, AttributeSet attrs){
super(context, attrs);
initialise(context);
}
//Stuff
}
It draws correctly on the preview but returns a nullPointerException when dragScrollBar is used and 'fail' is printed to the console.
I've omitted the other normal constructors but when included they don't make a difference.
EDIT: Here's the stack trace
fail
Shutting down VM
FATAL EXCEPTION: main
Process: com.turingtechnologies.materialscrollbardemo, PID: 31890 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.turingtechnologies.materialscrollbardemo/com.turingtechnologies.materialscrollbardemo.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object com.turingtechnologies.materialscrollbar.DragScrollBar.bindRecyclerView(android.support.v7.widget.RecyclerView)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object com.turingtechnologies.materialscrollbar.DragScrollBar.bindRecyclerView(android.support.v7.widget.RecyclerView)' on a null object reference
at com.turingtechnologies.materialscrollbardemo.MainActivity.onCreate(MainActivity.java:26)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 
I had run setID() inside of initalise as part of an earlier scheme and hadn't thought to remove it during refactoring.

settext force close when trying to display text in textview

This is the Main java file,i was trying to use the settext method but everytime i use it it gives me force close
public class SecondActivity extends Activity {
TextView myAwesomeTextView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myAwesomeTextView = (TextView) findViewById(R.id.myAwesomeTextView);
myAwesomeTextView.setText("My Awesome Text");
}
}
XML
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/myAwesomeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="s"
android:textSize="20sp"
android:editable="true"/>
</LinearLayout>
this is my first app so i dont know much about android i searched alot but i coudnt fix it hence i asked the best group.thankyou
Logcat
12-13 12:21:37.946 29196-29196/eu.itcsolutions.android.tutorial D/AndroidRuntime﹕ Shutting down VM
12-13 12:21:37.947 29196-29196/eu.itcsolutions.android.tutorial E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: eu.itcsolutions.android.tutorial, PID: 29196
java.lang.RuntimeException: Unable to start activity ComponentInfo{eu.itcsolutions.android.tutorial/eu.itcsolutions.android.tutorial.SecondActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at eu.itcsolutions.android.tutorial.SecondActivity.onCreate(SecondActivity.java:47)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
12-13 12:21:39.658 29196-29196/eu.itcsolutions.android.tutorial I/Process﹕ Sending signal. PID: 29196 SIG: 9
You need to use setContentView(R.layout.second) instead of setContentView(R.layout.main).
public class SecondActivity extends Activity {
TextView myAwesomeTextView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
myAwesomeTextView = (TextView) findViewById(R.id.myAwesomeTextView);
myAwesomeTextView.setText("My Awesome Text");
}
}

Categories