I want to add an object to LinearLayout, but it throws an error
Error line: at me.solo_team.futureleader.ui.news.NewsFragment.onCreateView(NewsFragment.java:62)
( addElement(uris.get(i),names.get(i));)
NewsFragment
private LayoutInflater inflater;
private ViewGroup container;
private LinearLayout nw;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_news, container, false);
nw = root.findViewById(R.id.news_list);
this.inflater = inflater;
this.container = container;
List<String> uris = Arrays.asList(
// links...
);
List<String> names = Arrays.asList(
// names
);
for(int i =0;i<9;i++){
addElement(uris.get(i),names.get(i));
}
return root;
}
private void addElement(String uri,String name){
FrameLayout row = (FrameLayout) inflater.inflate(R.layout.news_news, container,false);
ConstraintLayout cn = row.findViewById(R.id.news_obj);
Picasso.get().load(uri).into((ImageView)cn.getChildAt(1));
((TextView)cn.getChildAt(2)).setText(name);
nw.addView(row);
}
fragment_news:
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 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=".ui.news.NewsFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/news_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent">
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
mews_news.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:id="@+id/news_obj"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_margin="15dp"
android:orientation="vertical"
app:contentPadding="10dp">
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gray_gradient_with_corners"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<ImageView
android:id="@+id/news_obj_image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/gray_gradient_with_corners"
android:src="@drawable/gray_gradient"
app:layout_constraintBottom_toTopOf="@id/news_obj_name"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" />
<TextView
android:id="@+id/news_obj_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginBottom="10dp"
android:autoSizeMaxTextSize="20sp"
android:autoSizeMinTextSize="12sp"
android:autoSizeStepGranularity="2sp"
android:autoSizeTextType="uniform"
android:text="news text"
android:textColor="@color/text_color1"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@id/news_obj"
app:layout_constraintLeft_toLeftOf="@id/news_obj" />
</androidx.constraintlayout.widget.ConstraintLayout>
ERROR:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: me.solo_team.futureleader, PID: 23516
java.lang.RuntimeException: Unable to start activity ComponentInfo{me.solo_team.futureleader/me.solo_team.futureleader.MainActivity}: android.view.InflateException: Binary XML file line #20: Binary XML file line #20: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.view.InflateException: Binary XML file line #20: Binary XML file line #20: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #20: Error inflating class fragment
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=8; index=8
at java.util.Arrays$ArrayList.get(Arrays.java:3769)
at me.solo_team.futureleader.ui.news.NewsFragment.onCreateView(NewsFragment.java:62)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1647)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3128)
at androidx.fragment.app.FragmentManager.dispatchViewCreated(FragmentManager.java:3065)
at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2988)
at androidx.fragment.app.FragmentStateManager.ensureInflatedView(FragmentStateManager.java:392)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:281)
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:140)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:319)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:298)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:706)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
at me.solo_team.futureleader.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
E/AndroidRuntime: at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2
Answers
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=8; index=8 at java.util.Arrays$ArrayList.get(Arrays.java:3769) at me.solo_team.futureleader.ui.news.NewsFragment.onCreateView(NewsFragment.java:62)
Here’s the real reason for the crash ^
It seems to me that your
tries to get an item that is out of bounds.
It should be i<8, because while your list seems to have 8 entries (length = 8 in the exception), they start at 0 (0,1,2…6,7 for a total of 8 entries), so i=8 and i=9 will be out of bounds.
You could also set i<uris.size(), that should also prevent crashing if the length of your uris and names lists are the same
Crashes of this type are often misleading, as you’re only seeing the most recent (or least recent) exception in LogCat.
Whenever I encounter a bug that causes an app crash, I INTENTIONALLY ignore the first crash and repeat the previous behavior to trigger a second crash. Then suddenly, LogCat reports the exception completely differently, typically pointed at the correct source of the error. Yes, it’s the very definition of insanity, as you’re repeating the same behavior, but getting different results. But this is Android. Insanity comes baked in.