简体   繁体   English

Android Studio上带有片段的导航抽屉

[英]Navigation drawer with fragment on Android Studio

I am trying to implements this tutorial https://github.com/codepath/android_guides/wiki/Fragment-Navigation-Drawer After using a splashscreen, I've did everything, but my problem is the fragment after the splashscreen, I don't know if I did this correctly: 我正在尝试实现本教程https://github.com/codepath/android_guides/wiki/Fragment-Navigation-Drawer使用启动画面后,我已经完成了所有操作,但是我的问题是启动画面后的片段,我没有不知道我是否正确地做到了:

The folders here: http://i.stack.imgur.com/7qdTg.png 此处的文件夹: http : //i.stack.imgur.com/7qdTg.png

Home.java (fragment (first link of navigation)): Home.java(片段(导航的第一个链接)):

public class Home extends android.support.v4.app.Fragment {
public View onCreateView(LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
    /**
     * Inflate the layout for this fragment
     */
    return inflater.inflate(
            R.layout.activity_main, container, false);
}

} }

MainActivity (splashscreen) MainActivity(闪屏)

public class MainActivity extends Activity {

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

    Thread logoTimer = new Thread() {
        public void run() {
            try {
                int logoTimer = 0;
                while (logoTimer < 5) {
                    sleep(1000);
                    logoTimer = logoTimer + 1;
                }
                ;
                startActivity(new Intent("android.intent.action.Home"));
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                finish();
            }
        }
    };
    logoTimer.start();
}

} }

I added the fragment Home in the Manifest: 我在清单中添加了片段Home:

<activity
android:name=".Home"
android:label="@string/app_name" >
<intent-filter>
    <action android:name="android.intent.action.Home" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

But i get a error in the manifest, i think, its not the nice way to use and call the fragment after the splashscreen, how to do that ? 但是我认为清单中出现错误,我不是在启动画面后使用和调用片段的好方法,该怎么做? Thanks 谢谢

Fragment is not an Activity. 片段不是活动。 You don't need to add it in your Manifest file. 您无需将其添加到清单文件中。

You cannot use 你不能使用
startActivity(new Intent("android.intent.action.Home"));

because its a fragment not an activity.... use 因为它不是活动片段...使用

FragmentManager fragmentManager = getFragmentManager();
      FragmentTransaction fragmentTransaction = 
      fragmentManager.beginTransaction();
      Home frag = new Home();
fragmentTransaction.replace(android.R.id.content, frag );

try this..this will work 试试这个..这将工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM