0

나는 안드로이드 응용 프로그램에서 탐색 drawerLayout를 사용하는 것을 시도하고있다 캐스트 할 수없는하지만 내 응용 프로그램은

drawerLayout.closeDrawer(listviewsliding); 

내가 할 수에서 오류로 인해 충돌한다 어떻게 해결할 수 있을지 모르겠고 제안이 필요합니다.

오류는 다음과 같습니다

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nabia.myapplication/com.example.nabia.myapplication.Activities.FindNearbyMosque}: java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 
        at android.app.ActivityThread.access$900(ActivityThread.java:177) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:145) 
        at android.app.ActivityThread.main(ActivityThread.java:5942) 
        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:1389) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184) 
     Caused by: java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams 
        at android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:1425) 
        at android.support.v4.widget.DrawerLayout.closeDrawer(DrawerLayout.java:1676) 
        at android.support.v4.widget.DrawerLayout.closeDrawer(DrawerLayout.java:1666) 
        at com.example.nabia.myapplication.Activities.FindNearbyMosque.onCreate(FindNearbyMosque.java:105) 
        at android.app.Activity.performCreate(Activity.java:6289) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)  
        at android.app.ActivityThread.access$900(ActivityThread.java:177)  
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)  
        at android.os.Handler.dispatchMessage(Handler.java:102)  
        at android.os.Looper.loop(Looper.java:145)  
        at android.app.ActivityThread.main(ActivityThread.java:5942)  
        at java.lang.reflect.Method.invoke(Native Method)  
        at java.lang.reflect.Method.invoke(Method.java:372) 

내 자바 코드는 다음과 같습니다

public class FindNearbyMosque extends AppCompatActivity{ 
    private List<ItemSlideMenu> itemSlideMenus; 
    private SlidingMenuAdapter slidingMenuAdapter; 
    private ListView listviewsliding; 
    private DrawerLayout drawerLayout; 
    private ActionBarDrawerToggle actionBarDrawerToggle; 


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

     //Init component 
     listviewsliding = (ListView) findViewById(R.id.listView); 
     drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     itemSlideMenus = new ArrayList<>(); 
     //Add itmes for sliding list 
     itemSlideMenus.add(new ItemSlideMenu(R.drawable.time, "Masjid Timing")); 
     itemSlideMenus.add(new ItemSlideMenu(R.drawable.mosque, "My Mosque")); 
     itemSlideMenus.add(new ItemSlideMenu(R.drawable.settings, "Setting")); 
     slidingMenuAdapter = new SlidingMenuAdapter(this, itemSlideMenus); 
     listviewsliding.setAdapter(slidingMenuAdapter); 

     //Display icon to open/ close sliding list 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     //Set title 
     setTitle(itemSlideMenus.get(0).getTitle()); 
     //Item selected 
     listviewsliding.setItemChecked(0, true); 
     //Close menu 
     drawerLayout.closeDrawer(listviewsliding); 

     //Display fragment 1 when start 
     replaceFragment(0); 

     //Handle on item click 
     listviewsliding.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       //Set title 
       setTitle(itemSlideMenus.get(position).getTitle()); 
       //Item selected 
       listviewsliding.setItemChecked(position, true); 
       //Replce Fragment 
       replaceFragment(position); 
       //close menu 
       drawerLayout.closeDrawer(listviewsliding); 
      } 
     }); 

     actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_opened, R.string.drawer_closed){ 
      @Override 
      public void onDrawerOpened(View drawerView) { 
       super.onDrawerOpened(drawerView); 
       invalidateOptionsMenu(); 
      } 

      @Override 
      public void onDrawerClosed(View drawerView) { 
       super.onDrawerClosed(drawerView); 
       invalidateOptionsMenu(); 
      } 
     }; 

     drawerLayout.setDrawerListener(actionBarDrawerToggle); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.main_menu, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     if (actionBarDrawerToggle.onOptionsItemSelected(item)) 
     { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     actionBarDrawerToggle.syncState(); 
    } 

    //Create method replace fragment 

    private void replaceFragment (int pos){ 
     Fragment fragment = null; 
     switch (pos){ 
      case 0: 
       fragment = new MasjidTiming(); 
       break; 
      case 1: 
       fragment = new MyMosque(); 
       break; 

      case 2: 
       fragment = new Settings(); 
       break; 
      default: 
       fragment = new MasjidTiming(); 
       break; 
     } 
     if (null!=fragment) 
     { 
      FragmentManager fragmentManager = getFragmentManager(); 
      android.app.FragmentTransaction transaction = fragmentManager.beginTransaction(); 
      transaction.replace(R.id.main_consent, fragment); 
      transaction.addToBackStack(null); 
      transaction.commit(); 
     } 
    } 
} 

XML 파일은 다음과 같습니다

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/drawer_layout" 
    android:orientation="horizontal" 
    > 

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:id="@+id/main_consent" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
     <ListView 
     android:layout_width="200dp" 
     android:layout_height="match_parent" 
     android:id="@+id/listView" 
     android:choiceMode="singleChoice" 
     android:layout_gravity="start" 
     android:background="#FFFFFF" 
     /> 

</RelativeLayout> 

</android.support.v4.widget.DrawerLayout> 

Manifest.xml :

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

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

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".Activities.FindNearbyMosque" /> 
     <activity android:name=".Activities.LogIn" /> 
     <activity android:name=".Activities.SignUP" /> 
     <activity android:name=".Activities.LocateMe"></activity> 
    </application> 

</manifest> 

답변

0

RelativeLayout의 닫는 태그를 XML 파일로 대체하여 문제를 해결했습니다. 대신

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/drawer_layout" 
    android:orientation="horizontal" 
    > 

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:id="@+id/main_consent" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</RelativeLayout> 
     <ListView 
     android:layout_width="200dp" 
     android:layout_height="match_parent" 
     android:id="@+id/listView" 
     android:choiceMode="singleChoice" 
     android:layout_gravity="start" 
     android:background="#FFFFFF" 
     /> 
</android.support.v4.widget.DrawerLayout> 

는 :

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/drawer_layout" 
    android:orientation="horizontal" 
    > 

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:id="@+id/main_consent" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
     <ListView 
     android:layout_width="200dp" 
     android:layout_height="match_parent" 
     android:id="@+id/listView" 
     android:choiceMode="singleChoice" 
     android:layout_gravity="start" 
     android:background="#FFFFFF" 
     /> 

</RelativeLayout> 

</android.support.v4.widget.DrawerLayout> 
0

나는이 문제를 해결했다.

drawerLayout.openDrawer(mDrawerLinearLayout); 
drawerLayout.closeDrawer(mDrawerLinearLayout); 
0

추가 할 수 있습니다

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/main_left_drawer" 
    android:orientation="vertical" 
    android:id="@+id/main_consent" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</LinearLayout> 
     <ListView 
     android:layout_width="200dp" 
     android:layout_height="match_parent" 
     android:id="@+id/listView" 
     android:choiceMode="singleChoice" 
     android:layout_gravity="start" 
     android:background="#FFFFFF" 
     /> 
</LinearLayout> 

다음 각각의 코드 라인을 다음이에서 OnCreate 라인()

public LinearLayout mDrawerLinearLayout; 
mDrawerLinearLayout = (LinearLayout) findViewById(R.id.main_left_drawer); 

및 개폐 서랍을 입력 당신은 사용의 LinearLayout 마스트

개인용 RelativeLayout DrawerRelative;

DrawerRelative = (RelativeLayout) findViewById (R.id.main_consent);

다음

drawerLayout.closeDrawer (listviewsliding)를 변경;

drawerLayout.closeDrawer (DrawerRelative)

하는 단계;

+0

이 답변에는 형식이 매우 부족합니다. [좋은 대답 작성 방법] (https://stackoverflow.com/help/how-to-answer)을보십시오. 또한 여기에 이미 OP가 있기 때문에 해결책이 있으므로 문제는 말도 안되는 것처럼 보입니다 (미안하지만 사실입니다) 대답을 삭제 해보십시오. –

관련 문제