Starting Activity by intent from Fragment starts b

2019-08-13 15:32发布

问题:

I could not find an answer to my problem, so I make a new Question.

I have an Android app with a TabLayout (+ ViewPagerAdapter) in my MainActivity and every Tab is a Fragment. Now I want to start a new Activity with Intent from my Fragment. Once I want to open an Activity when pressing a FAB, and another time I want to open another Activity when pressing a FeedItem from my Newsfeed. Now everytime i press something, it opens a completely blank Page.

I do the setContentView in both Activities' onCreate() onto the right Layout, and both Activities (+layout) are working fine on their own when i display them directly in a Tab.

I will put the two Activities I want to call and the Fragment from where I call below, if you need anything else please let me know.

I would be really glad if someone could help me, I need this for my final school project :)

NewsFragment (where my Newsfeed and FAB is):

public class NewsFragment extends ListFragment 
           implements NumberPicker.OnValueChangeListener{

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    ViewGroup v = (ViewGroup)inflater.inflate(R.layout.fragment_news,container,false);

    ... 

    Context con = v.getContext();

    //FAB
    FloatingActionButton fab = (FloatingActionButton) v.findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Click action
            Intent intent = new Intent(con, OfferActivity.class);
            getActivity().startActivity(intent);
        }
    });
}

//Here I watch the Newsfeed Click
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    FeedItem item = (FeedItem) adapter.getItem(position);

    Intent intent = new Intent(getActivity(), BookActivity.class);
    intent.putExtra("item", item);
    intent.putExtra("rideid", rideidArray.get(position));
    intent.putExtra("seats", seatsArray.get(position));
    startActivity(intent);
}
}

This is the Activity I call by the FAB:

public class OfferActivity extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
    setContentView(R.layout.activity_offer);
    ...
}

And the Activity I call by clicking a ListView item:

public class DetailsActivity extends AppCompatActivity implements NumberPicker.OnValueChangeListener{

@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);
    setContentView(R.layout.dialog_ride_details);
    ...
}

EDIT: Here is what i get in the Android Monitor when i press the FAB. I made this Log call:

Log.d("onClick: ", getActivity().toString()+ " "+OfferActivity.class.toString());

and get this

D/onClick:: at.friendlyride.friendlyride.main.MainActivity@bc040c9 class at.friendlyride.friendlyride.main.OfferActivity  
D/OpenGLRenderer: endAllStagingAnimators on 0x7f978af000 (RippleDrawable) with handle 0x7f98e11d40

And here is the MainActivity xml file, where I have the TabLayout:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".MainActivity">

<!--<include-->
    <!--android:id="@+id/tool_bar"-->
    <!--layout="@layout/tool_bar"-->
    <!--/>-->

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/white"/>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"  />

回答1:

Replace:

 @Override 
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);
    setContentView(R.layout.dialog_ride_details);
    ... 
} 

With:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_ride_details);
        ....
}

in your DetailsActivity class

refer http://developer.android.com/reference/android/app/Activity.html


回答2:

You need to return a View in the onCreateView method:

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,    @Nullable Bundle savedInstanceState) {
    ViewGroup v = (ViewGroup)inflater.inflate(R.layout.fragment_news,container,false);
    ...
    return v;}


回答3:

How are you adding your fragment in Activity?

I can see that you are creating the view fragment and setting the activity layout,but I cannot see if your activity xml file has a <fragment name='fragmentClass'/> tag or you are using a frame layout to add your fragment dynamically. To add fragment dynamically, you must use getSupportFragmentManager() and BeginTransaction to add your fragment in the Activity.

Please, add your activity xml file.



回答4:

Replace:

  Intent intent = new Intent(con, OfferActivity.class);
  getActivity().startActivity(intent);

With:

 Intent intent = new Intent(getActivity(), OfferActivity.class);
 startActivity(intent);