我从调用片段类的活动时遇到问题。更多:来自片段的调用活动
XML适配器的布局:从主要活动
<TextView
style="@style/LiHeadLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:onClick="openProfile"
android:id="@+id/genFrom" />
代码调用片段:
public void openProfile(View v) {
View row = (View) v.getParent();
TextView child2 = (TextView) row.findViewById(R.id.genFromlogin);
String child3 = child2.getText().toString();
Intent ini = getActivity().getIntent();
String c_username = ini.getStringExtra(MainActivity.KEY_USERNAME);
String c_password = ini.getStringExtra(MainActivity.KEY_PASSWORD);
Intent ini2 = new Intent(context, User.class);
ini2.putExtra(MainActivity.KEY_USERNAME, c_username);
ini2.putExtra(MainActivity.KEY_PASSWORD, c_password);
ini2.putExtra(MainActivity.KEY_USER, child3);
getActivity().startActivity(ini2);
}
错误:
public void openProfile(View v) {
Fragment_Questions frau = new Fragment_Questions();
frau.openProfile(v);
}
从片段类代码
java.lang.IllegalStateException: Could not execute method for android:onClick
...
...
...
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Intent android.support.v4.app.FragmentActivity.getIntent()' on a null object reference
P.S. 1.应用打开默认的活动 2.用户调用一个新的活动以传递意图数据 3.新的活动呼吁片段
第1(主)活动:
Intent intent_settings = getIntent();
String c_username = intent_settings.getStringExtra(MainActivity.KEY_USERNAME);
String c_password = intent_settings.getStringExtra(MainActivity.KEY_PASSWORD);
Intent intent_settings_1 = new Intent(this, NewFeed.class);
intent_settings_1.putExtra(MainActivity.KEY_USERNAME, c_username);
intent_settings_1.putExtra(MainActivity.KEY_PASSWORD, c_password);
intent_settings_1.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent_settings_1);
好了,你应该知道所有片段都由带有制表符布局的viewpager调用。
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Fragment_Home tab1 = new Fragment_Home();
return tab1;
case 1:
Fragment_Questions tab2 = new Fragment_Questions();
return tab2;
default:
return null;
}
}
您将添加的意图这使tringExtra –
@DasserBasyouni是的,我已经添加了 – HEISENBERG