2017-11-14 4 views
0

3 개의 항목 및 3 개의 단추가 있습니다. 버튼의 경우 액션 함수 호출을 만들었으며 그 의도는 잘 작동합니다. 내 탐색 항목에 동일한 작업을 추가하고 싶습니다. 어떻게해야합니까?단추 및 탐색 항목에 대해 동일한 작업이 수행됩니다.

매우 명확해야합니다. 버튼과 탐색 항목을 클릭 할 때 동일한 액티비티를 표시해야합니다. 나는 안드로이드가 처음이다. 그래서 나는 이것이 가능하거나 불가능하다는 것을 모른다. 아니면이 작업을 수행하는 적절한 방법을 사용하고 있습니다.

<?xml version="1.0" encoding="utf-8"?> 
    <menu xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:id="@id/explore" 
     android:icon="@drawable/ic_menu_exp" 
     android:title="@string/Explore" /> 
    <item 
     android:id="@id/menuPack" 
     android:icon="@drawable/ic_menu_pack" 
     android:title="@string/packages" /> 
    <item android:id="@id/dealsMenu" 
     android:icon="@drawable/ic_menu_deals" 
     android:title="@string/deals" /> 
    </menu> 

이 세 개의 버튼 내가 지금

<Button 
       android:id="@+id/explore" 
       android:layout_width="100dp" 
       android:layout_height="100dp" 
       android:layout_marginStart="16dp" 
       android:layout_marginLeft="16dp" 
       android:layout_marginTop="40dp" 
       android:background="@null" 
       android:drawableTop="@drawable/explorebutton" 
       android:onClick="exploreActivity" 
       android:text="@string/Explore" 
       android:textAllCaps="false" 
       android:textColor="#838383" 
       app:layout_constraintLeft_toLeftOf="parent" 
       app:layout_constraintTop_toBottomOf="@+id/homelogo" /> 

      <Button 
       android:id="@+id/packages" 
       android:layout_width="100dp" 
       android:layout_height="100dp" 
       android:layout_marginEnd="24dp" 
       android:layout_marginStart="24dp" 
       android:layout_marginTop="40dp" 
       android:background="@null" 
       android:drawableTop="@drawable/packagebutton" 
       android:onClick="packagesActivity" 
       android:text="@string/packages" 
       android:textAllCaps="false" 
       android:textColor="#838383" 
       app:layout_constraintEnd_toStartOf="@+id/deals" 
       app:layout_constraintStart_toEndOf="@+id/explore" 
       app:layout_constraintTop_toBottomOf="@+id/homelogo" /> 

      <Button 
       android:id="@+id/deals" 
       android:layout_width="100dp" 
       android:layout_height="100dp" 
       android:layout_marginEnd="16dp" 
       android:layout_marginRight="16dp" 
       android:layout_marginTop="40dp" 
       android:background="@null" 
       android:drawableTop="@drawable/dealsbutton" 
       android:onClick="dealsActivity" 
       android:text="@string/deals" 
       android:textAllCaps="false" 
       android:textColor="#838383" 
       app:layout_constraintEnd_toEndOf="parent" 
       app:layout_constraintTop_toBottomOf="@+id/homelogo" /> 
하는 데 문제가 있습니다이 내 메뉴 항목입니다

public void exploreActivity(View view){ 
     Intent intent = new Intent(this,ExploreActivity.class); 
     startActivity(intent); 
    } 

    public void packagesActivity(View view){ 
     Intent intent = new Intent(this,PackagesActivity.class); 
     startActivity(intent); 
    } 

    public void dealsActivity(View view){ 
     Intent intent = new Intent(this,DealsActivity.class); 
     startActivity(intent); 
    } 

:

은 내가 버튼을 위해 사용하고 의도입니다

내 HomeActivity에서 변경했습니다. Java

그러나 16,나는

E/AndroidRuntime: FATAL EXCEPTION: main 
        Process: com.example.windows81.travelspoc, PID: 3485 
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.windows81.travelspoc/com.example.windows81.travelspoc.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.NavigationView.setNavigationItemSelectedListener(android.support.design.widget.NavigationView$OnNavigationItemSelectedListener)' on a null object reference 
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2625) 
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2691) 
         at android.app.ActivityThread.access$1000(ActivityThread.java:188) 
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1526) 
         at android.os.Handler.dispatchMessage(Handler.java:111) 
         at android.os.Looper.loop(Looper.java:207) 
         at android.app.ActivityThread.main(ActivityThread.java:5790) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1015) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876) 
        Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.NavigationView.setNavigationItemSelectedListener(android.support.design.widget.NavigationView$OnNavigationItemSelectedListener)' on a null object reference 
         at com.example.windows81.travelspoc.HomeActivity.onCreate(HomeActivity.java:40) 
         at android.app.Activity.performCreate(Activity.java:6275) 
         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2578) 
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2691)  
         at android.app.ActivityThread.access$1000(ActivityThread.java:188)  
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1526)  
         at android.os.Handler.dispatchMessage(Handler.java:111)  
         at android.os.Looper.loop(Looper.java:207)  
         at android.app.ActivityThread.main(ActivityThread.java:5790)  
         at java.lang.reflect.Method.invoke(Native Method)  
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1015)  
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876)  
+0

무엇에 따라 상황이 내가 추가 할 수 있습니다 [읽어 보시기 바랍니다. "빠른 답변"을 얻기 위해 "긴급한"또는 다른 유사한 문구를 내 질문에 사용합니까?] (// meta.stackoverflow.com/q/326569) - 요약하면 이것은 자원 봉사자를 대처하는 이상적인 방법이 아니며, 대답을 얻는 데 비생산적인 것입니다. 이 질문을 귀하의 질문에 추가하지 마십시오. – halfer

답변

0

Button like this

public void exploreActivity(View view){ 
     myMethod() 
    } 
에 메서드를 호출 이상이

public void myMethod(){ 
     Intent intent = new Intent(this,ExploreActivity.class); 
     startActivity(intent); 
    } 

같은 방법을 작성하려고 다음과 같은 오류가 발생했습니다

지금 당신의 활동에 NavigationView.OnNavigationItemSelectedListener 리스너를 구현 : 탐색 메뉴의 경우

 NavigationView mNavigationView = (NavigationView) findViewById(R.id.account_navigation_view); 
    `mNavigationView.setNavigationItemSelectedListener(this);` 

재정의 방법

public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 

    if (id == R.id.explore) { 
      Log.d("Explore","Clicked"); 
      myMethod() 
     } 
} 
+0

이것들은 내가 한 일입니다 HomeActivity는 AppCompatActivity를 확장하여 public class NavigationView를 구현합니다.OnNavigationItemSelectedListener { NavigationView mNavigationView; 및 생성시 mNavigationView.setNavigationItemSelectedListener (this); public void myMethod() { 인 텐트 intent = 새 인 텐트 (this, ExploreActivity.class); startActivity (intent); } 공개 무효 exploreActivity (보기보기) { myMethod(); } –

+0

@ShyjuKrishnankutty 님이 당신을 얻지 못했습니다. – Prem

+0

public boolean onNavigationItemSelected (MenuItem item) { // 핸들 탐색 항목보기를 클릭하십시오. int id = item.getItemId(); if (id == R.id.explore) { Log.d ("Explore", "Clicked"); myMethod(); } false를 반환합니다. 당신은 내가 질문 업데이트 ANS를 확인 @ShyjuKrishnankutty –

0

을 설정

public class YourActivity extends AppCompatActivity implements 
       NavigationView.OnNavigationItemSelectedListene 

보다 ->이 NavigationView

를 사용하여 만들기

여기에 대한 적절한 링크 예 ->http://v4all123.blogspot.in/2016/02/navigationview-example-in-android.html

메뉴 옵션에서 Nav 메뉴를 만들 수 있습니다.

또 다른 방법은 Recyclerview을 사용 -> Recyclerview을하고 어댑터를 통해 모든 텐트를 처리 (동적 목록 또는 긴 목록이있는 경우 다음 내가 두번째 방법을 제안합니다.)

관련 문제