英文:
android.view.InflateException when switching to landscape mode
问题
这是您提供的代码的翻译部分:
SettingsFragment.java
package com.example.celebquizapp;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.content.SharedPreferences;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
public class SettingsFragment extends Fragment implements View.OnClickListener {
private SharedPreferences preferences;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// 为此片段填充布局
View fragment_view = inflater.inflate(R.layout.fragment_settings, container, false);
preferences = this.getActivity().getSharedPreferences("values", Context.MODE_PRIVATE);
return fragment_view;
}
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
RadioGroup selectionOption = getActivity().findViewById(R.id.selection_option_setting);
setRadioButtonListener(selectionOption);
}
public void onClick(View v) {
RadioButton button = (RadioButton) v;
boolean checked = button.isChecked();
if (checked) {
int chosenVal = Integer.parseInt(String.valueOf(button.getText()));
Log.d("RADIOS", "当前选择的值:" + chosenVal);
preferences.edit().putInt("numofopt", chosenVal).apply();
}
}
public void setRadioButtonListener(RadioGroup radioGroup) {
for (int i = 0; i < radioGroup.getChildCount(); i++) {
radioGroup.getChildAt(i).setOnClickListener(this);
}
}
@Override
public void onStart() {
super.onStart();
// 不需要在首选项中保存数据
// 因此在启动时删除数据
preferences.edit().clear().apply();
}
}
SettingsFragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SettingsFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/selectionOptionLabel"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:layout_marginTop="0dp"
android:padding="10dp"/>
<RadioGroup
android:id="@+id/selection_option_setting"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:textSize="20sp"
android:text="@string/_2"
android:layout_marginLeft="10dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:text="@string/_3"/>
<!-- 其他 RadioButton 项 -->
</RadioGroup>
</LinearLayout>
请注意,由于您要求只返回翻译的部分,我只提供了代码的翻译部分,并省略了其他内容。如果您需要进一步的帮助或解释,请随时提问。
英文:
I am trying to make an app that involves two content mains, one being for portrait the other being for landscape, However, when I switch the orientation to landscape, the app crashes. Portrait and Landscape have different layouts, the only notable difference between the two is that landscape has one more fragment within its layout.
I want to iterate that this is my first time working with landscape mode in android studio so if I made any rookie mistakes, i'm sure you'll be forgiving.
Any input is appreciated, thanks in advance!
Settings Fragment.java
package com.example.celebquizapp;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.content.SharedPreferences;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
public class SettingsFragment extends Fragment implements View.OnClickListener {
private SharedPreferences preferences;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View fragment_view = inflater.inflate(R.layout.fragment_settings, container, false);
preferences = this.getActivity().getSharedPreferences("values", Context.MODE_PRIVATE);
return fragment_view;
}
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
RadioGroup selectionOption = getActivity().findViewById(R.id.selection_option_setting);
setRadioButtonListener(selectionOption);
}
public void onClick(View v) {
RadioButton button = (RadioButton) v;
boolean checked = button.isChecked();
if (checked) {
int chosenVal = Integer.parseInt(String.valueOf(button.getText()));
Log.d("RADIOS", "Current value chosen: " + chosenVal);
preferences.edit().putInt("numofopt", chosenVal).apply();
}
}
public void setRadioButtonListener(RadioGroup radioGroup) {
for (int i = 0; i < radioGroup.getChildCount(); i++) {
radioGroup.getChildAt(i).setOnClickListener(this);
}
}
@Override
public void onStart() {
super.onStart();
// Not required to save data in preferences
// So delete on start up
preferences.edit().clear().apply();
}
}
Settings Fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SettingsFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/selectionOptionLabel"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:layout_marginTop="0dp"
android:padding="10dp"/>
<RadioGroup
android:id="@+id/selection_option_setting"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:textSize="20sp"
android:text="@string/_2"
android:layout_marginLeft="10dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:text="@string/_3"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:text="@string/_4"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:text="@string/_5"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:text="@string/_6"/>
</RadioGroup>
</LinearLayout>
Error message
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.celebquizapp, PID: 11657
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.celebquizapp/com.example.celebquizapp.MainActivity}: android.view.InflateException: Binary XML file line #22 in com.example.celebquizapp:layout/activity_main: Binary XML file line #26 in com.example.celebquizapp:layout/content_main: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:5279)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5187)
at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
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:930)
Caused by: android.view.InflateException: Binary XML file line #22 in com.example.celebquizapp:layout/activity_main: Binary XML file line #26 in com.example.celebquizapp:layout/content_main: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #26 in com.example.celebquizapp:layout/content_main: Error inflating class fragment
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RadioGroup.getChildCount()' on a null object reference
at com.example.celebquizapp.SettingsFragment.setRadioButtonListener(SettingsFragment.java:50)
at com.example.celebquizapp.SettingsFragment.onViewCreated(SettingsFragment.java:32)
at androidx.fragment.app.FragmentManagerImpl.ensureInflatedFragmentView(FragmentManagerImpl.java:1144)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:851)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1133)
at androidx.fragment.app.FragmentManagerImpl.addFragment(FragmentManagerImpl.java:1393)
at androidx.fragment.app.FragmentManagerImpl.onCreateView(FragmentManagerImpl.java:3205)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:134)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:357)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:336)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1069)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:997)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:1263)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1119)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.example.celebquizapp.MainActivity.onCreate(MainActivity.java:18)
at android.app.Activity.performCreate(Activity.java:7802)
E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:5279)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5187)
at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
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:930)
答案1
得分: 0
RadioGroup selectionOption = view.findViewById(R.id.selection_option_setting);
英文:
> RadioGroup selectionOption = getActivity().findViewById(R.id.selection_option_setting);
It should be
RadioGroup selectionOption = view.findViewById(R.id.selection_option_setting);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论