我已经环顾四周并实现了一些我发现的东西,以便让findViewById在我的Fragment中工作。但是,我得到了无法解析的方法findViewById或者我得到了“无法访问的语句”。我不完全确定我哪里出错了。
这是我的片段的代码。
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
/**
* Created by James Singleton on 8/8/2016.
*/
public class SettingsFragment extends Fragment
{
View myView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.settings_layout, container, false);
return myView;
RadioGroup radioGroup = (RadioGroup) getView().findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
switch(checkedId) {
case R.id.radioButton7:
// switch to fragment 1
break;
case R.id.radioButton6:
// Fragment 2
break;
}
}
});
}
}
这是我的布局代码
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:text="Use Cell Data to retrieve driving data:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:textSize="18sp"
android:textColor="@color/cast_expanded_controller_background_color"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<RadioGroup
android:layout_width="89dp"
android:layout_height="69dp"
android:weightSum="1"
android:layout_below="@+id/textView"
android:id="@+id/radioGroup">
<RadioButton
android:text="Enable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButton7"
android:layout_weight="1" />
<RadioButton
android:text="Disable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButton6"
android:layout_weight="1"
android:checked="true" />
</RadioGroup>
答案 0 :(得分:8)
更改您的代码:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.settings_layout, container, false);
RadioGroup radioGroup = (RadioGroup) myView .findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
switch(checkedId) {
case R.id.radioButton7:
// switch to fragment 1
break;
case R.id.radioButton6:
// Fragment 2
break;
}
}
});
return myView;
}
答案 1 :(得分:1)
更改onCreate方法代码,就像你在无线电组代码
之前返回myView一样@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.settings_layout, container, false);
RadioGroup radioGroup = (RadioGroup) myView .findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
switch(checkedId) {
case R.id.radioButton7:
// switch to fragment 1
break;
case R.id.radioButton6:
// Fragment 2
break;
}
}
});
}
return myView;
}
答案 2 :(得分:0)
在onCreateView()
中返回视图之前,请使用您夸大的视图:
view.findViewById(R.id.your_id);
所以在你的情况下它会像:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.settings_layout, container, false);
RadioGroup radioGroup = (RadioGroup) myView.findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
switch(checkedId) {
case R.id.radioButton7:
// switch to fragment 1
break;
case R.id.radioButton6:
// Fragment 2
break;
}
}
});
return myView;
}
答案 3 :(得分:0)
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.settings_layout, container, false);
RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);
return myView;
}