2014-04-21 39 views
0

我有一個帶有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(本機方法)

當運行此代碼,我得到一個空指針異常。任何知道我在做什麼錯的人?

+0

你在哪裏得到的NPE? – zgc7009

+0

getArguments()返回null – user3557718

+0

嘗試更改fragment.setArguments(args);到fragment.putExtras(args);和getArguments()。getInt(「position」); getIntent()。getExtras()。getInt(「position」); – zgc7009

回答

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 ,它必須從喲擴展片段烏爾支持庫(進口android.support.v4.app.Fragment;),因爲你的FragmentActivity是從支持庫,你可以在創建活動後

public void onActivityCreated(Bundle savedInstanceState){ 
    getArguments().getInt("positon", 0); 
    //do whatever 
} 

你是通過你的包通過你的意圖,這樣訪問,你在通過你的意圖開始的活動中得到它,然後你從它包含Activity中設置你的片段的參數。

+0

感謝您的努力,但它仍然無法正常工作。在EditBundle中,我正確地接收了位置,但是如果我想將它傳遞給EditSms,我仍然會得到de nullpointer ... – user3557718

+0

@ user3557718檢查我的編輯。我認爲問題在於,一旦你設置了參數,你從來沒有真正將你的片段添加到堆棧,所以參數仍然被設置爲默認值。 – zgc7009

+0

我得到一個錯誤與替換:「更改類型的'片段'片段」。那我有沒有做錯別的? – user3557718

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; 
    } 

在我的片段類,EditSms,我所說的getter和獲取位置

EditBundle b = (EditBundle)getActivity(); 
     Log.d("pos", b.getPosition() + ""); 
相關問題