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

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

本文介绍了在android上将活动之间的值传递给片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开启活动:

@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行是

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

  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)


推荐答案

来自活动你意图发送数据为:

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

和Fragment onCreateView方法:

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.

这篇关于在android上将活动之间的值传递给片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:05