我以前做过一个简单的应用程序,只是使用seekbar和SharedReferences设置字体大小。然而,当我尝试使用片段时,它显示了这个错误。
2021-07-08 19:18:54.164 12897-12897/com.example.testtt E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.testtt, PID: 12897
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SeekBar.setProgress(int)' on a null object reference
at com.example.testtt.SizeFontFragment.onCreateView(SizeFontFragment.java:68)
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.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
这是我的尺码。
package com.example.testtt;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import androidx.annotation.Nullable;
import com.google.android.material.navigation.NavigationView;
import static android.content.Context.MODE_PRIVATE;
public class SizeFontFragment extends Fragment {
int textSize = 30;
int textSize2 = 13;
int textSize3 = 13;
//setbutton
public Button buttonconfirm;
//SharedPreferences
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
//definebutton
buttonconfirm = v.findViewById(R.id.buttonconfirm);
//sharedpreferences
sharedPreferences = this.getActivity().getSharedPreferences("fontsize", MODE_PRIVATE);
editor = sharedPreferences.edit();
//definetextview
TextView arabictext1 = v.findViewById(R.id.arabictextPREVIEW);
TextView textviewsize = v.findViewById(R.id.arabictextview);
TextView transliterationtext = v.findViewById(R.id.transliterationtextPREVIEW);
TextView textviewsize2 = v.findViewById(R.id.transliterationtextview);
TextView translationtext = v.findViewById(R.id.trasnlationtextPREVIEW);
TextView textviewsize3 = v.findViewById(R.id.translationtextview);
//seekbararabictext
int progress = sharedPreferences.getInt("progress", 0);
SeekBar seekbar1 = v.findViewById(R.id.seekbararab);
seekbar1.setProgress(progress); // size 30sp
arabictext1.setTextSize(textSize+seekbar1.getProgress());
textviewsize.setText(seekbar1.getProgress() + "/" + seekbar1.getMax()); // 0/30
seekbar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int progressNew = 0;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
textSize = textSize + (progress - progressNew);
progressNew = progress;
arabictext1.setTextSize(textSize);
editor.putInt("progress", progress);
editor.apply();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
textviewsize.setText(seekbar1.getProgress() + "/" + seekbar1.getMax()); // 0/30
}
});
//seekbartransliterationtext
int progress2 = sharedPreferences.getInt("progress2", 0);
SeekBar seekbar2 = v.findViewById(R.id.seekbartransliteration);
seekbar2.setProgress(progress2); // size 13sp
transliterationtext.setTextSize(textSize2+seekbar2.getProgress());
textviewsize2.setText(seekbar2.getProgress() + "/" + seekbar2.getMax()); // 0/13sp
seekbar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int progressNew = 0;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
textSize2 = textSize2 + (progress - progressNew);
progressNew = progress;
transliterationtext.setTextSize(textSize2);
editor.putInt("progress2", progress);
editor.apply();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
textviewsize2.setText(seekbar2.getProgress() + "/" + seekbar2.getMax()); // 0/13
}
});
//seekbartranslationtext
int progress3 = sharedPreferences.getInt("progress3", 0);
SeekBar seekbar3 = v.findViewById(R.id.seekbartranslation);
seekbar3.setProgress(progress3); // size 13sp
translationtext.setTextSize(textSize3+seekbar3.getProgress());
textviewsize3.setText(seekbar3.getProgress() + "/" + seekbar3.getMax()); // 0/13
seekbar3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int progressNew = 0;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
textSize3 = textSize3 + (progress - progressNew);
progressNew = progress;
translationtext.setTextSize(textSize3);
editor.putInt("progress3", progress);
editor.apply();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
textviewsize3.setText(seekbar3.getProgress() + "/" + seekbar3.getMax()); // 0/13
}
});
return v;
}
}
我试图找出错误和问题,但它似乎很好。我真的很想知道这些问题。提前谢谢
暂无答案!
目前还没有任何答案,快来回答吧!