1

我无法找到我的问题的答案,所以我提出了一个新的问题。从片段的意图开始活动开始空白活动

我在MainActivity有一个TabLayout(+ ViewPagerAdapter)的Android应用程序,每个标签是一个片段。现在我想从我的片段中用Intent开始一个新的活动。一旦我想在按FAB时打开一个活动,而另一次我想从我的Newsfeed中按下一个FeedItem时打开另一个活动。现在每当我按下某个东西,它就会打开一个完全空白的页面。

我在这两个活动的onCreate()中的setContentView到正确的布局,并且两个活动(+布局)是当我在标签直接显示他们对自己工作的罚款。

我将把这两个活动我想打个电话从我拨打以下,如果你有什么事请让我知道的片段。

,我会很高兴,如果有人可以帮助我,我需要为我的最终学校项目:)

NewsFragment(我的新闻源和FAB是):

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:

public class OfferActivity extends AppCompatActivity { 

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

而且我通过单击ListView项所说的活动:

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

编辑:这是我所得到的在Android监视器时,我按下FAB。我做了这个记录呼叫:

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

,并得到这个

D/onClick:: [email protected] class at.friendlyride.friendlyride.main.OfferActivity 
D/OpenGLRenderer: endAllStagingAnimators on 0x7f978af000 (RippleDrawable) with handle 0x7f98e11d40 

这里是在MainActivity xml文件,在那里我有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

仅用于调试目的,您可以在活动的布局中设置背景颜色,并查看在活动打开时屏幕是否显示您设置的颜色?如果是,那么这个活动似乎是正确的,但是你的布局实现出了问题。如果没有,那么必须调试功能。 – Thilek

+0

书籍活动在哪里? –

+0

谢谢,得到它运行感谢Shijil :) –

回答

1

替换:

@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类

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

非常感谢,这就是它! :D –

+0

另一件事,你知道该怎么做,以便活动在碎片上方打开,这样当我按下后退按钮时,活动关闭,我回到碎片? :) –

0

您需要在onCreateView方法中返回View:

@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;} 
+0

我确实回报它,只是忘了在这里添加它,谢谢:) –

-1

替换:

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

有了:

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

你是如何在活动中加入您的片段?

我可以看到您正在创建视图片段并设置活动布局,但我看不到您的活动xml文件是否有<fragment name='fragmentClass'/>标记,或者您正在使用框架布局来动态添加片段。要动态添加片段,您必须使用getSupportFragmentManager()和BeginTransaction在活动中添加片段。

请添加您的活动xml文件。