簡體   English   中英

Android SlidingMenu setBehindContentView異常

[英]Android SlidingMenu setBehindContentView Exception

我想將Jeremy Feinstein的“滑動菜單”集成到我的項目中。 我創建了一個片段,希望在滑動菜單中顯示它,但是它似乎根本不起作用。 我目前正在開發一個簡單的音樂應用程序,因為我女友使用的該應用程序不支持隨機播放,因此我無事可做,因此我一直在努力。 當我運行該應用程序時,我得到一個java.lang.RuntimeException。 它發生在庫的setBehindContentView方法中。 以下是我用於SlidingMenu的每個代碼。

主要活動:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_songs);

        setBehindContentView(R.id.frame_menu);
        Fragment fragment;

        if (savedInstanceState == null) {
            FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction();
            fragment = new SlidingMenuFragment();
            transaction.replace(R.id.frame_menu, fragment);
            transaction.commit();
        }
        else
            fragment = (Fragment) this.getSupportFragmentManager().findFragmentById(R.id.frame_menu);

        SlidingMenu menu = getSlidingMenu();
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

SlidingMenuFragment:

package com.shuffleplay.intracode.shuffleplay;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Johnny Manson on 3/22/2015.
 */
public class SlidingMenuFragment extends Fragment {

    List<Playlist> playlistList = new ArrayList<Playlist>();
    DatabaseHandler dbHandler;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.sliding_menu, null);
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        dbHandler = new DatabaseHandler(getActivity());

        final Button addBtn = (Button) getView().findViewById(R.id.btnAddPlaylist);
        EditText txtPlaylistName = (EditText) getView().findViewById(R.id.txtPlaylist);

        txtPlaylistName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                addBtn.setEnabled(!String.valueOf(s).trim().isEmpty());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Playlist newPlaylist = new Playlist(playlistList.size(), String.valueOf(((TextView) getView().findViewById(R.id.txtPlaylist)).getText()));
                dbHandler.createPlaylist(newPlaylist);
            }
        });
    }

    public class PlaylistAdapter extends ArrayAdapter<Playlist> {

        public PlaylistAdapter(Context context) {
            super(context, R.layout.lvplaylists_item, playlistList);
        }

        @Override
        public int getCount() {
            return playlistList.size();
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        public View getView(int pos, View convertView, ViewGroup parent) {

            if (convertView == null)
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.lvplaylists_item, parent, false);

            TextView txtTitle = (TextView) convertView.findViewById(R.id.lviPlaylistName);
            TextView txtArtist = (TextView) convertView.findViewById(R.id.lviSongsInPlaylist);

            Playlist currentPlaylist = playlistList.get(pos);

            txtTitle.setText(currentPlaylist.getName());
            txtArtist.setText(currentPlaylist.getSongs().length + " Songs");

            convertView.setTag(currentPlaylist.getId());
            return convertView;
        }
    }
}

slide_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/sliding_menu">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="50dp">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:id="@+id/txtPlaylist"
        android:layout_marginTop="10dp"
        android:hint="Create new Playlist..."
        android:maxLength="50"
        android:layout_weight="10" />

        <Button
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:layout_weight="50"
            android:text="+"
            android:enabled="false"
            android:id="@+id/btnAddPlaylist" />

    </LinearLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray"
        android:layout_marginTop="10dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp" />

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/lvPlaylists"
        android:layout_marginTop="20dp" />

    </LinearLayout>

</FrameLayout>

menu_frame.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frame_menu"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</FrameLayout>

堆棧跟蹤:

03-22 12:21:05.219    1448-1448/com.shuffleplay.intracode.shuffleplay E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.shuffleplay.intracode.shuffleplay, PID: 1448
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shuffleplay.intracode.shuffleplay/com.shuffleplay.intracode.shuffleplay.SongsActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f06000d type #0x12 is not valid
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            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:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f06000d type #0x12 is not valid
            at android.content.res.Resources.loadXmlResourceParser(Resources.java:2314)
            at android.content.res.Resources.getLayout(Resources.java:939)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:395)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity.setBehindContentView(SlidingFragmentActivity.java:83)
            at com.shuffleplay.intracode.shuffleplay.SongsActivity.onCreate(SongsActivity.java:47)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            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:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

我發現了問題。 我沒有設置R.layout.frame_menu,而是設置了R.id.frame_menu。 那就是問題所在,現在可以正常使用了。

暫無
暫無

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

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