簡體   English   中英

Android textView.setText() 拋出異常

[英]Android textView.setText() throws exception

我正在嘗試創建一個應用程序,我可以在LinearLayout多次放置相同類型的布局。 布局只有一個TextView ,我正在嘗試使用setText但這行代碼似乎不起作用。

我不知道我必須向LinearLayout添加多少布局,因為這取決於用戶將上傳到應用程序的無人機數量(我有一個布局/無人機),所以我制作了一個讀取文本的函數以放入來自SharedPreferenceTextView並且還讀取另一個SharedPreferences的無人機數量(因此布局數量)。

我將字符串放入的TextView來自名為dronelayoutmain 的布局。 我嘗試在setText只發送“測試”,但它還是崩潰了,所以我認為 android 找不到我的TextView ,但我真的不知道為什么。

所以這是我在OnCreate中調用的函數,它應該為我擁有的每架無人機添加一個布局

    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);


        }
    }
}

這是我試圖放入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>

該函數應該在 applimaindata 中檢查要添加的布局數量,然后在 for() 中,應該從droneprefs 文件中設置TextView的文本,然后將布局添加到主LinearLayout

如果這個TextView屬於新膨脹的布局,你必須在布局膨脹后找到它:

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM