Issue
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:
protected void onCreate(Bundle savedInstanceState) {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
GlobalCls.hideSystemUI(getWindow());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GlobalCls._loadGlobalCls(getApplicationContext(), MainActivity.this);
GlobalCls.database = openOrCreateDatabase(GlobalCls.dbName, Context.MODE_PRIVATE, null);
GlobalCls.createSQLTbl();
startLoading();
}
private void startLoading() {
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.FragGame2(), "game1");
pagerAdapter.addFragment(new com.telluridetainment.FragGame1(), "game2");
if (GlobalCls.theUser.equals("j")) {
pagerAdapter.addFragment(new com.telluridetainment.FragUser(), "j");
} else {
pagerAdapter.addFragment(new com.telluridetainment.FragUser(), "t");
}
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.game1);
}
break;
case 5:
if (tab != null) {
tab.setIcon(R.drawable.game2);
}
break;
case 6:
if (tab != null) {
tab.setIcon(R.drawable.j);
}
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.
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:
But like I said above once it gets to setting the text it's 'null':
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).
UPDATE 3
Updated the code to the full call.
UPDATE 4
This is the main_activty XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
android:layout_x="10dp"
android:layout_y="100dp"
android:id="@+id/mainPage"
tools:context=".MainActivity">
<com.tridetainment.noTabSwiping
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:tag="VP"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/mTabLayout"
tools:ignore="SpeakableTextPresentCheck" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/mTabLayout"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#F44336"
android:tag="TL"
android:elevation="0dp"
app:tabTextAppearance="@style/MyCustomTabText"
app:layout_constraintTop_toTopOf="parent"
app:tabGravity="fill"
app:tabIndicatorColor="@color/white"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/white"
app:tabIndicatorHeight="10dp"
app:tabTextColor="@color/grey"
app:tabInlineLabel="true"
tools:ignore="SpeakableTextPresentCheck" />
<View
android:layout_width="match_parent"
android:layout_height="15dp"
android:layout_below="@+id/mTabLayout"
android:layout_x="10dp"
android:layout_y="10dp"
android:background="@drawable/gui_tabshadow"
app:layout_constraintTop_toTopOf="@+id/viewPager" />
<FrameLayout
android:id="@+id/videoFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#122AFF"
android:tag="VF"
android:visibility="invisible">
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Solution
I guess this should be the problem then, you did not set the custom view for your tab layout and so it is giving the null pointer exception. This code should now work fine
viewPager.setOffscreenPageLimit(7);
viewPager.setAdapter(pagerAdapter);
tabLayout.setupWithViewPager(viewPager);
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
if (tab != null) tab.setCustomView(R.layout.my_custom_tab);
TextView t = findViewById(R.id.tabText);
// if it still gives error replace the above referencing with
// TextView t = (TextView)tab.view.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.game1);
}
break;
case 5:
if (tab != null) {
tab.setIcon(R.drawable.game2);
}
break;
case 6:
if (tab != null) {
tab.setIcon(R.drawable.j);
}
break;
}
}
Answered By - Arsh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.