I am a beginner making Android apps. Try doing a tutorial and add a Relative layout and a Gridlayout and some Imageviews. But my app crashes.
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);
}
}
and 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>
I got an error message FATAL EXCEPTION: main. And fatal Exceptions main. I don't understand what the error messag means.
I think I haven't done that much, so I don't understand why it is not working.
You have to bind the view in onCreate() . Since according to Android lifecycle onStart() -> onCreate() execute.. So, basically the problem is you are accessing imageView to set animation but you haven't initialized. So, your code should be something like this-
`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);
}
}`
Please add this gradle file
compile 'com.android.support:multidex:1.0.1'
and make the class below
public class MultiDexApplication extends Application {
public MultiDexApplication() {
}
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
and define in manifest also
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.