Reputation: 41
I want to pass data from main activity to one tab of TabView.Here is my code and it isn't working,what to do now
There is my codes MainActivity :
public class MainActivity extends AppCompatActivity {
Button button;
EditText text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button= (Button) findViewById(R.id.getInfoButton);
text= (EditText) findViewById(R.id.gitHubUserName);
Logger.addLogAdapter(new AndroidLogAdapter());
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String user = text.getText().toString();
Intent in=new Intent(MainActivity.this,Tab.class);
in.putExtra("SS",user);
Logger.d(user);
Bundle bundle=new Bundle();
bundle.putString("NAME",user);
ProfileActivity pro = new ProfileActivity();
pro.setArguments(bundle);
startActivity(in);
}
});
}
}
my Adapter for tabViews :
public class PagerAdapter extends FragmentPagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
ProfileActivity profileActivity=new ProfileActivity();
return profileActivity;
case 1:
FollowersActivity followersActivity=new FollowersActivity();
return followersActivity;
case 2:
PublicRepos publicRepos=new PublicRepos();
return publicRepos;
default:
return null;
}
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Profile Info";
case 1:
return "Followers";
case 2:
return "Public Repos";
}
return null;
}
}
and this is ProfileActivity where I want to pass the data which I got from EditText in Main Activity :
public class ProfileActivity extends Fragment {
ImageView imageView;
TextView name;
TextView bio;
String text;
public void dataPass(String txt){
text = txt;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_profile, container, false);
name = rootView.findViewById(R.id.name);
bio = rootView.findViewById(R.id.bio);
Logger.addLogAdapter(new AndroidLogAdapter());
Bundle bundle = this.getArguments();
String nameString = bundle.getString("NAME");
Logger.d(nameString);
name.setText(nameString);
return rootView;
}
}
when I run the app, app stopped working and in logcat this error :
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at com.salman.getgithubofgeek.Activity.Activity.Activites.ProfileActivity.onCreateView(ProfileActivity.java:43)
Upvotes: 1
Views: 481
Reputation: 4121
Button button;
EditText text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button= (Button) findViewById(R.id.getInfoButton);
text= (EditText) findViewById(R.id.gitHubUserName);
Logger.addLogAdapter(new AndroidLogAdapter());
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String user = text.getText().toString();
Intent in=new Intent(MainActivity.this,Tab.class);
in.putExtra("SS",user);
Logger.d(user);
//modification goes here
intent.putString("NAME",user);
startActivity(in);
}
});
}
}
and
public PagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
ProfileActivity profileActivity=new ProfileActivity();
//modifications goes here
Bundle b = new Bundle();
bundle.putString("NAME",user);
profileActivity.setArguments(b);
return profileActivity;
case 1:
FollowersActivity followersActivity=new FollowersActivity();
return followersActivity;
case 2:
PublicRepos publicRepos=new PublicRepos();
return publicRepos;
default:
return null;
}
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Profile Info";
case 1:
return "Followers";
case 2:
return "Public Repos";
}
return null;
}
}
Upvotes: 0
Reputation: 42
You cannot use setArguments
like that.
Do this:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String user = text.getText().toString();
Intent in=new Intent(MainActivity.this,Tab.class);
in.putExtra("SS",user);
in.putExtra("NAME",user);
Logger.d(user);
startActivity(in);
}
});
then in Tab
activity, pass the extra to the adapter wherever you call it like so:
PagerAdapter adapter = new PagerAdapter(fm, user);
and make the following changes to the adapter class:
public class PagerAdapter extends FragmentPagerAdapter {
String nameString;
public PagerAdapter(FragmentManager fm, String nameString) {
super(fm);
this.nameString = nameString;
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
ProfileActivity profileActivity=new ProfileActivity();
Bundle bundle=new Bundle();
bundle.putString("NAME",nameString);
profileActivity.setArguments(bundle);
return profileActivity;
case 1:
FollowersActivity followersActivity=new FollowersActivity();
return followersActivity;
case 2:
PublicRepos publicRepos=new PublicRepos();
return publicRepos;
default:
return null;
}
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Profile Info";
case 1:
return "Followers";
case 2:
return "Public Repos";
}
return null;
}
}
Upvotes: 2
Reputation: 4121
The problem is you are trying to add the bundle
in fragment that is in `
TabActivity
and in
Pager` you are again declaring ProfileActivity fragment that is with out
setArguments()
.
All you need to do is send the bundle
to activity
first and then before using ProfileActivity
fragment in Pager
set the bundle
to the ProfileActivity
fragment.
Upvotes: 0