簡體   English   中英

單擊ActionBar菜單返回null時顯示片段

[英]Show fragment when click ActionBar menu return null

單擊動作欄菜單項時,我想顯示/隱藏片段。 但是,當我單擊時,我的應用程序給我錯誤。 片段首次運行。 當我想隱藏片段應用程序時,請給出null錯誤。

我怎么解決這個問題 ?

現在謝謝。

logcat的

01-26 03:48:40.942    2295-2295/test.sy.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at android.app.BackStackRecord.run(BackStackRecord.java:661)
        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
        at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

LayerList.Java

package test.sy.myapplication;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class LayerList extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View mView = inflater.inflate(R.layout.layerlist,container,false);
    return mView;

}
}

MainActivity.Java

package test.sy.myapplication;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

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


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}




@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    switch (item.getItemId()) {
        case R.id.action_settings:
            return true;
        case R.id.showsth:

            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.setCustomAnimations(android.R.animator.fade_in,
                    android.R.animator.fade_out);
            ft.show(fm.findFragmentById(R.id.fragment));
            ft.commit();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
}

ft.show(fm.findFragmentById(R.id.fragment));

這條線導致問題

fm.findFragmentById(R.id.fragment)返回null。

在此行中,您嘗試在layout/activity_main找到一個片段,該片段先前已添加到R.id.fragment 如果您之前沒有添加或替換片段,則很明顯返回空AMK

我解決了這個問題。

這種情況是由於添加片段而沒有任何幀布局引起的。

一旦將framelayout添加到主活動中。 並給它id;

<RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<FrameLayout
    android:layout_width="200dp"
    android:layout_height="450dp"

    android:layout_centerVertical="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="false"
    android:id="@+id/frameLayout"></FrameLayout>

第二;

創建具有其活動性的片段。 在此示例中,我們的片段名稱可能是FragmentTest.java,我們的Fragments活動可能是fragment_activity.xml。

並在主要活動中添加片段; 在Oncreate方法中,您應該添加此行;

FragmentTest Mfragmenttest = new FragmentTest();
FragmentManager fm = getSupportFragmentManager();
                    FragmentTransaction ft = fm.beginTransaction();
//R.id.frameLayout is our frame layouts id. 
 ft.add(R.id.frameLayout, Mfragmenttest , "Hello Fragment");
ft.commit

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM