我有一個帶有listview的活動類。如果我點擊一個listview的項目,我想去我的fragmentactivity類,並傳遞一些信息在一個包中。從我的激活類發送數據到片段類(選項卡)
這是在我的活動類(ProviderList)與列表視圖中的代碼。 EditSms是一個片段類(一個選項卡)。 EditBundle是創建選項卡的fragmentActivity類。
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
EditSms fragment = new EditSms();
Bundle args = new Bundle();
args.putInt("position", position);
fragment.setArguments(args);
Intent i = new Intent(ProviderList.this, EditBundle.class);
startActivity(i);
}
在我的片段類的EditText我有以下代碼:
public class EditSms extends Fragment implements OnClickListener {
private Button bChangeSMS;
private EditText etSmsEN;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View editsms = inflater.inflate(R.layout.sms_frag, container, false);
int position = getArguments().getInt("position");
Log.d("test", "position: " + position);
bChangeSMS = (Button)editsms.findViewById(R.id.bAdjustSMS);
etSmsEN = (EditText)editsms.findViewById(R.id.etSmsEN);
bChangeSMS.setOnClickListener(this);
return editsms;
}
EditBundle:
package com.example.mobieleplatformen;
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
public class EditBundle extends FragmentActivity {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
@Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
setContentView(R.layout.editbundle);
int position = getIntent().getExtras().getInt("position");
Log.d("test", position + "");
Bundle in = getIntent().getExtras();
EditSms fragment = new EditSms();
fragment.setArguments(in);
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.pager, fragment).commit();
TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager) findViewById(R.id.pager);
Tab.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position);
}
});
Tab.setAdapter(TabAdapter);
actionBar = getActionBar();
// Enable Tabs on Action Bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
@Override
public void onTabReselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Tab.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
};
// Add New Tab
actionBar.addTab(actionBar.newTab().setText("Call")
.setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("SMS")
.setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Data")
.setTabListener(tabListener));
}
}
EditSms:
package com.example.mobieleplatformen;
import java.util.ArrayList;
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.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class EditSms extends Fragment implements
OnClickListener {
private Button bChangeSMS;
private EditText etSmsEN;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View editsms = inflater.inflate(R.layout.sms_frag, container, false);
int position = getArguments().getInt("position", 0);
Log.d("test", "position: ");
bChangeSMS = (Button) editsms.findViewById(R.id.bAdjustSMS);
etSmsEN = (EditText) editsms.findViewById(R.id.etSmsEN);
bChangeSMS.setOnClickListener(this);
return editsms;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
的logcat:
logcat的: 04-21 19:49:51.541:E/AndroidRuntime(1042):致命異常:主 04-21 19:49:51.541:E/AndroidRuntime(1042):顯示java.lang.NullPointerException 04 -21 19:49:51.541:E/AndroidRuntime(1042):at com.example.mobieleplatformen.EditSms.onCreateView(EditSms.java:25) 04-21 19:49:51.541:E/AndroidRuntime(1042):at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500) 04-21 19:49:51.541:E/AndroidRuntime(1042):at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager。 java:927) 04-21 19:49:51.541:E/AndroidRuntime(1042):at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) 04-21 19:49:51.541: E/AndroidRuntime(1042):在android.sup E:// AndroidRuntime(1042):at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java: 1467) 04-21 19:49:51.541:E/AndroidRuntime(1042):at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472) 04-21 19:49:51.541:E/AndroidRuntime(1042):在android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163) 04-21 19:49:51.541:E/AndroidRuntime(1042):at android.support.v4.view。 ViewPager.populate(ViewPager.java:1068) 04-21 19:49:51.541:E/AndroidRuntime(1042):at android.support.v4.view.ViewPager.populate(ViewPager.java:914) 04-21 19:49:51.541:E/AndroidRuntime(1042):at android.support.v4.view.ViewPager.onMeasure(ViewPage r.java:1436) 04-21 19:49:51.541:E/AndroidRuntime(1042):at android.view.View.measure(View.java:15848) 04-21 19:49:51.541:E/AndroidRuntime(1042):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012) 04-21 19:49:51.541:E/AndroidRuntime(1042):在android.widget.FrameLayout.onMeasure(FrameLayout.java: 310) 04-21 19:49:51.541:E/AndroidRuntime(1042):at android.view.View.measure(View.java:15848) 04-21 19:49:51.541:E/AndroidRuntime(1042) :at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012) 04-21 19:49:51.541:E/AndroidRuntime(1042):at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java: 302) 04-21 19:49:51.541:E/AndroidRuntime(1042):at android.view.View.measure(View.java:15848)(E)/ AndroidRuntime(1042):at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 04-21 19:49:51。541:E/AndroidRuntime(1042):at com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2189) 04-21 19:49:51.541:E/AndroidRuntime(1042):at android.view.View.measure(View.java:15848) 04-21 19:49:51.541:E/AndroidRuntime(1042):at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905) 04-21 19:49:51.541:E/AndroidRuntime(1042):在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104) 04-21 19:49:51.541:E/AndroidRuntime(1042):在android.view。 ViewRootImpl.performTraversals(ViewRootImpl.java:1284) 04-21 19:49:51.541:E/AndroidRuntime(1042):at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004) 04-21 19:49: 51.541:E/AndroidRuntime(1042):在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5481) 04-21 19:49:51.541:E/AndroidRuntime(1042):at android.view.Choreographer $ CallbackRecord.run(Choreographer.java:749) 04-21 19:49:51.541:E/AndroidRuntime(1042) :at android.view.Choreographer.doCallbacks(Choreographer.java:562) 04-21 19:49:51.541:E/AndroidRuntime(1042):at android.view.Choreographer.doFrame(Choreographer.java:532) 04 -21 19:49:51.541:E/AndroidRuntime(1042):at android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:735) 04-21 19:49:51.541:E/AndroidRuntime(1042):at android.os.Handler.handleCallback(Handler.java:730) 04-21 19:49:51.541:E/AndroidRuntime(1042):at android.os.Handler.dispatchMessage(Handler.java:92) 04-21 19:49:51.541:E/AndroidRuntime(1042):at android.os.Looper.loop(Looper.java:137) 04-21 19:49:5 1.541:E/AndroidRuntime(1042):在android.app.ActivityThread.main(ActivityThread.java:5103) 04-21 19:49:51.541:E/AndroidRuntime(1042):在java.lang.reflect.Method。 invokeNative(Native Method) 04-21 19:49:51.541:E/AndroidRuntime(1042):at java.lang.reflect.Method.invoke(Method.java:525) 04-21 19:49:51.541:E/AndroidRuntime(1042):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737) 04-21 19:49:51.541:E/AndroidRuntime(1042):at com.android.internal .os.ZygoteInit.main(ZygoteInit.java:553) 04-21 19:49:51.541:E/AndroidRuntime(1042):在dalvik.system.NativeStart.main(本機方法)
當運行此代碼,我得到一個空指針異常。任何知道我在做什麼錯的人?
你在哪裏得到的NPE? – zgc7009
getArguments()返回null – user3557718
嘗試更改fragment.setArguments(args);到fragment.putExtras(args);和getArguments()。getInt(「position」); getIntent()。getExtras()。getInt(「position」); – zgc7009