【发布时间】:2021-03-05 12:46:48
【问题描述】:
我正在尝试为按钮实现LayoutInflater,但在执行此操作后出现此错误。我不确定它是否已正确实施。对于同一个问题,我尝试了不同的解决方案,但作为 Android 新手,我仍然发现很难解决这个问题。
https://www.dropbox.com/s/s2k92n6ss4mtztg/Screenrecorder-2020-11-23-00-34-16-139.mp4?dl=0(我很抱歉无法解释工作流程,但请参阅此剪辑以获得更好的理解,第一个活动是 IntroductoryActivity,最后一个有开始按钮的活动是 onboardingfragment3)
请解释一下这个问题。谢谢!
IntroductoryActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_introductory);
............
**//The button from fragment_on_boarding3**
start_l=findViewById(R.id.startb);
.............
**//Having issue with this part**
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layoutScreen = inflater.inflate(R.layout.fragment_on_boarding3,null);
start_l.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent mainActivity = new Intent(getApplicationContext(),MainActivity.class);
startActivity(mainActivity);
// I need to save a boolean value to storage so next time when the user runs the app,I could know that he has already checked the intro screen activity
// I'm going to use shared preferences forthat process
savePrefsData();
finish();
}
});
}
private boolean restorePrefData() {
SharedPreferences pref = getApplicationContext().getSharedPreferences("myPrefs",MODE_PRIVATE);
Boolean isIntroActivityOpnendBefore = pref.getBoolean("isIntroOpnend",false);
return isIntroActivityOpnendBefore;
}
private void savePrefsData() {
SharedPreferences pref = getApplicationContext().getSharedPreferences("myPrefs",MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("isIntroOpnend",true);
editor.commit();
}
}
OnBoardingFragment3.java:
public class OnBoardingFragment3 extends Fragment {
Context mContext ;
Button start;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
ViewGroup root=(ViewGroup) inflater.inflate(R.layout.fragment_on_boarding3,container,false);
return root;
}
fragment_on_boarding3.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:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:id="@+id/startb"
android:layout_width="157dp"
android:layout_height="59dp"
android:fontFamily="@font/bungee"
android:text="Let's Start"
android:textSize="15dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.754" />
</androidx.constraintlayout.widget.ConstraintLayout>
错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.liq/com.example.liq.IntroductoryActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946) at....................
【问题讨论】:
-
你能添加完整的错误吗?
-
您不能在 Activity 中使用来自 Fragment 的视图。使用其片段中的按钮,否则您会像现在一样获得 NPE。
-
@Tuqay 以下是错误:java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.liq/com.example.liq.IntroductoryActivity}:java.lang.NullPointerException:尝试在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946) 处的空对象引用上调用虚拟方法“void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)”...... ......
标签: layout-inflater android-inflate