Home > other >  Android TextView next to ImageView set text programmatically causing a null when referencing
Android TextView next to ImageView set text programmatically causing a null when referencing

Time:07-31

Hey all I am trying to figure out how to put text into a TextView that's beside a ImageView so that I can label each image as this section of code does:

tabLayout = findViewById(R.id.mTabLayout);
viewPager = findViewById(R.id.viewPager);
//adapter setup
pagerAdapter = new com.telluridetainment.PagerAdapter(getSupportFragmentManager());
pagerAdapter.addFragment(new com.telluridetainment.FragMovies(), "Movies");
pagerAdapter.addFragment(new com.telluridetainment.FragShows(), "Shows");
pagerAdapter.addFragment(new com.telluridetainment.FragMusic(), "Music");
pagerAdapter.addFragment(new com.telluridetainment.FragAndroid(), "Android")
pagerAdapter.addFragment(new com.telluridetainment.FragSNES(), "game1");
pagerAdapter.addFragment(new com.telluridetainment.FragNES(), "game2");

if (GlobalCls.theUser.equals("jayden")) {
    pagerAdapter.addFragment(new com.telluridetainment.FragUser(), "Jayden");
} else {
    pagerAdapter.addFragment(new com.telluridetainment.FragUser(), "Tabby");
}

viewPager.setOffscreenPageLimit(7);
viewPager.setAdapter(pagerAdapter);
tabLayout.setupWithViewPager(viewPager);

for (int i = 0; i < tabLayout.getTabCount(); i  ) {
     TabLayout.Tab tab = tabLayout.getTabAt(i);
     //TextView t = findViewById(R.id.tabText);

     switch(i){
         case 0: if (tab != null){
              tab.setIcon(R.drawable.movies);
              //t.setText("Movies");
         }break;
         case 1: if (tab != null){
              tab.setIcon(R.drawable.shows);
         }break;
         case 2: if (tab != null){
              tab.setIcon(R.drawable.music);
         }break;
         case 3: if (tab != null){
              tab.setIcon(R.drawable.android);
         }break;
         case 4: if (tab != null){
              tab.setIcon(R.drawable.nes);
         }break;
         case 5: if (tab != null){
              tab.setIcon(R.drawable.snes);
         }break;
         case 6: if (tab != null){
              tab.setIcon(R.drawable.jayden);
         }break;
     }

     if (tab != null) tab.setCustomView(R.layout.my_custom_tab);
}

And the XML layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@android:id/icon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginStart="13dp" />

    <TextView
        android:id="@ id/tabText"
        style="@style/MyCustomTabText"
        android:layout_width="92dp"
        android:layout_height="50dp"
        android:text="hi"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginStart="72dp"
        android:maxHeight="20dp"
        android:textColor="@color/white" />
</RelativeLayout>

The code works just fine. It places the image(s) where I need them with the text (currently only static text) beside it.

enter image description here

However, when I try the code

t.setText("Movies");

I know it finds the TextView because it has it's number when I hover over it:

enter image description here

But like I said above once it gets to setting the text it's 'null':

enter image description here

Not sure what it is I'm doing incorrectly to cause it to be null?

UPDATE 1

Here is the error log that was requested:

D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.tridetainment, PID: 15100
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tridetainment/com.tridetainment.MainActivity}: 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:3270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.tridetainment.MainActivity.lambda$startLoading$3$com-tridetainment-MainActivity(MainActivity.java:213)
        at com.tridetainment.MainActivity$$ExternalSyntheticLambda3.run(Unknown Source:2)
        at android.app.Activity.runOnUiThread(Activity.java:6904)
        at com.tridetainment.MainActivity.startLoading(MainActivity.java:178)
        at com.tridetainment.MainActivity.onCreate(MainActivity.java:310)
        at android.app.Activity.performCreate(Activity.java:7802)
        at android.app.Activity.performCreate(Activity.java:7791)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
I/Process: Sending signal. PID: 15100 SIG: 9
Disconnected from the target VM, address: 'localhost:41902', transport: 'socket'

UPDATE 2

I have added more code to the above to show the relationship(s).

CodePudding user response:

findviewbyid is returning null probably because you're not calling it on the parent of the view you are looking for. if tab is the parent of the textview then replace findViewById(R.id.tabText) with tab.getCustomView().findViewById(R.id.tabText).

for (int i = 0; i < tabLayout.getTabCount(); i  ) {
     TabLayout.Tab tab = tabLayout.getTabAt(i);
     TextView t = (TextView)tab.getCustomView().findViewById(R.id.tabText);

CodePudding user response:

You need to provide more code because there could be something else that could be causing the error. Kindly provide the code of the whole activity if you can.

Either way, there can be a few possibilities that are causing this error :

  1. You put setContentView(); after declaring your textView. You can check out this answer.

  2. You could be trying to access fragment's view from activity or facing a similar problem in which case I would suggest you look at this answer.

In any case, I think this error would have been caused by a silly mistake. If any of these doesn't help, consider providing more code so that we can help you.

  • Related