简体   繁体   中英

Android textView.setText() throws exception

I'm trying to create an app where I can put several times the same kind of layout in a LinearLayout . The layout just has a TextView and i'm trying to use setText but this line of code doesn't seems to work.

I don't know how many layout I must add to the LinearLayout because it depends of how many drones (I have one layout/drone) the user will have upload to the app, so I made a function that read the text to put in the TextView from a SharedPreference and that also read the number of drones (so the number of layouts) in another SharedPreferences .

The TextView i'im putting the string in is from a layout named dronelayoutmain. I tried sending just "test" in the setText , but it crashes anyway, so I think that android just can't find my TextView , but I don't really know why.

So here is the function I call in OnCreate that is supposed to add one layout for each drone I have

    public void affichagedrone(){
    LinearLayout mlineairelayout = (LinearLayout) 
    findViewById(R.id.lineaireLayout);
    LinearLayout.LayoutParams params = new 
    LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
    LinearLayout.LayoutParams.WRAP_CONTENT);
    Context mContext;
    mContext = getApplicationContext();
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    SharedPreferences droneprefs = mContext.getSharedPreferences("dronepref", MODE_PRIVATE);
    SharedPreferences applimaindata = mContext.getSharedPreferences("applimaindata", MODE_PRIVATE);
    int nbdrone = applimaindata.getInt(String.valueOf(emplacementNbDrone),1);

    String string;

    TextView textViewnomConstructeurDrone;

    if (nbdrone !=0) {
        for(int i=0;i<nbdrone;i++) {
            textViewnomConstructeurDrone = (TextView) findViewById(R.id.textViewNomConstructeur);

            string = droneprefs.getString(String.valueOf(i),"Fail");
            textViewnomConstructeurDrone.setText(string);

            final View rowView = inflater.inflate(R.layout.dronelayoutmain, null);
            // Add the new row before the add field button.
            mlineairelayout.addView(rowView);


        }
    }
}

And here is the layout i'm trying to put in the LinearLayout .

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="120dp"
>

<TextView
    android:id="@+id/textViewNomConstructeur"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="5dp"
    android:text="@string/adddrone"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

The function is supposed to check in applimaindata the number of layout to add, and then, in the for(), is supposed to set the Text of the TextView from the droneprefs file and then add the Layout to the main LinearLayout

If this TextView belongs in the newly inflated layout, you must find it after the layout is inflated:

for(int i = 0; i < nbdrone; i++) {
    final View rowView = inflater.inflate(R.layout.dronelayoutmain, null);
    textViewnomConstructeurDrone = rowView.findViewById(R.id.textViewNomConstructeur);
    string = droneprefs.getString(String.valueOf(i),"Fail");
    textViewnomConstructeurDrone.setText(string);
    mlineairelayout.addView(rowView);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM