我想将数据从主要活动传递到TabView的一个标签。这是我的代码,它不起作用,现在该做什么
我的代码 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);
}
});
}
}
我的适用于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;
}
}
这是我想在主要活动中传递EditText数据的ProfileActivity:
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;
}
}
当我运行应用程序时,应用程序停止工作并在logcat中出现此错误:
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)
答案 0 :(得分:2)
你不能那样使用setArguments
。
这样做:
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);
}
});
然后在Tab
活动中,将额外内容传递给适配器,无论你在哪里调用它,如下所示:
PagerAdapter adapter = new PagerAdapter(fm, user);
并对适配器类进行以下更改:
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;
}
}
答案 1 :(得分:0)
问题是你试图在`
中的片段中添加libc
TabActivity
bundle
Pager`您再次宣布没有
setArguments()
。
您需要做的就是先将and in
发送到bundle
,然后在activity
中使用ProfileActivity
片段,然后将Pager
设置为bundle
片段。
答案 2 :(得分:0)
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);
}
});
}
}
和
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;
}
}