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?
warum diese Linie Image img = (Bildansicht) findViewById (R.id.profile_image); in der onlick existieren? – steevoo