-1
我一直有一個奇怪的問題,我嘗試創建一個與我總是一樣的textView,但現在當我初始化textView和setText時,我得到一個空指針例外,並不能找出我做錯了什麼。當嘗試setText(Android工作室)時出現空指針異常
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView test = (TextView) findViewById(R.id.randomID);
test.setText("sample text");
}
XML
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/Constraint"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="364dp"
android:layout_height="223dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/cityID"
app:layout_constraintVertical_bias="0.006">
<ImageView
android:id="@+id/weatherImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/celsius"
app:layout_constraintTop_toBottomOf="@+id/loadingLocation"
app:layout_constraintVertical_bias="0.555"
app:srcCompat="@drawable/thunder_cloud_rain"/>
</RelativeLayout>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/settingsToolbar"
android:titleTextColor="@color/white"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:background="@color/colorPrimary"
android:minHeight="?android:attr/actionBarSize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:theme="@style/ToolBarStyle"/>
<TextView
android:id="@+id/celsius"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="45sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/settingsToolbar"
app:layout_constraintVertical_bias="0.035"
tools:text="96"/>
<TextView
android:id="@+id/skyForecast"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="7dp"
android:gravity="center"
android:includeFontPadding="false"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:text="Scattered Clouds"
app:layout_constraintHorizontal_bias="1.0"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/celsius"
app:layout_constraintVertical_bias="0.0"/>
<TextView
android:id="@+id/cityID"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="4dp"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/skyForecast"
app:layout_constraintVertical_bias="0.005"
tools:text="Redmond, Washington"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"/>
<TextView
android:id="@+id/currentTime"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/skyForecast"
app:layout_constraintVertical_bias="0.554"
tools:text="Monday, 10:30 am"/>
<TextView
android:id="@+id/sunrise"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.057"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/skyForecast"
app:layout_constraintVertical_bias="0.854"
tools:text="6:30 am"/>
<TextView
android:id="@+id/windSpeed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintVertical_bias="0.856"
tools:text="10 m/s"
app:layout_constraintTop_toTopOf="@+id/sunrise"
tools:layout_editor_absoluteX="159dp"/>
<TextView
android:id="@+id/randomID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="18sp"
tools:text="10 m/s"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/currentTime"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toRightOf="@+id/windSpeed"
android:layout_marginLeft="8dp"
app:layout_constraintHorizontal_bias="0.284"
app:layout_constraintVertical_bias="0.649"/>
<TextView
android:id="@+id/sunset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.059"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/sunrise"
app:layout_constraintVertical_bias="0.0"
tools:text="7:30 pm"/>
<ProgressBar
android:id="@+id/loadingLocation"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/settingsToolbar"
app:layout_constraintVertical_bias="0.438"
tools:visibility="visible"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"/>
<View
android:id="@+id/view"
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_marginBottom="8dp"
android:background="@color/colorPrimaryDark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/settingsToolbar"
app:layout_constraintVertical_bias="0.0"/>
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:srcCompat="@drawable/ic_wb_sunny_black_24dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/currentTime"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintHorizontal_bias="0.107"
app:layout_constraintVertical_bias="0.397"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"/>
<ImageView
android:id="@+id/imageView2"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/currentTime"
app:layout_constraintVertical_bias="0.375"
app:srcCompat="@drawable/wind_512"
tools:layout_editor_absoluteX="173dp"/>
我試圖重建項目,unistalling應用,清潔工程,建設APK,我非常困惑什麼,我做錯了。
你究竟在哪裏調用findVIewById(),分享更多的代碼,請 – Yarik
對不起,我應該補充一點。我更新了帖子 –
你確定在R.layout.activity_main裏聲明瞭randomID嗎? – Yarik