例如,我分别使用 A,B,C main java A,B,C和主活动xml。
我试图将框架布局放在 main 中 我还创建 FM_A,FM_B和FM_C ,这是一个链接到
的片段java由于 main 是一个导航栏活动,我将main替换为片段中的框架布局,这一切都是成功的。
直到我尝试使用布局中的按钮,出现错误:
Could not find method saveSetting(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'btnSave'
单独使用活动而不使用片段访问它们,它可以正常运行,我这样做是不是错了?
参考链接:https://www.youtube.com/watch?v=ju837bQOBfg&spfreload=5
更新:已添加代码
Setting.java
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
startLinking();
startSharedPreferences();
// Check for second entry
if (sharedPreferences.getString("Name", "") != null)
{
displayInfo();
btnEdit.setVisibility(View.VISIBLE);
btnSave.setVisibility(View.INVISIBLE);
btnClear.setVisibility(View.INVISIBLE);
btnGoWelcome.setVisibility(View.VISIBLE);
etxtName.setEnabled(false);
etxtFood.setEnabled(false);
spinnerBreed.setEnabled(false);
}
}
public void startLinking()
{
etxtName = (EditText) findViewById(R.id.etxtName);
etxtFood = (EditText) findViewById(R.id.etxtFood);
btnSave = (Button) findViewById(R.id.btnSave);
btnClear = (Button) findViewById(R.id.btnClear);
btnEdit = (Button) findViewById(R.id.btnEdit);
btnGoWelcome = (Button) findViewById(R.id.btnGoWelcome);
spinnerBreed = (Spinner) findViewById(R.id.spinnerBreed);
adapter = ArrayAdapter.createFromResource(this, R.array.breedNameArr, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerBreed.setAdapter(adapter);
}
public void startSharedPreferences()
{
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
editor = sharedPreferences.edit();
}
public void saveSetting(View view)
{
String petName, breed, food, userID;
if (TextUtils.isEmpty(etxtName.getText().toString()) == true || spinnerBreed.getCount() == 0 || TextUtils.isEmpty(etxtFood.getText().toString()) == true)
{
Toast.makeText(this, "Please fill all the info!", Toast.LENGTH_SHORT).show();
return;
} else
{
editor = sharedPreferences.edit();
editor.putString("Name", etxtName.getText().toString());
editor.putString("Food", etxtFood.getText().toString());
editor.putInt("BreedPos", spinnerBreed.getSelectedItemPosition());
editor.putString("Breed", spinnerBreed.getSelectedItem().toString());
editor.commit();
Toast.makeText(this, "SAVE SUCCESS!", Toast.LENGTH_SHORT).show();
btnEdit.setVisibility(View.VISIBLE);
btnSave.setVisibility(View.INVISIBLE);
btnClear.setVisibility(View.INVISIBLE);
btnGoWelcome.setVisibility(View.VISIBLE);
etxtName.setEnabled(false);
etxtFood.setEnabled(false);
spinnerBreed.setEnabled(false);
}
}
FM_Setting.java(片段)
public class FM_Setting extends Fragment
{
View myview;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
myview=inflater.inflate(R.layout.activity_setting, container, false);
return myview;
}
}
activity_setting.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/save"
android:id="@+id/btnSave"
android:onClick="saveSetting"
android:background="@android:color/transparent"
android:textColor="@color/colorTitleBar"
android:layout_below="@+id/etxtFood"
android:layout_centerHorizontal="true"/>
答案 0 :(得分:2)
您需要在main
活动中使用与XML中定义的onClick
相对应的方法。很难说没有代码示例,但我怀疑它是未定义的,或者您将它移动到片段类。 XML中的onClick
必须与Activity中的方法相对应。
public void saveSetting(View v){
}
或者,您可以在片段中找到视图并以编程方式设置OnClickListener
,然后从XML中删除onClick
行。这是常见的做法。
View view = getActivity().findViewById(R.id.btnSave);
view.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
saveSetting(v);
}
});