performClick()

时间:2016-02-25 08:15:17

标签: java android

我正在尝试点击片段中的按钮。就像在下面的过程中一样:

  1. 点击片段导航抽屉中的A
  2. App open up Fragment A
  3. 当片段A启动时,它会在其类中的按钮上执行Click(),这将导致活动B
  4. 我已经尝试将btnSchedule.performClick();放在OnCreate,OnStart甚至是AsyncTask onPostExecute方法中。

    然而,这一切都会导致nullpointerException,如下所示:

      

    02-25 16:03:13.421 17348-17348 / com.example.l33902.contactmanagment1512   E / AndroidRuntime:致命异常:主要                                                                                                处理:com.example.l33902.contactmanagment1512,PID:17348                                                                                                java.lang.NullPointerException:尝试调用虚方法   空对象上的'boolean android.widget.Button.performClick()'   参考                                                                                                    在   com.example.l33902.contactmanagment.FragmentPlanner.onStart(FragmentPlanner.java:106)                                                                                                    在android.app.Fragment.performStart(Fragment.java:2138)                                                                                                    在   android.app.FragmentManagerImpl.moveToState(FragmentManager.java:937)                                                                                                    在   android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)                                                                                                    在android.app.BackStackRecord.run(BackStackRecord.java:834)                                                                                                    在   android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)                                                                                                    在android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:452)                                                                                                    在android.os.Handler.handleCallback(Handler.java:739)                                                                                                    在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                                    在android.os.Looper.loop(Looper.java:145)                                                                                                    在android.app.ActivityThread.main(ActivityThread.java:6897)                                                                                                    at java.lang.reflect.Method.invoke(Native Method)                                                                                                    在java.lang.reflect.Method.invoke(Method.java:372)                                                                                                    在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1404)                                                                                                    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

    以下是我的代码(片段A)(目前在onStart方法中放置btnSchedule.performClick();):

    public class FragmentPlanner extends Fragment {
    
        View root;
        Context context;
        Button btnSchedule;
    
        public FragmentPlanner() {
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            final View rootView = inflater.inflate(R.layout.fragment_planner, container, false);
            root = rootView;
            context = rootView.getContext();
            setHasOptionsMenu(true);
    
            Button btnSchedule = (Button)rootView.findViewById(R.id.btnSchedule);
            btnSchedule.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    // here you set what you want to do when user clicks your button,
    
                    Intent intent = new Intent(getActivity(), CalendarMain.class);
                    startActivity(intent);
    
                }
            });
    
    
    
            return rootView;
        }
    
        @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            inflater.inflate(R.menu.to_note, menu);
            MenuItem noteItem;
            noteItem = menu.findItem(R.id.action_note).setIcon(resizeImage(R.drawable.ic_note_add_white_24dp, 250, 250));
            noteItem.setVisible(true);
    
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            int id = item.getItemId();
            if(id == R.id.action_note){
                //Do whatever you want to do
    
                Intent intent = new Intent(context, CalendarMain.class);
                startActivity(intent);
    
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    
        private Drawable resizeImage(int resId, int w, int h) {
            Bitmap BitmapOrg = BitmapFactory.decodeResource(getResources(), resId);
            int width = BitmapOrg.getWidth();
            int height = BitmapOrg.getHeight();
            int newWidth = w;
            int newHeight = h;
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width, height, matrix, true);
            return new BitmapDrawable(resizedBitmap);
        }
    
        @Override
        public void onStart(){
            super.onStart();
    
            btnSchedule.performClick();
    
            }
    
    
    }
    

3 个答案:

答案 0 :(得分:4)

这一行错了:

Button btnSchedule = (Button)rootView.findViewById(R.id.btnSchedule);

必须是:

btnSchedule = (Button)rootView.findViewById(R.id.btnSchedule);

因为有一个成员:

Button btnSchedule;

当你写:

Button btnSchedule = (Button)rootView.findViewById(R.id.btnSchedule);

您正在声明一个局部变量,而不是指定一个成员。

答案 1 :(得分:4)

onCreateView中,您正在重新定义本地范围内的成员btnSchedule,隐藏未初始化的类成员。在onCreateView中更改

Button btnSchedule = (Button)rootView.findViewById(R.id.btnSchedule);

btnSchedule = (Button)rootView.findViewById(R.id.btnSchedule);

答案 2 :(得分:1)

像这样修改代码:

public class FragmentPlanner extends Fragment {

    View rootView;
    Context context;
    Button btnSchedule;

    public FragmentPlanner() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_planner, container, false);
        context = rootView.getContext();
        setHasOptionsMenu(true);
        btnSchedule = (Button)rootView.findViewById(R.id.btnSchedule);
        btnSchedule.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // here you set what you want to do when user clicks your button,
                Intent intent = new Intent(getActivity(), CalendarMain.class);
                startActivity(intent);
            }
        });
        return rootView;
    }

希望它有所帮助!!!