I want to send a data from my Activity NavigationView to a fragment that derives from this

1

The data is being received from my MainActivity which is a Login, which sends the data (through Intent) to the Start activity (until here the data is sent without problems) and from the Start activity I am trying to send it to my Fragment that derives from the same class Start (here is where the data is not sent and gives error). Help by favor, thank you very much!

This is my Logcat

   D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.matias.tikon3, PID: 10514
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.matias.tikon3/com.example.matias.tikon3.Inicio}: android.content.res.Resources$NotFoundException: String resource ID #0x1
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2309)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2369)
                      at android.app.ActivityThread.access$800(ActivityThread.java:149)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:135)
Application terminated.

This is my Activity

  public class Inicio extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    TextView txtRut, etiNombre;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inicio);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        // Esto es para iniciar en un fragment
        //FragmentManager fragmentManager = getSupportFragmentManager();
        //fragmentManager.beginTransaction().replace(R.id.contenedor, new ImportFragment()).commit();

        txtRut= findViewById(R.id.tvRut);
        etiNombre= findViewById(R.id.tvNombre);


        Intent intent = getIntent();
        int rut = intent.getIntExtra("rut", 0);
        String nombre = intent.getStringExtra("nombre");
        String apellido = intent.getStringExtra("apellido");
        String correo = intent.getStringExtra("correo");
        String pas = intent.getStringExtra("pas");

        ImportFragment importFragment = new ImportFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("rut", rut);
        importFragment.setArguments(bundle);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.contenedor, importFragment, null);
        fragmentTransaction.commit();

        txtRut.setText(rut + "");
        etiNombre.setText(nombre);

    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        FragmentManager fragmentManager = getSupportFragmentManager();

        if (id == R.id.nav_camera) {
            fragmentManager.beginTransaction().replace(R.id.contenedor, new ImportFragment()).commit();
        } else if (id == R.id.nav_gallery) {
            fragmentManager.beginTransaction().replace(R.id.contenedor, new GalleryFragment()).commit();
        } else if (id == R.id.nav_slideshow) {
            fragmentManager.beginTransaction().replace(R.id.contenedor, new SlideshowFragment()).commit();
        } else if (id == R.id.nav_manage) {
            fragmentManager.beginTransaction().replace(R.id.contenedor, new ToolsFragment()).commit();
        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

And this is the fragment where I want to receive the data and I get an error (and if I have my tvRut in the fragment_import layout)

 public class ImportFragment extends Fragment {

        TextView txtRut;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if (getArguments() != null) {

                int rut = getArguments().getInt("rut", 0);

            }
        }

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

            // Inflate the layout for this fragment
            View vista = inflater.inflate(R.layout.fragment_import, container, false);

            int rut = getArguments().getInt("rut", 0);

            txtRut= vista.findViewById(R.id.tvRut);

            txtRut.setText(rut);


            return vista;
        }

    }
    
asked by Matías Nicolás Núñez Rivas 01.03.2018 в 21:45
source

1 answer

0

The error:

  

java.lang.RuntimeException: Unable to start activity   ComponentInfo {com.example.matias.tikon3 / com.example.matias.tikon3.Home}:   android.content.res.Resources $ NotFoundException: String resource ID   0x1

This is because your application tries to obtain a resource because you use the setText() method with an int value, you must assign a String value to avoid this error.

        ...
        int rut = getArguments().getInt("rut", 0);
        ....
        txtRut.setText(rut);
        ....

In this way you can convert the value to type String to avoid this problem and display the value in your TextView :

  txtRut.setText(String.valueOf(rut));
    
answered by 02.03.2018 / 19:42
source