-1

debugging an android project I get the error "IllegalStateException: Could not execute method of the activity".

I did research in this site and others and I found a lot of similar problems but I wasn't able to apply the suggested solutions to my case.

Can you help me? Even giving me links to tutorials, guides, etc. but if someone thinks I don't deserve helps please explain me the reason so that I can learn.

package nv.bao;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;

public class BaoActivity extends Activity{
private MediaPlayer menuSong;
private RadioGroup numberStartingStones;
private int startingStoneCount;
private boolean singlePlayer=false;
private BaoView bView;

@Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onC reate(savedInstanceState);
    menuSong = MediaPlayer.create(KahlaActivity.this, R.raw.planescape2);
    setContentView(R.layout.activity_bao);
    startingStoneCount=10;
    numberStartingStones=(RadioGroup) findViewById(R.id.radioGroup1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_bao, menu);
    return true;
}   


// Called when the user clicks the Start button.  This will set the board view.
public void startGame(View view){
    setContentView(R.layout.baoboard);
    bView = (BaoView) findViewById(R.id.baoboard);  
    }

This is the LogCat

03-13 04:56:24.758: D/AndroidRuntime(2522): Shutting down VM
03-13 04:56:24.758: W/dalvikvm(2522): threadid=1: thread exiting with uncaught exception (group=0x41465700)
03-13 04:56:24.878: E/AndroidRuntime(2522): FATAL EXCEPTION: main
03-13 04:56:24.878: E/AndroidRuntime(2522): java.lang.IllegalStateException: Could not execute method of the activity
03-13 04:56:24.878: E/AndroidRuntime(2522): at android.view.View$1.onClick(View.java:3633)
03-13 04:56:24.878: E/AndroidRuntime(2522): at android.view.View.performClick(View.java:4240)
03-13 04:56:24.878: E/AndroidRuntime(2522): at android.view.View$PerformClick.run(View.java:17721)
03-13 04:56:24.878: E/AndroidRuntime(2522): at android.os.Handler.handleCallback(Handler.java:730)
03-13 04:56:24.878: E/AndroidRuntime(2522): at android.os.Handler.dispatchMessage(Handler.java:92)
03-13 04:56:24.878: E/AndroidRuntime(2522): at android.os.Looper.loop(Looper.java:137)
03-13 04:56:24.878: E/AndroidRuntime(2522): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-13 04:56:24.878: E/AndroidRuntime(2522): at java.lang.reflect.Method.invokeNative(Native Method)
03-13 04:56:24.878: E/AndroidRuntime(2522): at java.lang.reflect.Method.invoke(Method.java:525)
03-13 04:56:24.878: E/AndroidRuntime(2522): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-13 04:56:24.878: E/AndroidRuntime(2522): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-13 04:56:24.878: E/AndroidRuntime(2522): at dalvik.system.NativeStart.main(Native Method)
03-13 04:56:24.878: E/AndroidRuntime(2522): Caused by: java.lang.reflect.InvocationTargetException
03-13 04:56:24.878: E/AndroidRuntime(2522): at java.lang.reflect.Method.invokeNative(Native Method)
03-13 04:56:24.878: E/AndroidRuntime(2522): at java.lang.reflect.Method.invoke(Method.java:525)
03-13 04:56:24.878: E/AndroidRuntime(2522): at android.view.View$1.onClick(View.java:3628)
03-13 04:56:24.878: E/AndroidRuntime(2522): ... 11 more
03-13 04:56:24.878: E/AndroidRuntime(2522): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class nv.bao.BaoView
03-13 04:56:24.878: E/AndroidRuntime(2522): at android.view.LayoutInflater.createView(LayoutInflater.java:603)
03-13 04:56:24.878: E/AndroidRuntime(2522): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
03-13 04:56:24.878: E/AndroidRuntime(2522): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
03-13 04:56:24.878: E/AndroidRuntime(2522): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
03-13 04:56:24.878: E/AndroidRuntime(2522): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
03-13 04:56:24.878: E/AndroidRuntime(2522): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
03-13 04:56:24.878: E/AndroidRuntime(2522): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
03-13 04:56:24.878: E/AndroidRuntime(2522): at android.app.Activity.setContentView(Activity.java:1895)
03-13 04:56:24.878: E/AndroidRuntime(2522): at com.example.kahla.KahlaActivity.startGame(BaoaActivity.java:50)
03-13 04:56:24.878: E/AndroidRuntime(2522): ... 14 more
03-13 04:56:24.878: E/AndroidRuntime(2522): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
03-13 04:56:24.878: E/AndroidRuntime(2522): at java.lang.Class.getConstructorOrMethod(Class.java:423)
03-13 04:56:24.878: E/AndroidRuntime(2522): at java.lang.Class.getConstructor(Class.java:397)
03-13 04:56:24.878: E/AndroidRuntime(2522): at android.view.LayoutInflater.createView(LayoutInflater.java:568)
03-13 04:56:24.878: E/AndroidRuntime(2522): ... 22 more
03-13 04:56:30.088: I/Process(2522): Sending signal. PID: 2522 SIG: 9

where is written that the error is caused by "BaoActivity.java:50":

setContentView(R.layout.baoboard);

and

Binary XML file line #8: Error inflating class nv.bao.BaoView

my XML file is the following:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:orientation="horizontal" >

<nv.bao.BaoView
         android:id="@+id/baoboard"
            android:layout_width="match_parent"
                    android:layout_height="match_parent"
                 />

      <Button
          android:id="@+id/quitInGame"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentRight="true"
          android:layout_alignParentTop="true"
          android:onClick="quitGame"
          android:text="@string/menu_button_quit" />

 </RelativeLayout>

Many thanks for your attention, Nino V

EDIT:

I need to pass to BaoView a string contain the initial number of seeds of each pit. I tried to do that as in the commented parameters:

public class BaoView extends View {

private BaoGame game;
private int pitSelected = -1;
private boolean eventOccured;
private static String apertura;
private Point[] pitLocation = new Point[34];
private int[] semeTracker = new int[34];
private Random generator = new Random();
private boolean buttonClicked;
private int playerIndex = 0;
private Bitmap[] imageMap = new Bitmap[8];
private long mMoveDelay = 70;
private long mLastMove;
private RefreshHandler mRedrawHandler = new RefreshHandler();
private String sud = getResources().getString(R.string.player1pit);
private String nord = getResources().getString(R.string.player2pit);

public BaoView(Context context, AttributeSet attrs /*, String strIniziale */) {
    super(context, attrs);
    game = new BaoGame(/* strIniziale */);
    init(/* strIniziale */);
}
public void init(/* String strIniziale */) {
    setFocusable(true);

    loadPitLocations(/* strIniziale */);
    loadImages();
    this.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            float x1 = 0, x2, y1 = 0, y2, dx, dy;
            int d = 0; // direction;
            switch (event.getAction()) {
                case (MotionEvent.ACTION_DOWN):
                    x1 = event.getX();
                    y1 = event.getY();
                    break;
                case (MotionEvent.ACTION_UP):
                    x2 = event.getX();
                    y2 = event.getY();
                    dx = x2 - x1;
                    dy = y2 - y1;
                    if (Math.abs(dx) > Math.abs(dy)) {
                        if (dx > 0) {
                            d = 1;
                        } else {
                            d = -1;
                        }
                    }
                    break;
                default:
                    return false;
            }
            if (buttonListener(x1, y1)) {
                game.doTurn(pitSelected, d);
                playerIndex = game.getCurrentPlayerIndex();
                eventOccured = true;
                update();
            }
            return true;
        }
    });
}

public boolean buttonListener(float x, float y) {
    buttonClicked = false;
    if (playerIndex == 0) {
        for (int i = 0; i <= 15; i++) {
            if (x >= pitLocation[i].x
                    && x <= pitLocation[i].x + 100
                    && y >= pitLocation[i].y
                    && y <= pitLocation[i].y + 100
                    && semeTracker[i] != 0) {
                buttonClicked = true;
                pitSelected = i;
                break;
            }
        }
    }
    if (playerIndex == 1) {
        for (int i = 16; i <= 31; i++) {
            if (x >= pitLocation[i].x
                    && x <= pitLocation[i].x + 100
                    && y >= pitLocation[i].y
                    && y <= pitLocation[i].y + 100
                    && semeTracker[i] != 0) {
                buttonClicked = true;
                pitSelected = (i - 16);
                break;
            }
        }
    }
    return buttonClicked;
}

public void loadImages() {
    Resources r = this.getContext().getResources();
    loadImage(0, r.getDrawable(R.drawable.seme), 15, 15);
    loadImage(1, r.getDrawable(R.drawable.buca), 70, 100);
    loadImage(2, r.getDrawable(R.drawable.granaio), 70, 200);
    loadImage(3, r.getDrawable(R.drawable.tavola), 700, 400);
    loadImage(4, r.getDrawable(R.drawable.mucchio), 70, 100);
    loadImage(5, r.getDrawable(R.drawable.playerone), 325, 75);
    loadImage(6, r.getDrawable(R.drawable.playertwo), 325, 75);
    loadImage(7, r.getDrawable(R.drawable.tavola), 700, 400);
}

public void loadPitLocations(/* String strIniziale */) {
    int W = this.getWidth(); // 800
    int H = this.getHeight(); //404

    String[] separati = strIniziale.split("\\:");
    String HALI=separati[7];
    String[] aHali=HALI.split("-");
    String[] aPlayer = new String[34];
    aPlayer[0] = aHali[16];
    aPlayer[10] = aHali[3];
    aPlayer[11] = aHali[4];
    aPlayer[12] = aHali[5];
    aPlayer[13] = aHali[6];
    aPlayer[14] = aHali[7];
    aPlayer[15] = aHali[8];
    aPlayer[16] = aHali[0]; // ghala
    aPlayer[1] = aHali[15];
    aPlayer[2] = aHali[14];
    aPlayer[3] = aHali[13];
    aPlayer[4] = aHali[12];
    aPlayer[5] = aHali[11];
    aPlayer[6] = aHali[10];
    aPlayer[7] = aHali[9];
    aPlayer[8] = aHali[1];
    aPlayer[9] = aHali[2];

    aPlayer[19] = separati[8]; // Data
    aPlayer[17] = separati[6]; // Kutakatia
    aPlayer[18] = separati[7]; // Name
    aPlayer[20] = separati[9]; // Nyumba

    aPlayer[17] = aHali[17];
    aPlayer[18] = aHali[18];
    aPlayer[19] = aHali[19];
    aPlayer[20] = aHali[20];
    aPlayer[21] = aHali[21];
    aPlayer[22] = aHali[22];
    aPlayer[23] = aHali[23];
    aPlayer[24] = aHali[24];
    aPlayer[25] = aHali[32];
    aPlayer[26] = aHali[31];
    aPlayer[27] = aHali[30];
    aPlayer[28] = aHali[29];
    aPlayer[29] = aHali[28];
    aPlayer[30] = aHali[27];
    aPlayer[31] = aHali[26];
    aPlayer[32] = aHali[25];
    aPlayer[33] = aHali[33]; // ghala

    aPlayer[39] = separati[10]; // Kutakatia
    aPlayer[40] = separati[3]; // Name
    aPlayer[19] = separati[4]; // Data
    aPlayer[20] = separati[5]; // Nyumba
    for (int i = 0; i <= 33; i++) {
        semeTracker[i] = Integer.valueOf(aPlayer[i]);
    }

    for (int i = 0; i <= 7; i++) { // bene
        pitLocation[i] = createPoint((574 - (i * 71)), 95);
    }

    for (int i = 8; i <= 15; i++) { // bene
        pitLocation[i] = createPoint((77 + ((i - 8) * 71)), 0);
    }

    for (int i = 16; i <= 23; i++) {
        pitLocation[i] = createPoint((70 + ((i - 16) * 71)), 210);

    }
    for (int i = 24; i <= 31; i++) {
        pitLocation[i] = createPoint((567 - ((i - 24) * 71)), 300);
    }

    pitLocation[32] = createPoint(0, 0); // bene
    pitLocation[33] = createPoint(630, 200);
}

public Point createPoint(int x, int y) {
    Point location = new Point();
    location.set(x, y);
    return location;
}


public void loadImage(int key, Drawable tile, int width, int height) {
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    tile.setBounds(0, 0, width, height);
    tile.draw(canvas);
    imageMap[key] = bitmap;
}

public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int W = this.getWidth();
    int H = this.getHeight();//404
    Paint paint = new Paint();
    paint.setTextSize(15);
    paint.setARGB(255, 255, 255, 0);
    canvas.drawBitmap(imageMap[3], 10, 0, null); // Tavola
    int[] aXCoord = {14, 15, 19, 20, 24, 33, 33, 39, 28, 35, 37, 46, 51, 48, 52};
    int[] aYCoord = {53, 40, 65, 27, 75, 20, 34, 47, 54, 80, 66, 24, 63, 37, 50};
    ArrayList<Integer> posSemiB = new ArrayList<Integer>();
    for (int i = 0; i <= 14; i++) {
        posSemiB.add(i);
    }
    for (int i = 0; i <= 31; i++) {     //draw player1 pits
        int n = 0;
        if (semeTracker[i] > 0 && semeTracker[i] < 15) {
            Collections.shuffle(posSemiB);
            for (int x = 0; x < semeTracker[i]; x++) {
                // semi
                canvas.drawBitmap(imageMap[0],
                        (pitLocation[i].x - 10 + aXCoord[posSemiB.get(x)]), // aXCoord[coord]),
                        (pitLocation[i].y - 10 + aYCoord[posSemiB.get(x)]), // aYCoord[coord]),
                        null);
            }
        } else if (semeTracker[i] >= 15) {
            // 'mucchio'= >12
            canvas.drawBitmap(imageMap[4],
                    (pitLocation[i].x + 7),
                    (pitLocation[i].y - 5),
                    null);
            paint.setTextSize(32);
            canvas.drawText("" + semeTracker[i], pitLocation[i].x + 15,
                    pitLocation[i].y + 50,
                    paint);
        }
    }
    int[] aXCoordG = {34, 25, 28, 47, 13, 50, 30, 12, 50, 42, 18, 52, 12, 29, 41, 15, 50, 40, 25, 20, 42, 13};
    int[] aYCoordG = {97, 116, 78, 118, 95, 86, 138, 76, 100, 62, 60, 138, 130, 44, 154, 150, 47, 29, 165, 26, 169, 42};
    ArrayList<Integer> posSemiG = new ArrayList<Integer>();
    for (int i = 0; i <= 21; i++) {
        posSemiG.add(i);
    }
    paint.setTextSize(40);
    paint.setARGB(255, 255, 0, 0);
    Collections.shuffle(posSemiG);
    for (int x = 0; x < semeTracker[32]; x++) {
        canvas.drawBitmap(imageMap[0],
                (pitLocation[32].x + 8 + aXCoordG[posSemiG.get(x)]), // aXCoord[coord]),
                (pitLocation[32].y - 5 + aYCoordG[posSemiG.get(x)]), // aXCoord[coord]),
                null);
    }
    canvas.drawText("|" + semeTracker[32], pitLocation[32].x +8, pitLocation[33].y + 280, paint);
    paint.setARGB(255, 0, 0, 255);
    Collections.shuffle(posSemiG);
    for (int x = 0; x < semeTracker[33]; x++) {
        canvas.drawBitmap(imageMap[0],
                (pitLocation[33].x + aXCoordG[posSemiG.get(x)]), // aXCoord[coord]),
                (pitLocation[33].y - 5 + aYCoordG[posSemiG.get(x)]), // aXCoord[coord]),
                null);
    }
    canvas.drawText("|" + semeTracker[33], pitLocation[33].x + 18, pitLocation[33].y + 280, paint);


    if (game.isDone()) {
        // Bao Hamna
        canvas.drawBitmap(imageMap[7], 300, 0, null);
    } else {
        if (playerIndex == 0) {
            paint.setARGB(255, 255, 255, 0);
            canvas.drawText(sud, 300, 0, paint);
        } else if (playerIndex == 1) {
            paint.setARGB(255, 255, 255, 0);
            canvas.drawText(nord, 300, 0, paint);
        }
    }
}

public void setStarting(String x) {
    Log.d("X=",x);
    this.apertura = x;
    Log.d("a-X=",this.apertura);
}

public String getStarting() {
    Log.d("Apertura=",this.apertura);
    return this.apertura;
}

public void updateSemeCounts() {
    int[] temp = new int[6];
    for (int i = 0; i <= 1; i++) {
        if (i == 0) {
            for (int x = 0; x <= 5; x++) {
                temp = game.getPitsCounts(i);
                semeTracker[x] = temp[x];
            }
        }
        if (i == 1) {
            for (int x = 0; x <= 5; x++) {
                temp = game.getPitsCounts(i);
                semeTracker[x + 6] = temp[x];
            }
        }
    }
    semeTracker[12] = game.getScore(0);
    semeTracker[13] = game.getScore(1);
}

public void update() {
    if (eventOccured) {
        long now = System.currentTimeMillis();
        if (now - mLastMove > mMoveDelay) {
            updateSemeCounts();
            mLastMove = now;
            eventOccured = false;
        }
        mRedrawHandler.sleep(mMoveDelay);
    }

}
class RefreshHandler extends Handler {

    private Point startPoint, endPoint, movePoint;

    public void handleMessage(Message msg) {
        BaoView.this.update();
        BaoView.this.invalidate();
    }

    public void sleep(long delayMillis) {
        this.removeMessages(0);
        sendMessageDelayed(obtainMessage(0), delayMillis);
    }
};

}

  • whatever baoview is, appears to be the cause. besides, why is that layout supposed to look like? boaview takes up all parent space, yet you add a button to it. Might want to use framelayout. – Daniel Bo Mar 13 '14 at 10:01
  • @Daniel Bo: the layout displays a mancala board (Bao) with for row of 8 pits each and in each pit it should be some seeds. I think you are right in saying that the cause is BaoView, if fact I tried to pass to it a parameter. But I don't understand how to do that, so I deleted the parameter in calling the class but not in the class itself. Can you help me in understanding how to pass that parameter? – Nino Vessella Mar 13 '14 at 15:07
  • @blackbelt: please see the edited message and my comment to Daniel Bo. Thank you all. – Nino Vessella Mar 13 '14 at 15:08
  • adding that constructor fixed the IllegalStateException? – Blackbelt Mar 13 '14 at 15:19
  • @blackbelt: yes. The error was in passing the parameter. I have to pass to that class a string. I don't know how to do that :( I tried to implement a set/get pair, but it didn't work. Can you help me. – Nino Vessella Mar 13 '14 at 15:41
  • That is another issue then. – Blackbelt Mar 13 '14 at 16:07

1 Answers1

1
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]

you need the constructor with Context and AttributeSet inside your BaoView

   public BaoView(Context context, AttributeSet attr) {

   }
Blackbelt
  • 156,034
  • 29
  • 297
  • 305