我有一个带有listview的活动类。如果我点击列表视图的一个项目,我想转到我的fragmentactivity类并在一个包中传递一些信息。
这是我的Activity类(ProviderList)中带有te listview的代码。 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):在android.support.v4.app.Fragment.performCreateView(Fragment.java:1500) 04-21 19:49:51.541:E / AndroidRuntime(1042):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927) 04-21 19:49:51.541:E / AndroidRuntime(1042):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) 04-21 19:49:51.541:E / AndroidRuntime(1042):在android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 04-21 19:49:51.541:E / AndroidRuntime(1042):在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467) 04-21 19:49:51.541:E / AndroidRuntime(1042):在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):在android.support.v4.view.ViewPager.populate(ViewPager.java:1068) 04-21 19:49:51.541:E / AndroidRuntime(1042):在android.support.v4.view.ViewPager.populate(ViewPager.java:914) 04-21 19:49:51.541:E / AndroidRuntime(1042):在android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436) 04-21 19:49:51.541:E / AndroidRuntime(1042):在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):在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):at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302) 04-21 19:49:51.541:E / AndroidRuntime(1042):在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 com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2189) 04-21 19:49:51.541:E / AndroidRuntime(1042):在android.view.View.measure(View.java:15848) 04-21 19:49:51.541:E / AndroidRuntime(1042):在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):在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):在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:749) 04-21 19:49:51.541:E / AndroidRuntime(1042):在android.view.Choreographer.doCallbacks(Choreographer.java:562) 04-21 19:49:51.541:E / AndroidRuntime(1042):在android.view.Choreographer.doFrame(Choreographer.java:532) 04-21 19:49:51.541:E / AndroidRuntime(1042):在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:735) 04-21 19:49:51.541:E / AndroidRuntime(1042):在android.os.Handler.handleCallback(Handler.java:730) 04-21 19:49:51.541:E / AndroidRuntime(1042):在android.os.Handler.dispatchMessage(Handler.java:92) 04-21 19:49:51.541:E / AndroidRuntime(1042):在android.os.Looper.loop(Looper.java:137) 04-21 19:49:51.541:E / AndroidRuntime(1042):在android.app.ActivityThread.main(ActivityThread.java:5103) 04-21 19:49:51.541:E / AndroidRuntime(1042):at 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):at dalvik.system.NativeStart.main(Native Method)
当我运行此代码时,我得到一个nullpointer异常。谁知道我做错了什么?
答案 0 :(得分:0)
我不确定这一点,可能需要在我做一些改变之后编辑一些东西,但我认为你需要做这样的事情。
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Bundle args = new Bundle();
args.putInt("position", position);
Intent i = new Intent(ProviderList.this, EditBundle.class);
i.putExtras(args);
startActivity(i);
}
然后在EditBundle的onCreate
中Bundle in = getIntent().getExtras();
EditSms fragment = new EditSms();
fragment.setArguments(in);
FragmentManager fm = getSupportFragmentManager();
然后,如果你还没有添加你的片段
fm.beginTransaction().replace(R.id.container, fragment).commit();
如果不是
fm.beginTransaction().add(R.id.container, fragment).commit();
在EditSms中,必须从支持库扩展Fragment(导入android.support.v4.app.Fragment;),因为您的FragmentActivity来自支持库,您可以在创建活动后访问
public void onActivityCreated(Bundle savedInstanceState){
getArguments().getInt("positon", 0);
//do whatever
}
这样你就可以通过你的意图传递你的bundle了,你是通过你的意图开始的活动中获得它,然后你设置你的片段的参数包含Activity。
答案 1 :(得分:0)
我已经研究过一个解决方案,我想我找到了一个解决方案。也许它不是最好的,但现在我觉得它很好。
活动类:
Bundle basket = new Bundle();
basket.putInt("position", position);
Intent i = new Intent(ProviderList.this, EditBundle.class);
i.putExtras(basket);
startActivity(i);
EditBundle
Bundle gotBasket = getIntent().getExtras();
position = gotBasket.getInt("position");
而且我也为位置做了一个吸气剂。
public int getPosition(){
return position;
}
在我的Fragment类EditSms中,我调用getter并获取位置
EditBundle b = (EditBundle)getActivity();
Log.d("pos", b.getPosition() + "");