Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的教程应用程序崩溃?_Java_Android - Fatal编程技术网

Java 为什么我的教程应用程序崩溃?

Java 为什么我的教程应用程序崩溃?,java,android,Java,Android,我是Android应用程序的初学者。尝试做一个教程,添加一个相对布局、一个网格布局和一些ImageView。但我的应用程序崩溃了 MainActivity.java package se.jakobia.connect3; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; pu

我是Android应用程序的初学者。尝试做一个教程,添加一个相对布局、一个网格布局和一些ImageView。但我的应用程序崩溃了

MainActivity.java

package se.jakobia.connect3;

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

public class MainActivity extends AppCompatActivity {


        public void dropIn(View view) {

    ImageView counter =(ImageView)view;

    counter.setTranslationY(-1000f);

    counter.setImageResource(R.drawable.yellow);

    counter.animate().translationYBy(1000f).setDuration(300);

}

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

<RelativeLayout 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="se.jakobia.connect3.MainActivity">

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="360dp"
        android:layout_centerVertical="true"


        android:background="@drawable/board"
        android:columnCount="3"
        android:rowCount="3">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_column="0"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:layout_row="0"
            android:src="@drawable/red" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_column="1"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:layout_row="0"
            android:src="@drawable/red" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_column="2"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:layout_row="0"
            android:src="@drawable/red" />

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_column="0"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="30dp"
            android:layout_row="1"
            android:src="@drawable/red" />

        <ImageView
            android:id="@+id/imageView5"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_column="1"
            android:layout_marginLeft="40dp"
            android:layout_marginTop="30dp"
            android:layout_row="1"
            android:onClick="dropIn" />

        <ImageView
            android:id="@+id/imageView6"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_column="2"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="30dp"
            android:layout_row="1"
            android:src="@drawable/red" />

        <ImageView
            android:id="@+id/imageView7"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_column="0"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="30dp"
            android:layout_row="2"
            android:src="@drawable/red" />

        <ImageView
            android:id="@+id/imageView8"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_column="1"
            android:layout_marginLeft="40dp"
            android:layout_marginTop="30dp"
            android:layout_row="2"
            android:src="@drawable/red" />

        <ImageView
            android:id="@+id/imageView9"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_column="2"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="30dp"
            android:layout_row="2"
            android:src="@drawable/red" />


    </GridLayout>
</RelativeLayout>
我收到一条错误消息致命异常:main。还有致命的例外。我不明白这个错误消息是什么意思


我想我没有做那么多,所以我不明白为什么它不工作。

必须在onCreate中绑定视图。因为根据Android生命周期onStart->onCreate执行。。 所以,基本上问题是您正在访问imageView以设置动画,但尚未初始化。 所以,你的代码应该是这样的-

`import      android.support.v7.app.AppCompatActivity; 
 import android.os.Bundle;
 import android.view.View; 
 import android.widget.ImageView; 
 public class MainActivity extends    AppCompatActivity 
 { 
 private ImageView counter;
@Override 
protected void onCreate(Bundle   savedInstanceState) { 
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = (ImageView) findViewById  (R.id.imageView);
counter.setTranslationY(-1000f);     counter.setImageResource(R.drawable.yellow);     counter.animate().translationYBy(1000f).setDuration(300); 
  }
}`

请添加此渐变文件

编译'com.android.support:multidex:1.0.1'

让我们来上下面的课

public class MultiDexApplication extends Application {
    public MultiDexApplication() {
    }

    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}

并在清单中定义错误日志:02-09 09:22:40.526 741-741/se.jakobia.connect3 E/AndroidRuntime:FATAL EXCEPTION:main Process:se.jakobia.connect3,PID:741 java.lang.RuntimeException:无法启动活动组件信息{se.jakobia.connect3/se.jakobia.connect3.MainActivity}:android.view.InflateException:二进制XML文件行0:二进制XML文件行0:请完整显示stacktrace尝试删除转换,然后执行。。代码应该运行