【问题标题】:Starting Activity by intent from Fragment starts blank Activity从 Fragment 通过 Intent 启动 Activity 启动空白 Activity
【发布时间】: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


【解决方案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

【讨论】:

  • 非常感谢,就是这样! :D
  • 另一件事,你知道该怎么做才能让 Activity 在 Fragment 的“上方”打开,这样当我按下后退按钮时,Activity 就会关闭并返回 Fragment? :)
【解决方案2】:

需要在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;}

【讨论】:

  • 我确实退货了,只是忘了在这里添加,谢谢:)
【解决方案3】:

您是如何在 Activity 中添加片段的?

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

请添加您的活动 xml 文件。

【讨论】:

    【解决方案4】:

    替换:

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

    与:

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

    【讨论】:

      猜你喜欢
      • 2017-07-20
      • 1970-01-01
      • 2016-12-19
      • 2015-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-03
      相关资源
      最近更新 更多