【发布时间】:2016-02-02 13:34:42
【问题描述】:
我找不到问题的答案,所以我提出了一个新问题。
我的 MainActivity 中有一个带有 TabLayout (+ ViewPagerAdapter) 的 Android 应用,每个 Tab 都是一个片段。现在我想从我的 Fragment 开始一个带有 Intent 的新 Activity。有一次我想在按下 FAB 时打开一个 Activity,还有一次我想在按下我的 Newsfeed 中的 FeedItem 时打开另一个 Activity。现在每次我按下某些东西时,它都会打开一个完全空白的页面。
我在两个活动的 onCreate() 中将 setContentView 设置到正确的布局上,当我直接在选项卡中显示它们时,两个活动(+布局)都可以正常工作。
我会将我想调用的两个活动和我调用的片段放在下面,如果您需要其他任何内容,请告诉我。
如果有人可以帮助我,我会非常高兴,我的期末学校项目需要这个:)
NewsFragment(我的 Newsfeed 和 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 项目调用的 Activity:
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.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:
<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" />
【问题讨论】:
-
仅出于调试目的,您可以在活动的布局中设置背景颜色,并查看屏幕是否显示您在活动打开时设置的颜色?如果是,那么活动似乎是正确的,但您的布局实现有问题。如果没有,则必须调试功能。
-
图书活动在哪里??
-
谢谢,感谢 Shijil 让它运行起来 :)
标签: android listview android-fragments android-intent android-activity