如何从片段开始活动?

时间:2015-03-30 19:19:30

标签: java android

我在Frangment3上有3个片段和一个按钮。当我按下那个按钮时,我想开始一个名为Baigimas的活动,但它不起作用。当我在模拟器中按下按钮时,我的程序会关闭。

Fragment3.java

public class Fragment3 extends Fragment{

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment3_layout, container, false);


    View.OnClickListener listnr=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), Baigimas.class);
            startActivity(intent);
        }
    };

    Button btn =(Button) v.findViewById(R.id.button);
    btn.setOnClickListener(listnr);

    return v;
}}

这是我在logcat中看到的内容:

03-31 05:51:45.166    1937-1937/bandymas.viewpagerexample I/art﹕ Not late-enabling -Xcheck:jni (already on)
03-31 05:51:45.518    1937-1962/bandymas.viewpagerexample D/OpenGLRenderer﹕ Render dirty regions requested: true
03-31 05:51:45.521    1937-1937/bandymas.viewpagerexample D/﹕ HostConnection::get() New Host Connection established 0xae0cfdb0, tid 1937
03-31 05:51:45.554    1937-1937/bandymas.viewpagerexample D/Atlas﹕ Validating map...
03-31 05:51:47.776    1937-1950/bandymas.viewpagerexample I/art﹕ Background sticky concurrent mark sweep GC freed 2039(140KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 1891KB/1891KB, paused 131.931ms total 163.748ms
03-31 05:51:47.898    1937-1962/bandymas.viewpagerexample D/﹕ HostConnection::get() New Host Connection established 0xae0cff40, tid 1962
03-31 05:51:47.912    1937-1962/bandymas.viewpagerexample I/OpenGLRenderer﹕ Initialized EGL, version 1.4
03-31 05:51:47.924    1937-1962/bandymas.viewpagerexample D/OpenGLRenderer﹕ Enabling debug mode 0
03-31 05:51:47.945    1937-1962/bandymas.viewpagerexample W/EGL_emulation﹕ eglSurfaceAttrib not implemented
03-31 05:51:47.946    1937-1962/bandymas.viewpagerexample W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xae0e7860, error=EGL_SUCCESS
03-31 05:51:47.988    1937-1950/bandymas.viewpagerexample I/art﹕ Background partial concurrent mark sweep GC freed 326(35KB) AllocSpace objects, 0(0B) LOS objects, 39% free, 1866KB/3MB, paused 1.087ms total 144.656ms
03-31 05:51:48.750    1937-1962/bandymas.viewpagerexample W/EGL_emulation﹕ eglSurfaceAttrib not implemented
03-31 05:51:48.750    1937-1962/bandymas.viewpagerexample W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xae0e7860, error=EGL_SUCCESS
03-31 05:51:57.303    1937-1937/bandymas.viewpagerexample D/AndroidRuntime﹕ Shutting down VM
    --------- beginning of crash
03-31 05:51:57.305    1937-1937/bandymas.viewpagerexample E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: bandymas.viewpagerexample, PID: 1937
    android.content.ActivityNotFoundException: Unable to find explicit activity class {bandymas.viewpagerexample/bandymas.viewpagerexample.Baigimas}; have you declared this activity in your AndroidManifest.xml?
            at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1761)
            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1485)
            at android.app.Activity.startActivityForResult(Activity.java:3736)
            at android.app.Activity.startActivityForResult(Activity.java:3697)
            at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:826)
            at android.support.v4.app.Fragment.startActivity(Fragment.java:896)
            at bandymas.viewpagerexample.Fragment3$1.onClick(Fragment3.java:25)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="bandymas.viewpagerexample" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

3 个答案:

答案 0 :(得分:1)

按如下方式更新您的清单  并尝试,

    <activity
        android:name="<full package name>.Baigimas"
        android:screenOrientation="portrait"  >
    </activity>

答案 1 :(得分:0)

在您的清单中添加您的活动。

<activity
    android:name=".Baigimas"/>

答案 2 :(得分:0)

深入了解logcat:

android.content.ActivityNotFoundException: Unable to find explicit activity class {bandymas.viewpagerexample/bandymas.viewpagerexample.Baigimas}; have you declared this activity in your AndroidManifest.xml?

它显然告诉您:您可能无法部署Baigima.java活动。 通过添加以下行来完成您的AndroidManifest.xml

<activity
    android:name="<your-activity-baigimas-path>.Baigimas"
    ......
>
</activity>