在android上将活动之间的值传递给片段

时间:2014-01-10 11:27:08

标签: java android android-fragments android-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());
 }

片段代码:

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;
    }





}

当编译器说:第25行的null异常时。(java.lang.nullexception) 第25行是

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

怎么了?

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 个答案:

答案 0 :(得分:7)

从活动中您按意图发送数据:

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

并在Fragment onCreateView方法中:

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

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

或者使用fragment中的构造函数在片段对象初始化时传递值。

答案 1 :(得分:2)

您的文字是:

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

它应该是:

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

编辑:

查看与您的问题相关的问题:

Here

答案 2 :(得分:0)

    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;
     }
    }