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)
Your Text is :
It should Be:
Edit :
Check Out this Question which is Related to your Problem:
Here
From Activity you send data with intent as:
and in Fragment onCreateView method:
Or Use constructor in fragment to pass values at fragment object initialization.