如何将数据传递到导航抽屉项目?

时间:2015-11-02 01:36:43

标签: java android android-fragments navigation-drawer

我正在使用带有三个项目的导航抽屉(时间表,声明,项目),我想将ab传递给声明。在时间表中我有3个活动,ab在work_details活动中。如何实现这一目标?

TimeSheet有3个活动(信息(扩展到片段),劳动力,工作细节)

声明有2项活动(声明(扩展到片段),cameraMain)。

WorkDetails.java

  Button btn1 = (Button) findViewById(R.id.button2);
  btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                AlertDialog.Builder builder = new AlertDialog.Builder(WorkDetailsTable.this);
                builder.setTitle("Data Saved");
                builder.setMessage("Are you sure you want to save?");
                builder.setIcon(android.R.drawable.ic_dialog_alert);
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int ii) {
                long ab = ts.insertTimeSheet(name, weather, date, status,b,i);         
               Bundle bundle =new Bundle();
               bundle.putString("ab","ab");
               Claims o=new Claims();
               o.setArguments(bundle);
}

Claims.java

final Long fk=this.getArguments().getLong("ab");
 Button button = (Button) claims.findViewById(R.id.button8);
button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                Intent intent = new Intent(getActivity().getApplicationContext(), CameraMain.class);
                Toast.makeText(getActivity().getApplicationContext(), fk+"", Toast.LENGTH_LONG).show();
                startActivity(intent);
            }
        });
        return claims;
    }

我从Send data from activity to fragment in android引用,但是当我选择声明项时应用程序崩溃了。

  

11-02 09:25:55.910 6116-6116 / com.example.project.project   E / AndroidRuntime:致命异常:主要       处理:com.example.project.project,PID:6116       显示java.lang.NullPointerException               在com.example.project.project.Claims.onCreateView(Claims.java:59)               在android.app.Fragment.performCreateView(Fragment.java:1700)               在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)               在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)               在android.app.BackStackRecord.run(BackStackRecord.java:684)               在android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1453)

我认为NullPointException来自这里

  

FK = data.getLong(" AB&#34);

我是否需要在MainActivity中添加参数?

MainActivity.java

mainActivity中的部分代码段

 private void selectItem(int position) {

            Fragment fragment = null;

            switch (position) {
                case 0:
                    fragment=new Information();
                    break;
                case 1:
                    fragment=new Claims();
                    break;

                case 2:
                    fragment=new Project();

                case 3:
                    fragment=new Report();

                case 4:
                    fragment=new ViewView();

                default:
                    break;
            }

            if (fragment != null) {
                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

                mDrawerList.setItemChecked(position, true);
                mDrawerList.setSelection(position);
                setTitle(mNavigationDrawerItemTitles[position]);
                mDrawerLayout.closeDrawer(mDrawerList);

            } else {
                Log.e("MainActivity", "Error in creating fragment");
            }

EDITED

 Button btn1 = (Button) findViewById(R.id.button2);
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                AlertDialog.Builder builder = new AlertDialog.Builder(WorkDetailsTable.this);
                builder.setTitle("Data Saved");
                builder.setMessage("Are you sure you want to save?");
                builder.setIcon(android.R.drawable.ic_dialog_alert);
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int ii) {
               long ab = ts.insertTimeSheet(name, weather, date, status,b,i);
                Bundle bundle =new Bundle();
                bundle.putLong("ab",ab);
                Claims o=new Claims();
                o.setArguments(bundle);
                }

Claims.java

  Bundle bundle=this.getArguments();
  fk=bundle.getLong("ab");
  button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity().getApplicationContext(), fk+"", Toast.LENGTH_LONG).show();
                AlertDialogRadio(a1);


            }
        });

MainActivity

  private void selectItem(int position) {

            Fragment fragment = null;

            switch (position) {
                case 0:
                    fragment=new Information();
                    break;
                case 1:
                    fragment=new Claims();
                    break;

                case 2:
                    fragment=new Project();
                    break;

                case 3:
                    fragment=new Report();
                    break;

                case 4:
                    fragment=new ViewView();

                default:
                    break;
            }

            if (fragment != null) {
                FragmentManager fragmentManager = getFragmentManager();
                if(fragment instanceof Claims)
                {
                    Bundle bundle=new Bundle();
                    bundle.putString("ab","ab");
                    fragment.setArguments(bundle);
                    fragment.setArguments(bundle);
                }

                fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

                mDrawerList.setItemChecked(position, true);
                mDrawerList.setSelection(position);
                setTitle(mNavigationDrawerItemTitles[position]);
                mDrawerLayout.closeDrawer(mDrawerList);

            } else {
                Log.e("MainActivity", "Error in creating fragment");
            }
        }
    }

3 个答案:

答案 0 :(得分:1)

<强> MainActivity

private void selectItem(int position) {

        Fragment fragment = null;

        switch (position) {
            case 0:
                fragment=new Information();
                break;
            case 1:
                fragment=new Claims();
                Bundle bundle=new Bundle();
                bundle.putLong("ab",WorkDetails.ab);
                fragment.setArguments(bundle);
                break;

            case 2:
                fragment=new Project();
                break;

            case 3:
                fragment=new Report();
                break;

            case 4:
                fragment=new ViewView();

            default:
                break;
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getFragmentManager();

            fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);
            setTitle(mNavigationDrawerItemTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);

        } else {
            Log.e("MainActivity", "Error in creating fragment");
        }
    }
}

WorkDetails

public static long ab=-1;//this is flied ,don't put in the method



Button btn1 = (Button) findViewById(R.id.button2);
btn1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
    AlertDialog.Builder builder = new     AlertDialog.Builder(WorkDetailsTable.this);
    builder.setTitle("Data Saved");
    builder.setMessage("Are you sure you want to save?");
            builder.setIcon(android.R.drawable.ic_dialog_alert);
            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int ii) {
           ab = ts.insertTimeSheet(name, weather, date, status,b,i);         

}

答案 1 :(得分:1)

您可以做的是将此数据放在放置片段的活动中。例如。

在你的活动中       长ab = 12.4;

在你的片段中      long data =((YourActivity)getActivity())。ab;

     "OR"

使ab喜欢

static long ab = value; // your Details activity

在您的详细信息活动中获取此

的值

详细活动名称如

long data = Details.ab //in your claims activity.                                                                        

答案 2 :(得分:0)

当您实例化您的Claim Fragment时,实例化一个bundle对象并分配您需要在bundle中传递的任何数据,并将其设置为该片段的参数。

Bundle bundle = new Bundle();
bundle.putLong("ab",10L);
new Claims().setargument(bundle);

然后在您的声明片段的onCreateView()中,您可以访问此捆绑数据

 Bundle bundle = getArguments();
 long ab = bundle.getLong("ab");