2014-10-07 17 views
1

我有应用程序viewpager.I需要调用其他片段方法,当我点击第一个片段上的按钮。getSupportFragmentManager()。findFragmentById给了我一个nullPointer

这是我的邮件活动

view plainprint? 
Note: Text content in the code blocks is automatically word-wrapped 
package src.com.programmingmobile.pageviewer; 

package src.com.programmingmobile.pageviewer; 

import java.util.ArrayList; 
import java.util.List; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.widget.Toast; 

public class PageViewActivity extends FragmentActivity implements Communicator{ 
    MyPageAdapter pageAdapter; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     try{ 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_page_view);   
     List<Fragment> fragments = getFragments();   
     pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);   
     ViewPager pager = (ViewPager)findViewById(R.id.viewpager); 
     pager.setAdapter(pageAdapter); 

     }catch(Exception e){ 
      e.printStackTrace(); 
     } 

    } 

    private List<Fragment> getFragments(){ 
     List<Fragment> fList = new ArrayList<Fragment>(); 

     fList.add(MyFragment.newInstance("Fragment 1")); 
     fList.add(MyFragment2.newInstance("Fragment 2")); 
     //fList.add(MyFragment.newInstance("Fragment 3")); 

     return fList; 
    } 

    private class MyPageAdapter extends FragmentPagerAdapter { 

     private List<Fragment> fragments; 

     public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) { 
      super(fm); 
      this.fragments = fragments; 
     } 
     @Override 
     public Fragment getItem(int position) { 
      return this.fragments.get(position); 
     } 

     @Override 
     public int getCount() { 
      return this.fragments.size(); 
     } 
    } 

    @Override 
    public void respond(String data) { 
     // TODO Auto-generated method stub 
     MyFragment2 fragment = (MyFragment2) getSupportFragmentManager().findFragmentById(R.id.Fragment2); 
     fragment.print(); 

    } 
} 

这是我的片段1

view plainprint? 
Note: Text content in the code blocks is automatically word-wrapped 
package src.com.programmingmobile.pageviewer; 

import java.io.InputStream; 

import javax.net.ssl.ManagerFactoryParameters; 

import com.epapyrus.plugpdf.SimpleDocumentReader; 
import com.epapyrus.plugpdf.SimpleDocumentReaderListener; 
import com.epapyrus.plugpdf.SimpleReaderFactory; 
import com.epapyrus.plugpdf.core.viewer.DocumentState; 

import android.app.Activity; 
import android.content.res.AssetManager; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
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.LinearLayout; 
import android.widget.TextView; 

public class MyFragment extends Fragment{ 
    public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE"; 

    Communicator com; 

    public void onAttach(Activity a) { 
     super.onAttach(a); 
     com = (Communicator) a; 
    } 

    public static final MyFragment newInstance(String message) { 
     MyFragment f = new MyFragment(); 
     Bundle bdl = new Bundle(1); 
     bdl.putString(EXTRA_MESSAGE, message); 
     f.setArguments(bdl); 
     return f; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     String message = getArguments().getString(EXTRA_MESSAGE); 
     View v = inflater.inflate(R.layout.myfragment_layout, container, false); 
     TextView messageTextView = (TextView) v.findViewById(R.id.textView); 
     messageTextView.setText(message); 
     // OpenPdf(); 

     LinearLayout mLinearLayout = (LinearLayout) inflater.inflate( 
       R.layout.myfragment_layout, container, false); 

     // note that we're looking for a button with id="@+id/myButton" in your 
     // inflated layout 
     // Naturally, this can be any View; it doesn't have to be a button 
     Button mButton = (Button) mLinearLayout.findViewById(R.id.button1); 
     mButton.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       com.respond("Fragment "); 
      } 
     }); 

     return mLinearLayout; 
    } 



} 

这是fragment2

view plainprint? 
Note: Text content in the code blocks is automatically word-wrapped 
package src.com.programmingmobile.pageviewer; 

import java.io.InputStream; 

import com.epapyrus.plugpdf.SimpleDocumentReader; 
import com.epapyrus.plugpdf.SimpleDocumentReaderListener; 
import com.epapyrus.plugpdf.SimpleReaderFactory; 
import com.epapyrus.plugpdf.core.viewer.DocumentState; 

import android.content.res.AssetManager; 
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.ViewGroup; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MyFragment2 extends Fragment { 
    public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE"; 

    // create a listener for receiving provide pdf loading results 
    SimpleDocumentReaderListener m_listener = new SimpleDocumentReaderListener() { 

     @Override 
     public void onLoadFinish(DocumentState.OPEN state) { 
     } 
    }; 

    public static final MyFragment2 newInstance(String message) { 
     MyFragment2 f = new MyFragment2(); 
     Bundle bdl = new Bundle(1); 
     bdl.putString(EXTRA_MESSAGE, message); 
     f.setArguments(bdl); 
     return f; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     String message = getArguments().getString(EXTRA_MESSAGE); 
     View v = inflater.inflate(R.layout.myfragment_layout2, container, false); 
     TextView messageTextView = (TextView) v.findViewById(R.id.textView); 
     messageTextView.setText(message); 
     //OpenPdf(); 
     return v; 
    } 

    public void print(){ 
     Toast.makeText(getActivity(), " inside Respond ", 100); 
    } 

} 

这是myfragment_layout xml文件

view plainprint? 
Note: Text content in the code blocks is automatically word-wrapped 
package src.com.programmingmobile.pageviewer; 

import java.io.InputStream; 

import javax.net.ssl.ManagerFactoryParameters; 

import com.epapyrus.plugpdf.SimpleDocumentReader; 
import com.epapyrus.plugpdf.SimpleDocumentReaderListener; 
import com.epapyrus.plugpdf.SimpleReaderFactory; 
import com.epapyrus.plugpdf.core.viewer.DocumentState; 

import android.app.Activity; 
import android.content.res.AssetManager; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

public class MyFragment extends Fragment { 
    public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE"; 

    Communicator com; 

    public void onAttach(Activity a) { 
     super.onAttach(a); 
     com = (Communicator) com; 
    } 

    public static final MyFragment newInstance(String message) { 
     MyFragment f = new MyFragment(); 
     Bundle bdl = new Bundle(1); 
     bdl.putString(EXTRA_MESSAGE, message); 
     f.setArguments(bdl); 
     return f; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     String message = getArguments().getString(EXTRA_MESSAGE); 
     View v = inflater.inflate(R.layout.myfragment_layout, container, false); 
     TextView messageTextView = (TextView) v.findViewById(R.id.textView); 
     messageTextView.setText(message); 
     // OpenPdf(); 
     return v; 
    } 

    public void onclick(View v) { 

     com.respond("Fragment "); 
    } 

} 

这是my_fragment2 XML filee

view plainprint? 
Note: Text content in the code blocks is automatically word-wrapped 
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/Fragment2" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:textAppearance="?android:attr/textAppearanceLarge"/> 

</RelativeLayout> 

当我运行它给我一个button.when所述第一片段中的应用程序,我clcick按钮它给我以下错误

view plainprint? 
Note: Text content in the code blocks is automatically word-wrapped 
10-07 09:37:52.610: E/AndroidRuntime(10907): FATAL EXCEPTION: main 
10-07 09:37:52.610: E/AndroidRuntime(10907): java.lang.NullPointerException 
10-07 09:37:52.610: E/AndroidRuntime(10907): at src.com.programmingmobile.pageviewer.PageViewActivity.respond(PageViewActivity.java:66) 
10-07 09:37:52.610: E/AndroidRuntime(10907): at src.com.programmingmobile.pageviewer.MyFragment$1.onClick(MyFragment.java:63) 
10-07 09:37:52.610: E/AndroidRuntime(10907): at android.view.View.performClick(View.java:3620) 
10-07 09:37:52.610: E/AndroidRuntime(10907): at android.view.View$PerformClick.run(View.java:14322) 
10-07 09:37:52.610: E/AndroidRuntime(10907): at android.os.Handler.handleCallback(Handler.java:605) 
10-07 09:37:52.610: E/AndroidRuntime(10907): at android.os.Handler.dispatchMessage(Handler.java:92) 
10-07 09:37:52.610: E/AndroidRuntime(10907): at android.os.Looper.loop(Looper.java:137) 
10-07 09:37:52.610: E/AndroidRuntime(10907): at android.app.ActivityThread.main(ActivityThread.java:4507) 
10-07 09:37:52.610: E/AndroidRuntime(10907): at java.lang.reflect.Method.invokeNative(Native Method) 
10-07 09:37:52.610: E/AndroidRuntime(10907): at java.lang.reflect.Method.invoke(Method.java:511) 
10-07 09:37:52.610: E/AndroidRuntime(10907): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978) 
10-07 09:37:52.610: E/AndroidRuntime(10907): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745) 
10-07 09:37:52.610: E/AndroidRuntime(10907): at dalvik.system.NativeStart.main(Native Method) 

这给该生产线空指针低于

view plainprint? 
Note: Text content in the code blocks is automatically word-wrapped 
MyFragment2 fragment = (MyFragment2) getSupportFragmentManager().findFragmentById(R.id.Fragment2); 

回答

1

source for FragmentPagerAdapter来看,他们使用FragmentManager.findFragmentByTag(name)其中name是在

"android:switcher:" + viewId + ":" + id; 

形式,其中viewIdViewPageridandroid:idgetItemId(position)的结果(在你的情况可能只是位置)。

注意:这是不安全的,可能会从支持库的版本更改。它在所有以前的版本中保持不变,但是您应该考虑在两个组件(技术上讲,在您的第一个片段,活动,然后是第二个片段之间)setting up an event callback interface

+0

如何获取片段的位置? – 2014-10-07 03:22:41

+0

你写了'MyPageAdapter'的'getItem(int position)'对吗?我想如果你正在寻找一个特定的片段,它应该很容易找到。 – ianhanniballake 2014-10-07 03:25:38

+0

如何调用适配器getItem方法。我尝试如下,它给我错误。 MyFragment2 fragment =(MyFragment2)getSupportFragmentManager()。findFragmentByTag(R.id.viewpager + String.valueOf(getItem(0))); 。它说Methos是未定义的 – 2014-10-07 03:40:20

相关问题