自定义界面不适用于Activity

时间:2017-02-08 07:09:52

标签: java android

我还是创建界面的新手。我参考了很多教程,但仍然没有工作。我想将值从活动传递给片段。 mListener为空。

如何以正确的方式调用听众?

接口

public interface ListenerCardDetails {
    void getCardSelected(String id);
}

MainActivity.java

public class CardDetailsActivity extends AppCompatActivity {

private ListenerCardDetails mListener;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // some stuff
    mListener.getCardSelected("20");
}

public void setListener(ListenerCardDetails mListener) {
        this.mListener = mListener;
    }

MyFragment.java

public class TabPoints extends Fragment implements ListenerCardDetails {

    private CardDetailsActivity mCardDetailsActivity;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab_points, container, false);
        configView(view);
        return view;
    }

    private void configView(View view){
        ((CardDetailsActivity) getActivity()).setListener(this);
    }

    @Override
    public void getCardSelected(String id) {
         Toast.makeText(getActivity(), id, Toast.LENGTH_LONG).show();
    }
}

错误

  

java.lang.RuntimeException:无法实例化活动   java.lang.InstantiationException:   java.lang.Class中   没有零参数构造函数                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)   引起:java.lang.InstantiationException:   java.lang.Class中   没有零参数构造函数                         在java.lang.Class.newInstance(本机方法)

非常感谢任何帮助。

6 个答案:

答案 0 :(得分:2)

这样做

new CardDetailsActivity(this);

不是android希望你创建活动的方式,你的代码不起作用,因为基本上你正在创建一个完全新的活动实例,而不是记住框架在后台为你做的所有准备工作。 ..

解决方案可以是:

获取活动并添加一个设置监听器界面的新方法!

答案 1 :(得分:0)

Unable to instantiate activity java.lang.InstantiationException: java.lang.Class has no zero argument constructor

这告诉你究竟发生了什么。在Android中,您不得使用自定义构造函数创建任何ActivityFragment。 这是因为Android框架使用反射来实例化它们。

如果您想在ActivityFragment之间进行通信,则应使用onAttach(Context context)中的Fragment

请参阅Android https://developer.android.com/training/basics/fragments/communicating.html

的官方文档

答案 2 :(得分:0)

如何操作,在Interface中定义Fragment并添加void getCardSelected(String id);方法。在Activity中实现相同的接口并覆盖接口方法并在该方法中编写逻辑。

For more explanation follow this tutorial

如果您要将Activity的值发送到Fragment,请点按以下链接:Send data from activity to fragment in android

希望它会对你有所帮助。

答案 3 :(得分:0)

不要使用自定义构造函数,而是使用setListener。

public class MainActivity extends AppCompatActivity {

    private ListenerCardDetails mListener;

    public void setListener(ListenerCardDetails mListener) {
        this.mListener = mListener;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // some stuff
        mListener.getCardSelected("20");
    }


    public class TabPoints extends Fragment implements ListenerCardDetails {

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_tab_points, container, false);
            configView(view);
            return view;
        }

        private void configView(View view){
            ((MainActivity) getActivity()).setListener(this);
        }


        @Override
        public void getCardSelected(String id) {
            Toast.makeText(getActivity(), id, Toast.LENGTH_LONG).show();
        }
    }

答案 4 :(得分:0)

试试这个`public class MainActivity扩展AppCompatActivity {

    private ListenerCardDetails mListener;

    public void setListener(ListenerCardDetails mListener) {
        this.mListener = mListener;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TabPoints fragmentListingMyGrid = new TabPoints();
        getSupportFragmentManager().beginTransaction().replace(R.id.hidesContainer, fragmentListingMyGrid);

        fragmentListingMyGrid.setCardSelected("20");
    }

}

public class TabPoints extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab_points, container, false);
        return view;
    }

    public void setCardSelected(String id) {
        Toast.makeText(getActivity(), id, Toast.LENGTH_LONG).show();
    }
}`

答案 5 :(得分:0)

CardDetailActivity中更改功能setListener,如下所示:

public static void setListener(ListenerCardDetails mListener) {
    this.mListener = mListener;
}

现在你TabPoints片段替换:

((CardDetailsActivity) getActivity()).setListener(this);

with:

CardDetailActivity.setListener(this);
  

OR

CardDetailActivity.java

public class CardDetailsActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TabPoints.bindListener(new ListenerCardDetails(){
            @Override
            public void getCardSelected(String id) {
                Toast.makeText(CardDetailsActivity.this, id, Toast.LENGTH_LONG).show();
            }
         }
    }
}

TabPoints.java

public class TabPoints extends Fragment {
ListenerCardDetails mListener;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_tab_points, container, false);
    return view;
    configView(view);

}

private void configView(View view){
    mListener.getCardSelected(20);
}

public static void bindListener(ListenerCardDetails listener) {
    mListener = listener;
}