如何从片段活动中调用MainActivity变量

时间:2015-07-02 12:42:40

标签: android android-fragments

我是新来的android有一个带有片段的标签布局,我有一个编辑文本字段,在我的父活动中声明,我想检查天气编辑文本字段是空的或不是从片段我怎么能这样做这就是我所做的,但它显示错误 这是我的主要活动编辑文本字段:

search_edit_text = (FloatingEditText)findViewById(R.id.edt_missile_number);
        search_edit_text.setInputType(InputType.TYPE_CLASS_NUMBER);
        search_edit_text.setGravity(Gravity.CENTER);
        search_edit_text.setCursorVisible(false);
        search_edit_text.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                search_edit_text.setCursorVisible(true);

            }
        });

这是我的片段活动:

public class Manual_AC_Fuse_ckt extends Fragment {


    static Manual manual_child;    //MANUAL----->PARENT CLASS NAME


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {



         Root_view = inflater.inflate(R.layout.manualmode_ac_fuse_ckt, null);
         manual_child = new Manual();
        (CheckBox)Root_view.findViewById(check_box_count_fuse_after_cnc[i]);

        registerForContextMenu(table_fuse_afer_cnc);
        return Root_view;


    }
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        menu.setHeaderTitle("MANUAL MODE : CONNECTION");
        menu.add(1, v.getId(), 0, "TEST  ");
        menu.add(1, v.getId(), 0, "SELECT ALL  ");
        menu.add(1, v.getId(), 0, "DESELECT ALL  ");
        super.onCreateContextMenu(menu, v, menuInfo);
    }
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        if(item.getTitle()=="TEST  ")
        {

            if(manual_child.search_edit_text.equals(""))
            {
                Toast.makeText(getActivity(),"Empty Message",Toast.LENGTH_LONG).show();
            }

}
}

这是我的logcat:

01-02 07:27:25.830: E/AndroidRuntime(28243): FATAL EXCEPTION: main
01-02 07:27:25.830: E/AndroidRuntime(28243): java.lang.NullPointerException
01-02 07:27:25.830: E/AndroidRuntime(28243):    at com.developer.milanandroid.Manual_AC_Fuse_ckt.onContextItemSelected(Manual_AC_Fuse_ckt.java:120)
01-02 07:27:25.830: E/AndroidRuntime(28243):    at android.support.v4.app.Fragment.performContextItemSelected(Fragment.java:1909)
01-02 07:27:25.830: E/AndroidRuntime(28243):    at android.support.v4.app.FragmentManagerImpl.dispatchContextItemSelected(FragmentManager.java:2058)
01-02 07:27:25.830: E/AndroidRuntime(28243):    at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:359)
01-02 07:27:25.830: E/AndroidRuntime(28243):    at com.android.internal.policy.impl.PhoneWindow$DialogMenuCallback.onMenuItemSelected(PhoneWindow.java:3644)
01-02 07:27:25.830: E/AndroidRuntime(28243):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
01-02 07:27:25.830: E/AndroidRuntime(28243):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
01-02 07:27:25.830: E/AndroidRuntime(28243):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
01-02 07:27:25.830: E/AndroidRuntime(28243):    at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:167)
01-02 07:27:25.830: E/AndroidRuntime(28243):    at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:941)
01-02 07:27:25.830: E/AndroidRuntime(28243):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
01-02 07:27:25.830: E/AndroidRuntime(28243):    at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
01-02 07:27:25.830: E/AndroidRuntime(28243):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2788)
01-02 07:27:25.830: E/AndroidRuntime(28243):    at android.widget.AbsListView$1.run(AbsListView.java:3473)
01-02 07:27:25.830: E/AndroidRuntime(28243):    at android.os.Handler.handleCallback(Handler.java:730)
01-02 07:27:25.830: E/AndroidRuntime(28243):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-02 07:27:25.830: E/AndroidRuntime(28243):    at android.os.Looper.loop(Looper.java:137)
01-02 07:27:25.830: E/AndroidRuntime(28243):    at android.app.ActivityThread.main(ActivityThread.java:5103)
01-02 07:27:25.830: E/AndroidRuntime(28243):    at java.lang.reflect.Method.invokeNative(Native Method)
01-02 07:27:25.830: E/AndroidRuntime(28243):    at java.lang.reflect.Method.invoke(Method.java:525)
01-02 07:27:25.830: E/AndroidRuntime(28243):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-02 07:27:25.830: E/AndroidRuntime(28243):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-02 07:27:25.830: E/AndroidRuntime(28243):    at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:0)

你应该使用一个接口。您可以在活动实现的片段中创建接口。这是与活动视图沟通的更好方式。

请参阅此帖子以获取有关如何执行此操作的参考:Android: error with on fragment interaction Listener

答案 1 :(得分:0)

这可以是两点。 你可以用 FloatingEditText editText =(FloatingEditText)((MainActivity.this)getActivity())。findViewById(R.id.edt_missile_number); 2.使用if(item.getTitle()。equals(“TEST”))cond。

答案 2 :(得分:-1)

您可以在片段中调用主要活动变量,如下所示:

EditText edt = (EditText)getActivity().findViewById(R.id.edtSearch);