从Fragment意图启动活动将启动空白活动 [英] Starting Activity by intent from Fragment starts blank Activity
问题描述
我找不到我的问题的答案,所以我提出了一个新问题.
I could not find an answer to my problem, so I make a new Question.
我的MainActivity中有一个带有TabLayout(+ ViewPagerAdapter)的Android应用,每个选项卡都是一个Fragment.现在,我想从我的片段开始一个带有意图的新活动.一次,我想在按FAB时打开一个活动,而另一次想在按新闻源中的FeedItem时打开另一个活动.现在,每当我按一些东西时,它就会打开一个完全空白的页面.
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.
我在两个Activity的onCreate()上的setContentView上都放在了正确的Layout上,当我直接在Tab中显示它们时,两个Activity(+ layout)都可以正常工作.
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(我的新闻提要和FAB所在的位置):
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);
}
}
这是FAB所说的活动:
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);
...
}
然后我通过单击ListView项来调用活动":
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);
...
}
这是当我按下FAB时进入Android监视器的内容.我发出了此Log调用:
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());
得到这个
D/onClick:: at.friendlyride.friendlyride.main.MainActivity@bc040c9 class at.friendlyride.friendlyride.main.OfferActivity
D/OpenGLRenderer: endAllStagingAnimators on 0x7f978af000 (RippleDrawable) with handle 0x7f98e11d40
这是MainActivity xml文件,其中有TabLayout:
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" />
推荐答案
替换:
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.dialog_ride_details);
...
}
使用:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_ride_details);
....
}
在您的DetailsActivity类中
in your DetailsActivity class
refer http://developer.android.com/reference/android/app/Activity.html
这篇关于从Fragment意图启动活动将启动空白活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!