添加ImageView后,应用程序在启动时崩溃

时间:2018-04-18 17:06:03

标签: java android crash imageview

我正在运行一个带有一个ImageView的基本应用程序。它一直运行,直到我添加以下

ImageView gohan = findViewById(R.id.gohanView);

这些是我唯一做过的事情:

  • 创建项目
  • 创建空活动
  • 将ImageView添加到活动
  • 导入图像到可绘制文件夹
  • 将图像附加到ImageView的src
  • RUN(此时一切正常)
  • 然后我将上面的代码添加到我的MainActivity类中,应用程序将不再运行。

爪哇

package com.example.david.dragonballz;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    ImageView gohan = findViewById(R.id.gohanView);

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

XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/gohanView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="69dp"
        android:layout_marginEnd="114dp"
        android:layout_marginLeft="114dp"
        android:layout_marginRight="114dp"
        android:layout_marginStart="114dp"
        android:layout_marginTop="69dp"
        android:src="@drawable/gohan"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

logcat的

04-18 13:02:51.416 15392-15392/com.example.david.dragonballz E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.david.dragonballz, PID: 15392
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.david.dragonballz/com.example.david.dragonballz.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2839)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6938)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
        at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:117)
        at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:149)
        at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:56)
        at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:31)
        at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:31)
        at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:198)
        at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
        at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
        at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
        at com.example.david.dragonballz.MainActivity.<init>(MainActivity.java:9)
        at java.lang.Class.newInstance(Native Method)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1180)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2829)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6938) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 

以下是我正在考虑的Android课程中的WORKING代码示例。为什么这对他有用并且曾经为我工作过? EXAMPLE

2 个答案:

答案 0 :(得分:1)

永远不会在onCreate()方法之前找到任何视图的ID,因为xml布局已加载到onCreate()上。

setContentView(R.layout.activity_main);

onCreate()

中找到ID
gohan = (ImageView) findViewById(R.id.gohanView);

答案 1 :(得分:0)

声明

public ImageView gohan;

在oncreate()

中添加此行
gohan = (ImageView)findViewById(R.id.gohanView);

这一行之后

 setContentView(R.layout.activity_main);

最后你的onCreate()

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
    gohan = (ImageView)findViewById(R.id.gohanView);

 gohan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // do something 
           gohan.animate().alpha(0).setDuration(2000);
            }
        });
    }

现在还可以访问这个IMAGEVIEW