gpt4 book ai didi

Android fragment onAttach 错误

转载 作者:行者123 更新时间:2023-11-29 20:16:44 25 4
gpt4 key购买 nike

我的 on 有问题 附上它在他身上做红线并写信给我:

“覆盖‘android.support.v4.app.Fragment’中已弃用的方法”

请帮助我理解我做错了什么?为所有的 helper 打交道!

package com.example.omermalka.memecreator;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
/**
* Created by omermalka on 14/11/2015.
*/



public class TopSectionFragment extends Fragment {

private static EditText TopText;
private static EditText BottomText;

TopSectionListener acitivtyCommander;

public interface TopSectionListener{
public void createMime(String top , String Bottom);
}


@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try{
acitivtyCommander = (TopSectionListener) activity;
}catch (ClassCastException e){
throw new ClassCastException(activity.toString());
}
}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.top_section_fragment, container, false);

TopText = (EditText) view.findViewById(R.id.TopTextInput);
BottomText = (EditText) view.findViewById(R.id.BottomTextInput);
final Button button = (Button) view.findViewById(R.id.BottomTextInput);

button.setOnClickListener(
new View.OnClickListener() {

@Override
public void onClick(View v) {
buttonClicked(v);
}
}
);



return view;
}


public void buttonClicked(View v) {
acitivtyCommander.createMime(TopText.getText().toString(),BottomText.getText().toString());
}

}

最佳答案

你需要改变

public void onAttach(Activity activity)

public void onAttach(Context context)

最终代码:

@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
acitivtyCommander = (TopSectionListener) context;
} catch (ClassCastException e){
throw new ClassCastException(context.toString());
}
}

关于Android fragment onAttach 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33713426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com