Passing value between activity to fragment on andr

2020-04-18 07:14发布

On Activity:

@Override
 public void onTabSelected(Tab tab, FragmentTransaction ft) {
 // on tab selected
 // show respected fragment view
  Log.v("Test", "Menjalankan intent antar activity ke fragment");
  Bundle bundle=new Bundle();
  //String message= "From activity";
  int nilai=10;
  bundle.putInt("Nilai", nilai);
  Log.v("Testing", "Data yang akan dikirim "+nilai);
    //set Fragmentclass Arguments
  Lirik f=new Lirik();
  f.setArguments(bundle);

  viewPager.setCurrentItem(tab.getPosition());
 }

Fragment code:

package com.example.egmim;

import com.example.support.dbAdapter;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;



public class Lirik extends Fragment{


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.lirik_view, container, false);

        Log.v("Testing", "Menjalankan proses pengambilan data dari activity");

        Bundle bundle = getArguments();
          if (bundle != null) {
              Log.v("Testing", "Data adalah ");
          }else{
              Log.v("Testing", "Data null");
          }



        //searchonDB();
        return rootView;
    }





}

When compiler said: null exception on line 25. (java.lang.nullexception) and the line 25 is

  int strtext=getArguments().getInt("Nilai", 0);

What's wrong?

01-10 06:39:53.734: V/Testing(4097): Proses Intent dijalankan, nomor yang dipilih adalah 1

01-10 06:39:54.144: V/Test(4097): Menjalankan intent antar activity ke fragment

01-10 06:39:54.154: V/Testing(4097): Data yang akan dikirim 10

01-10 06:39:54.845: V/Testing(4097): Menjalankan proses pengambilan data dari activity

01-10 06:39:54.875: D/AndroidRuntime(4097): Shutting down VM

01-10 06:39:54.894: W/dalvikvm(4097): threadid=1: thread exiting with uncaught exception (group=0x41465700)

01-10 06:39:55.104: E/AndroidRuntime(4097): FATAL EXCEPTION: main

01-10 06:39:55.104: E/AndroidRuntime(4097): java.lang.NullPointerException

01-10 06:39:55.104: E/AndroidRuntime(4097):     at com.example.egmim.Lirik.onCreateView(Lirik.java:24)

01-10 06:39:55.104: E/AndroidRuntime(4097):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)

3条回答
姐就是有狂的资本
2楼-- · 2020-04-18 07:32

Your Text is :

int strtext=getArguments().getInt("Nilai", 0);

It should Be:

int strtext=getArguments().getInt("nilai", 0);

Edit :

Check Out this Question which is Related to your Problem:

Here

查看更多
够拽才男人
3楼-- · 2020-04-18 07:33
    public class MyFragment extends Fragment {
        int strtext;

     @Override
    public void onCreate(Bundle savedInstanceState) 
          {
            super.onCreate(savedInstanceState);
            Bundle data = getArguments();
            strtext= data.getInt("Nilai");
          }

    @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
      View rootView = inflater.inflate(R.layout.lirik_view, container, false);
      Log.v("Testing", "Menjalankan proses pengambilan data dari activity");

      Log.v("Testing", "Data adalah "+strtext);
      return rootView;
     }
    }       
查看更多
▲ chillily
4楼-- · 2020-04-18 07:35

From Activity you send data with intent as:

Bundle bundle=new Bundle();
bundle.putString("name", "From Activity");
  //set Fragmentclass Arguments
Fragmentclass fragobj=new Fragmentclass();
fragobj.setArguments(bundle);

and in Fragment onCreateView method:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
          String strtext=getArguments().getString("name");

    return inflater.inflate(R.layout.fragment, container, false);
    }

Or Use constructor in fragment to pass values at fragment object initialization.

查看更多
登录 后发表回答