本人的MainActivity的代码如下:
public class MainActivity extends AppCompatActivity { private ViewPager mViewPage; private MainPagerAdapter mPagerAdapter; private ChatPageFragment mChatPage = new ChatPageFragment(); private MusicGroundFragment mMusicGround = new MusicGroundFragment(); private ContactPageFragment mContactPage = new ContactPageFragment(); private RecyclerView mChatRecycleView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //一些无关代码 initView(); } private void initView(){ mViewPage = (ViewPager) findViewById(R.id.main_view_pager); //mChatRecycleView = (RecyclerView) findViewById(R.id.chat_list_view); FragmentTransaction ft= getSupportFragmentManager().beginTransaction(); ft.add(R.id.main_view_pager,mChatPage); ft.add(R.id.main_view_pager,mMusicGround); ft.add(R.id.main_view_pager,mContactPage); ft.commit(); mChatRecycleView = mChatPage.getRecycleView(); mChatRecycleView.setLayoutManager(new LinearLayoutManager(this)); mChatRecycleView.setAdapter(new ChatPageAdapter(this)); ArrayList<Fragment> fragments = new ArrayList<>(); fragments.add(mChatPage); fragments.add(mMusicGround); fragments.add(mContactPage); mPagerAdapter = new MainPagerAdapter(fragments); mViewPage.setAdapter(mPagerAdapter); } //一些自动生成的代码 }
本人的activity_main的代码如下:
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context="com.f1993xuboss.musicpark.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/main_view_pager"> <android.support.v4.view.PagerTitleStrip android:id="@+id/pager_title_strip" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" > <ImageSwitcher android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/chat_title_view" android:layout_marginLeft="0dp"/> <ImageSwitcher android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/contact_title_view" android:layout_marginRight="0dp"/> </android.support.v4.view.PagerTitleStrip> </android.support.v4.view.ViewPager> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" app:srcCompat="@android:drawable/ic_dialog_email" /> </android.support.design.widget.CoordinatorLayout>
本人的ChatPageFragment的代码如下:
public class ChatPageFragment extends Fragment { private RecyclerView recyclerView; public RecyclerView getRecycleView(){return recyclerView;} @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.chat_list_view,container,false); this.recyclerView = (RecyclerView) view.findViewById(R.id.chat_list_view); return view; } }
本人的chat_list_view的代码如下:
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/chat_list_view"> </android.support.v7.widget.RecyclerView>
可能没什么关系,本人的ChatPageAdapter的代码如下:
public class ChatPageAdapter extends RecyclerView.Adapter<ChatPageAdapter.ChatItemViewHolder> { private Context mContext; public ChatPageAdapter(Context context){ this.mContext = context; } @Override public ChatItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(mContext).inflate(R.layout.chat_item,parent,false); return new ChatItemViewHolder(view); } @Override public void onBindViewHolder(ChatItemViewHolder holder, int position) { //TODO:add holder behavior to adjust the data from the internet } @Override public int getItemCount() { return 3; } class ChatItemViewHolder extends RecyclerView.ViewHolder { ImageView mImageView; TextView mPersonName; TextView mChatContext; TextView mData; public ChatItemViewHolder(View itemView) { super(itemView); mImageView = (ImageView) itemView.findViewById(R.id.chat_item_person); mPersonName = (TextView) itemView.findViewById(R.id.chat_item_person_name); mChatContext = (TextView) itemView.findViewById(R.id.chat_item_context); mData = (TextView) itemView.findViewById(R.id.chat_item_data); } } }
本人的log如下:
FATAL EXCEPTION: main
Process: com.f1993xuboss.musicpark, PID: 2633
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.f1993xuboss.musicpark/com.f1993xuboss.musicpark.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method “void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)” on a null object reference
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method “void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)” on a null object reference
at com.f1993xuboss.musicpark.MainActivity.initView(MainActivity.java:64)
at com.f1993xuboss.musicpark.MainActivity.onCreate(MainActivity.java:51)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
本人试了一下,网上说的常规方法是findViewById的方法不行,原因是本人activity_main.xml中没包含,所以本人想在fragment函数中获取,但是屡试屡败,高手们有什么解决方法吗?或换个思路也行。
10
30
60
mChatRecycleView.setLayoutManager(new LinearLayoutManager(getActivity())); mChatRecycleView.setAdapter(new ChatPageAdapter(getActivity()
));