错误-Didn找不到类" android.support.v4.widget

时间:2016-07-01 08:05:17

标签: android

这是我的主要活动文件

`     

// DRAW
for(var i = 0; i < pieces.length; i++){

    // HTML
    var el = "<div class=\"image piece"+(pieces[i])+"\"><p class=\"helpNum\"></p></div>";
    $(".grid").append(el);

    $(".piece"+(h*v)).addClass("voidBlock");
}

// CSS div positioning
var j = 0;
for(var pY = 0; pY > -600; pY -= finalHeight){
    for(var pX = 0; pX > -800; pX -= finalWidth){
        $(".piece"+pieces[j]).css({left: pX, top: pY});
        j++;
    }
}

`

main activity.java

`package piestudio.zine;

          <android.support.v4.widget.
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <FrameLayout android:layout_width="wrap_content"
                     android:layout_height="match_parent">


            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:theme="@style/Theme.AppCompat.Light.DarkActionBar"/>

        </FrameLayout>

        <fragment
            android:id="@+id/fragment.navigation.drawer"
            android:name="piestudio.zine.NavigationDrawerFragment"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:layout="@layout/fragment_navigation_drawer"
            tools:layout="@layout/fragment_navigation_drawer"/>



    </android.support.v4.widget.>



</LinearLayout> 

`

导航抽屉活动

`package piestudio.zine;

import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;

public class MainActivity extends AppCompatActivity{
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.drawer,menu);
        return true;
    }

    public android.support.v7.widget.Toolbar toolbar;


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

        toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);



       NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)
               getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
        drawerFragment.setUP(R.id.fragment_navigation_drawer,(DrawerLayout) findViewById(R.id.drawer_layout),toolbar);}



}

`

错误的logcat

import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * A simple {@link Fragment} subclass. */ public class NavigationDrawerFragment extends Fragment { public static final String PREF_FILE_NAME = "testpref"; public static final String KEY_USER_LEARNED_DRAWER="user_learned_drawer"; private ActionBarDrawerToggle mDrawerToggle; private DrawerLayout mDrawerLayout; private View containerView; private boolean mUserLearnedDrawer; private boolean mFromSavedInstanceState; public NavigationDrawerFragment() { // Required empty public constructor } @Override public void onCreate( Bundle savedInstanceState) { super.onCreate(savedInstanceState); mUserLearnedDrawer=Boolean.valueOf(readFromPreferences(getActivity(),KEY_USER_LEARNED_DRAWER,"false")); if(savedInstanceState!=null){ mFromSavedInstanceState=true; } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_navigation_drawer, container, false); } public void setUP(int fragmentId, DrawerLayout drawerLayout, final Toolbar toolbar) { containerView=getActivity().findViewById(fragmentId); mDrawerLayout = drawerLayout; mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) { @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); if (mUserLearnedDrawer){ mUserLearnedDrawer=true; saveToPreferences(getActivity(),KEY_USER_LEARNED_DRAWER,mUserLearnedDrawer+""); } getActivity().invalidateOptionsMenu(); } @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); getActivity().invalidateOptionsMenu(); } @Override public void onDrawerSlide(View drawerView, float slideOffset) { if (slideOffset<0.8){ toolbar.setAlpha(1-slideOffset);} } }; if (!mUserLearnedDrawer&&!mFromSavedInstanceState){ mDrawerLayout.openDrawer(containerView); } mDrawerLayout.setDrawerListener(mDrawerToggle); mDrawerLayout.post(new Runnable() { @Override public void run() { mDrawerToggle.syncState(); } }); } public static void saveToPreferences(Context context, String preferenceName, String preferenceValue) { SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString(preferenceName, preferenceValue); editor.apply(); } public static String readFromPreferences(Context context, String preferenceName, String defaultValue) { SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE); return sharedPreferences.getString(preferenceName,defaultValue); } }

我的应用程序没有启动,每次我在模拟器中运行时都会强制关闭。请帮帮我。我正在做这一切以获取导航抽屉上的项目(列表),你也可以指出代码中的任何错误:)。虽然logcat显示错误## -Didn找不到类&#34; android.support.v4.widget。 ##我无法解决此错误

2 个答案:

答案 0 :(得分:1)

添加compile&#39; com.android.support:support-v4:23.2.0&#39; build.gradle(Module:app)文件中依赖于block的行。

答案 1 :(得分:0)

正如我在您的代码中看到的那样,您正在实施NavigationDrawer并且您会错过以下内容。

此处显示错误<android.support.v4.widget.

将此<android.support.v4.widget.更改为此<android.support.v4.widget.NavigationDrawer

并将</android.support.v4.widget.>更改为此</android.support.v4.widget.NavigationDrawer>