Android在多个片段之间导航

时间:2013-12-03 13:30:23

标签: android android-fragments navigation

我是Android应用程序构建的新手,我这样做是为了我的大学项目的要求。请帮助我

我的应用已使用片段滑动标签。我有5个标签和5个片段,在一些标签(片段)中,我需要导航到片段中的新片段;例如当在Tab1中我按下按钮会将用户带到新片段时,我搜索了互联网并设法编写了代码。我的问题是在Tab1和按下按钮转到新片段我正在“应用程序停止工作”请帮助我

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View.OnClickListener;

 public class Unsafe extends Fragment implements OnClickListener {
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.unsafe, container, false);
    Button button1 = (Button) v.findViewById(R.id.u1);
    button1.setOnClickListener(this);
    return v;
}

public void onClick(View v) {
    startActivity(new Intent(getActivity(),unsafe1.class));
}
}

更新 - 发布Logcat

12-03 08:59:39.538    1460-1460/com.example.hfacs D/AndroidRuntime﹕ Shutting down VM
12-03 08:59:39.538    1460-1460/com.example.hfacs W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a77b90)
12-03 08:59:39.538      379-645/system_process I/ActivityManager﹕ START u0 {cmp=com.example.hfacs/.Unsafe1} from pid 1460
12-03 08:59:39.588    1460-1460/com.example.hfacs E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.hfacs, PID: 1460
    android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.hfacs/com.example.hfacs.Unsafe1}; have you declared this activity in your AndroidManifest.xml?
            at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
            at android.app.Activity.startActivityForResult(Activity.java:3423)
            at android.app.Activity.startActivityForResult(Activity.java:3384)
            at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:848)
            at android.support.v4.app.Fragment.startActivity(Fragment.java:878)
            at com.example.hfacs.Unsafe$1.onClick(Unsafe.java:28)
            at android.view.View.performClick(View.java:4424)
            at android.view.View$PerformClick.run(View.java:18383)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4998)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
            at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

我相信你不应该在onClick上调用startActivity()。 相反,当用户单击按钮时,通知包含此片段的FragmentActivity。在FragmentActivity中,调用ViewPager.setCurrentItem(position),其中position是int。然后,视图寻呼机将滚动到所需的选项卡。

我不确定这是否是最好的方法。但我以前试过这个并且它有效。 在FragmentActivity中添加一个函数,用于滚动到所需的选项卡。

public static void scroll(int pos){
    mViewPager.setCurrentItem(pos); //mViewPager is the ViewPager you have in FragmentActivity
}

这是onClickListener

Button btn = (Button) v.findViewById(R.id.u1);
    btn.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            //when button clicked, call the scroll function
            MainActivity.scroll(position_of_unsafe1);   //MainActivity is the FragmentActivity name         
        }           
    });

答案 1 :(得分:0)

我在logcat中发现了这一行:

ndroid.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.hfacs/com.example.hfacs.Unsafe1}; have you declared this activity in your AndroidManifest.xml?

这意味着您未在Unsafe1文件中声明活动AndroidManifest.xml

相关问题