我对android编程还很陌生,因此感谢您的帮助。
单击按钮时,我正在尝试将布尔值和字符串值存储到SharedPreferences中。存储后,它将更新mainActivty.xml页面。但是,经过许多尝试和数小时的查找,当尝试对字符串值进行.setText()时,我似乎找不到摆脱Null Pointer Exception的方法。任何帮助将不胜感激,谢谢
我尝试创建一个充气机,因为某些问题是由于视图没有充气而引起的。我也曾尝试为我的textView初始化重新分配位置,但无济于事。
这是设置页代码:
打包com.example.sleep;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class SettingPage extends AppCompatActivity{
public EditText setMinBar;
public Button setTimeEnterButton;
public Switch musicYesSwitch, soundYesSwitch;
public String SP = "New SP", timeStr = "nothing", finalTimeStr = "nil";
public boolean musicYes, soundYes;
public TextView minLeftTV, timerEndsTV;
public SharedPreferences varAcrossApp;
public SharedPreferences.Editor varEditor;
@Override //Creating the setting page
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settingpage);
setMinBar = findViewById(R.id.numMinsBar);
setTimeEnterButton = findViewById(R.id.setTimeButton);
musicYesSwitch = findViewById(R.id.musicSwitch);
soundYesSwitch = findViewById(R.id.soundSwitch);
timerEndsTV = findViewById(R.id.timerEndsTV);
minLeftTV = (TextView) findViewById(R.id.minsLeftDisplay);
varAcrossApp = PreferenceManager.getDefaultSharedPreferences(this);
setTimeEnterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveData();
loadData();
updateView();
}
});
}
public void saveData(){ //Declaring method saveData
varEditor = varAcrossApp.edit();
varEditor.putString(timeStr, setMinBar.getText().toString());
varEditor.putBoolean("mYes", musicYesSwitch.isChecked());
varEditor.putBoolean("sYes", soundYesSwitch.isChecked());
varEditor.apply();
}
public void loadData(){ //Declaring method loadData
finalTimeStr = varAcrossApp.getString(timeStr, "0");
musicYes = varAcrossApp.getBoolean("mYes", false);
soundYes = varAcrossApp.getBoolean("sYes", false);
Toast.makeText(getApplicationContext(), "Duration set" +
finalTimeStr, Toast.LENGTH_SHORT).show();
}
public void updateView() {
Log.d(SP, "Time is " + finalTimeStr);
minLeftTV.setText(finalTimeStr);
if (musicYes && soundYes) {
timerEndsTV.setText("-Turn off music /n-Change sound profile to
'Sound'");
} else if (soundYes = true) {
timerEndsTV.setText("-Change sound profile to 'Sound'");
} else if (musicYes = true) {
timerEndsTV.setText("-Turn off music");
} else {
timerEndsTV.setText("-Do nothing");
}
}
}
这是Homepage.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/homepage"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<TextView
android:id="@+id/timeLeft"
android:layout_width="191dp"
android:layout_height="49dp"
android:layout_marginStart="146dp"
android:layout_marginTop="70dp"
android:layout_marginEnd="147dp"
android:fontFamily="@font/bree_serif"
android:text="@string/text_header"
android:textAlignment="center"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/timeLeftNum"
android:layout_width="84dp"
android:layout_height="52dp"
android:layout_marginTop="143dp"
android:layout_marginEnd="103dp"
android:layout_marginBottom="138dp"
android:fontFamily="@font/bree_serif"
android:text="@string/text_timeUnit"
android:textAlignment="center"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/timerEndsTV"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/timeLeft" />
<TextView
android:id="@+id/whenTimerEnd"
android:layout_width="132dp"
android:layout_height="20dp"
android:layout_marginStart="54dp"
android:layout_marginEnd="253dp"
android:layout_marginBottom="12dp"
android:fontFamily="@font/bree_serif"
android:text="@string/text_whenTimerEnd"
android:textAlignment="viewStart"
app:layout_constraintBottom_toTopOf="@+id/timerEndsTV"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/timerEndsTV"
android:layout_width="299dp"
android:layout_height="54dp"
android:layout_marginStart="54dp"
android:layout_marginEnd="58dp"
android:layout_marginBottom="114dp"
android:fontFamily="@font/bree_serif"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/startButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/startButton"
android:layout_width="382dp"
android:layout_height="52dp"
android:layout_marginStart="13dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:fontFamily="@font/bree_serif"
android:text="@string/text_startButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/minsLeftDisplay"
android:layout_width="155dp"
android:layout_height="144dp"
android:layout_marginStart="68dp"
android:layout_marginTop="56dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="107dp"
android:text="@string/numberInDisplay"
android:textAlignment="textEnd"
android:textSize="125sp"
app:layout_constraintBottom_toTopOf="@+id/whenTimerEnd"
app:layout_constraintEnd_toStartOf="@+id/timeLeftNum"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/timeLeft" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是SettingPage.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/settingPageConstrainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/inputLayout"
android:layout_width="299dp"
android:layout_height="81dp"
android:layout_marginStart="56dp"
android:layout_marginEnd="56dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/TextboxDurationMins">
<EditText
android:id="@+id/numMinsBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<Button
android:id="@+id/setTimeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/bree_serif"
android:text="@string/setTimeButton" />
</LinearLayout>
<LinearLayout
android:id="@+id/switchesLayout"
android:layout_width="64dp"
android:layout_height="69dp"
android:layout_marginEnd="32dp"
android:layout_marginBottom="218dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<Switch
android:id="@+id/musicSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Switch
android:id="@+id/soundSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/textBoxLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="56dp"
android:layout_marginEnd="69dp"
android:layout_marginBottom="230dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/switchesLayout"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="@+id/turnOffMusics"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/bree_serif"
android:text="@string/offMusic"
android:textAlignment="textStart"
android:textSize="18sp" />
<TextView
android:id="@+id/onSound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/bree_serif"
android:text="@string/onSound"
android:textAlignment="textStart"
android:textSize="18sp" />
</LinearLayout>
<TextView
android:id="@+id/TextboxDurationMins"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="176dp"
android:layout_marginEnd="120dp"
android:fontFamily="@font/bree_serif"
android:text="@string/setDurationMins"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是错误代码:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sleep, PID: 16835
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.TextView.setText(java.lang.CharSequence)' on a null object
reference
at com.example.sleep.SettingPage.updateView(SettingPage.java:82)
at com.example.sleep.SettingPage$1.onClick(SettingPage.java:53)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3400(View.java:801)
at android.view.View$PerformClick.run(View.java:27301)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7319)
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:934)
答案 0 :(得分:1)
问题与minLeftTV.setText(finalTimeStr);
您正在通过调用
创建实例 minLeftTV = (TextView) findViewById(R.id.minsLeftDisplay);
但是您的xml布局没有具有该ID的任何视图-@+id/minsLeftDisplay
,因此minLeftTV
为null,并且您将获得null指针异常。
您有一个ID => @+id/TextboxDurationMins
的TextView,这是您要将文本设置为的视图(我假设)。更改布局中的ID或您的活动代码。