-2

When I Run Android Studio project shows Application unfortunately Stopped

The following are the activity_main.xml ,and MainActivity.java Code.

activity_main.xml


    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="@color/abc_primary_text_material_dark" >

<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="40dp"
    android:background="@color/abc_primary_text_disable_only_material_light"
    android:text="@string/black"
    android:onClick="onClick"/>

<ImageView
    android:id="@+id/image1"
    android:layout_width="320dp"
    android:layout_height="250dp"
    android:scaleType="fitXY"
    android:contentDescription="@string/black"
    android:onClick="onClick"/>

</RelativeLayout>

MainActivity.java

    package com.sudheergv.pndu;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.view.View;

import java.util.*;

public class MainActivity extends Activity {
    ImageView imageView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView= (ImageView)findViewById(R.id.image1);

    }


    public void onClick(View view) {



        int[] picture=

                {
                        R.drawable.android3d,R.drawable.android,R.drawable.image1};
        Random r = new Random();
        int n=r.nextInt(picture.length);
        imageView.setImageResource(picture[n]);
    }
}
tenten
  • 1,276
  • 2
  • 26
  • 54
Sudheergv
  • 11
  • 6
  • 3
    please post the error log – Msk Apr 14 '15 at 13:05
  • 2
    your `picture` array has a length of `3` and `n` is possible to have a value of `6`, sooner or later you will get an `ArrayIndexOutOfBoundsException` exception. – Titus Apr 14 '15 at 13:07
  • please tell me what to do ??? – Sudheergv Apr 14 '15 at 13:10
  • You can change `r.nextInt(7)` to `r.nextInt(picture.length)` – Titus Apr 14 '15 at 13:11
  • i think you have `outOfBoundIndex exeption` check out your log file – Josef Apr 14 '15 at 13:46
  • The error log for android can be found by using `LogCat` - at the time of the crash you should see a red stack trace - please click the [edit] button and add this stacktrace to help us to better help solve your issue. Also, try and first clean your project by in Eclipse / Android Studion -> Project -> Clean. This will regenerate your `R` file which may have been corrupted by major refactoring. – Matt Clark Apr 14 '15 at 14:51

1 Answers1

2

You should put View view in your onClick method;

public void onClick(View view) {

    int[] picture = {
        R.drawable.android3d,R.drawable.android,R.drawable.image1};
        Random r = new Random();
        int n=r.nextInt(7);
        imageView.setImageResource(picture[n]);
}

and you should give your ImageView a drawable like this :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:background="@color/abc_primary_text_material_dark" >

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="40dp"
        android:background="@color/abc_primary_text_disable_only_material_light"
        android:text="@string/black"
        android:onClick="onClick"/>

    <ImageView
        android:id="@+id/image1"
        android:layout_width="320dp"
        android:layout_height="250dp"
        android:src="@drawable/your_drawable"
        android:scaleType="fitXY"
        android:contentDescription="@string/black" />

</RelativeLayout>
Thomas Vos
  • 12,271
  • 5
  • 33
  • 71
  • public void onClick(View view) { int[] picture= { R.drawable.android3d,R.drawable.android,R.drawable.image1}; Random r = new Random(); int n=r.nextInt(picture.length); imageView.setImageResource(picture[n]); } Modified but still same friends – Sudheergv Apr 14 '15 at 13:33
  • Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class android.widget.RelativeLayout at android.view.LayoutInflater.createView(LayoutInflater.java:633) at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741) at android.view.LayoutInflater.inflate(LayoutInflater.java:482) @SuperThomasLab – Sudheergv Apr 14 '15 at 13:41
  • I found the bug! You should give your imageview a drawable: in your imageview you should put android:src="@drawable/example_drawable" – Thomas Vos Apr 14 '15 at 13:51
  • it now showing like this now Rendering Problems tag requires a 'drawable' attribute or child tag defining a drawable – Sudheergv Apr 14 '15 at 13:52
  • you mean android:src="@drawable/Image name" image name ahh?? @SuperThomasLab – Sudheergv Apr 14 '15 at 13:59
  • How to Accept Please tellme @SuperThomasLab – Sudheergv Apr 14 '15 at 14:37