2017-11-25 14 views
-2

Wenn ich auf die ImageView auf der Navigationsschublade klicke, um zur nächsten Aktivität zu gelangen, stürzte die App ab. HierAndroid Studio: "App hat aufgehört", wenn die Bildansicht beim Klicken in der Navigationsleiste

ist der Code:

Für MainActivity.java:

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

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
//  ActionBar actionBar = getSupportActionBar(); 
//  actionBar.setDisplayShowHomeEnabled(true); 
//  actionBar.setIcon(R.mipmap.ic_launcher); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 

     final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.addDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
     navigationView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       startActivity(new Intent(MainActivity.this, Profile.class)); 
      } 
     }); 

Für nav_header_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/nav_header" 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/nav_header_height" 
    android:background="@drawable/side_nav_bar" 
    android:gravity="bottom" 
    android:orientation="vertical" 
    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:theme="@style/ThemeOverlay.AppCompat.Dark"> 

    <ImageView 
     android:id="@+id/profile_image" 
     android:layout_width="83dp" 
     android:layout_height="75dp" 
     android:paddingTop="@dimen/nav_header_vertical_spacing" 
     android:scaleType="fitCenter" 
     android:clickable="true" 
     android:onClick="onClick" 
     app:srcCompat="@drawable/profile" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/nav_header_vertical_spacing" 
     android:text="Android Studio" 
     android:textAppearance="@style/TextAppearance.AppCompat.Body1" /> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="[email protected]" /> 

</LinearLayout> 

Profile.class:

public class Profile extends Fragment{ 
    View myView; //A view object called myView 

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

Logs:

11-25 17:30:34.959 6126-6126/? I/zygote: Not late-enabling -Xcheck:jni (already on) 
11-25 17:30:35.098 6126-6126/? W/zygote: Unexpected CPU variant for X86 using defaults: x86 
11-25 17:30:35.179 6126-6133/? E/zygote: Failed writing handshake bytes (-1 of 14): Broken pipe 
11-25 17:30:35.179 6126-6133/? I/zygote: Debugger is no longer active 
11-25 17:30:35.791 6126-6126/? I/InstantRun: starting instant run server: is main process 
11-25 17:30:36.123 6126-6151/? D/OpenGLRenderer: HWUI GL Pipeline 

               [ 11-25 17:30:36.160 6126: 6151 D/   ] 
               HostConnection::get() New Host Connection established 0x9fe29800, tid 6151 
11-25 17:30:36.323 6126-6151/com.example.hci I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0 
11-25 17:30:36.323 6126-6151/com.example.hci I/OpenGLRenderer: Initialized EGL, version 1.4 
11-25 17:30:36.323 6126-6151/com.example.hci D/OpenGLRenderer: Swap behavior 1 
11-25 17:30:36.323 6126-6151/com.example.hci W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without... 
11-25 17:30:36.323 6126-6151/com.example.hci D/OpenGLRenderer: Swap behavior 0 
11-25 17:30:36.325 6126-6151/com.example.hci D/EGL_emulation: eglCreateContext: 0xa28040c0: maj 2 min 0 rcv 2 
11-25 17:30:36.327 6126-6151/com.example.hci D/EGL_emulation: eglMakeCurrent: 0xa28040c0: ver 2 0 (tinfo 0xa2803140) 
11-25 17:30:36.340 6126-6131/com.example.hci I/zygote: Do partial code cache collection, code=12KB, data=19KB 
11-25 17:30:36.342 6126-6131/com.example.hci I/zygote: After code cache collection, code=12KB, data=19KB 
11-25 17:30:36.342 6126-6131/com.example.hci I/zygote: Increasing code cache capacity to 128KB 
11-25 17:30:36.345 6126-6131/com.example.hci I/zygote: Do partial code cache collection, code=12KB, data=37KB 
11-25 17:30:36.345 6126-6131/com.example.hci I/zygote: After code cache collection, code=12KB, data=37KB 
11-25 17:30:36.345 6126-6131/com.example.hci I/zygote: Increasing code cache capacity to 256KB 
11-25 17:30:36.346 6126-6131/com.example.hci I/zygote: JIT allocated 71KB for compiled code of void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int) 
11-25 17:30:36.346 6126-6131/com.example.hci I/zygote: Compiler allocated 4MB to compile void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int) 
11-25 17:30:36.388 6126-6151/com.example.hci D/EGL_emulation: eglMakeCurrent: 0xa28040c0: ver 2 0 (tinfo 0xa2803140) 
11-25 17:30:39.559 6126-6126/com.example.hci D/AndroidRuntime: Shutting down VM 
11-25 17:30:39.561 6126-6126/com.example.hci E/AndroidRuntime: FATAL EXCEPTION: main 
                     Process: com.example.hci, PID: 6126 
                     java.lang.IllegalStateException: Could not find method onClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatImageView with id 'profile_image' 
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327) 
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) 
                      at android.view.View.performClick(View.java:6294) 
                      at android.view.View$PerformClick.run(View.java:24770) 
                      at android.os.Handler.handleCallback(Handler.java:790) 
                      at android.os.Handler.dispatchMessage(Handler.java:99) 
                      at android.os.Looper.loop(Looper.java:164) 
                      at android.app.ActivityThread.main(ActivityThread.java:6494) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

Wie kann ich dieses Problem lösen?

+0

warum diese Linie Image img = (Bildansicht) findViewById (R.id.profile_image); in der onlick existieren? – steevoo

Antwort

1

Die ImageView in der Zeile:

ImageView img = (ImageView) findViewById(R.id.profile_image); 

ist ungenutzt entfernt es den Fehler in der Navigation lösen View, auch seine null erinnern.

Entfernen Sie auch das Attribut android:onClick="onClick" in Ihrem XML für die ImageView, weil Sie in Ihrer Aktivität keine Methode namens onClick definiert haben.

Aber mit meinen Instinkten wollen Sie eine andere Aktivität gehen (Profile.class), wenn der Header diese Verwendung geklickt wird:

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
View header_view = navigationView.getHeaderView(0); 
ImageView img = (ImageView) header_view.findViewById(R.id.profile_image); 
img.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      startActivity(new Intent(MainActivity.this, Profile.class)); 
     } 
    }); 

Aber wenn Sie nur auf eine andere Tätigkeit gehen, wenn die (ImageView) geklickt wird tun this:

+0

Die App stürzt immer noch, nachdem ich versucht, den Code verwenden Sie – user7146946

+0

Wo Debuggen Sie Ihre App. Wenn Sie Android Studio verwenden, wenn die App abstürzt, gibt es einige Protokolle rot. aber einige sind in blau! Wenn Sie klicken, können Sie sehen, welche Zeile den Absturz bringt. Zeig uns, welche Linie das macht! @ user7146946 – Xenolion

+0

Und übrigens habe ich Ihnen drei alternative Codes gegeben. Welche verursacht Absturz? – Xenolion

0

Das Problem ist hier in diesem Code entfernen Sie einfach das Bild Blick von der Onclick

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 
    navigationView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
//remove below image view line 
      ImageView img = (ImageView) findViewById(R.id.profile_image); 
      startActivity(new Intent(MainActivity.this, Profile.class)); 
     } 
    }); 

und der Grund dafür ist, dass die Bildansicht Sie bekommen innerhalb der Navigationsansicht in Ihrem Haupt-xml.

+0

Ich habe versucht, es zu entfernen, aber die App stürzt immer noch – user7146946

0

Sie werfen imageView falsch. Fügen Sie diese Zeilen innerhalb der onCreate() Ihrer Navigationsschubladenaktivität hinzu.

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
View view = navigationView.getHeaderView(0); 
ImageView yourProfileImageView=(ImageView)view.findViewById(R.id.userProfilePicture); 

und dann

yourProfileImageView.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      Intent intent = new Intent(v.getContext(),ProfileActivity.class); 

      startActivity(intent); 
     } 
    }); 

jetzt nur auf Image in Ihrem Navigations Header, klicken Sie neue Tätigkeit beginnen können.

0

Sie versuchen, Fragment von Aktivität zu starten.Die App wird automatisch abstürzen. Sie sollten also den folgenden Code in der onClick-Methode der Bildansicht verwenden.

tun dies dann in Ihrer Tätigkeit Fragment hinzuzufügen:

FragmentManager manager = getFragmentManager(); 
FragmentTransaction transaction = manager.beginTransaction(); 
transaction.add(R.id.container,new Profile(),YOUR_FRAGMENT_STRING_TAG); 
transaction.addToBackStack(null); 
transaction.commit(); 

Und um das zu ersetzen Fragment tun:

FragmentManager manager = getFragmentManager(); 
FragmentTransaction transaction = manager.beginTransaction(); 
transaction.replace(R.id.container,new Profile(),YOUR_FRAGMENT_STRING_TAG); 
transaction.addToBackStack(null); 
transaction.commit(); 

Hier Behälter die ID des frameLayout ist

Verwandte Themen