Omar AlMA3lOl
Omar AlMA3lOl

Reputation: 278

Android - I'm trying to make Onclick Event and while the app start it give me this Error

I'm trying to make an app that change some text in main activity but this is not the error. The real error in OnClick Event it says:

Incompatible Types

and here is the two files that contains the error

first_.xml is:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".First_Activ" >
<item android:id="@+id/action_settings"
    android:title="@string/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never" />
<item android:title="AlMA PRO Leader - First Test"
    android:id="@+id/AlmaSettingItem"
    android:onClick="Doit"
    />
</menu>

First_Activ.java is :

@Override
public Void DoIt(MenuItem Item){
    TextView txt= (TextView)findViewById(R.id.ARMY );
    txt.setText("Done");
    return true;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.first_, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

Error is in First_Activ.java

 @Override
public Void DoIt(MenuItem Item){
    TextView txt= (TextView)findViewById(R.id.ARMY );
    txt.setText("Done");
    return true;
}

Upvotes: 0

Views: 86

Answers (2)

PaintedRed
PaintedRed

Reputation: 1418

This code can't be compiled. You are trying to return something. But what your

public Void DoIt(MenuItem Item) 

is actually returning?

Omar, I think you don't know the main principles of Java. I suggest you to read Java basics and Android. Writing the code and asking the help here without understanding what this code is actually suppose to do is not the solution.

Upvotes: 2

codeMagic
codeMagic

Reputation: 44571

You have it declared in your xml as

android:onClick="Doit"

but in Java you have

public Void DoIt(MenuItem Item){

"Doit" isn't the same case in both. Fix that. If that isn't your problem then please post the errors.

So change in your xml

android:onClick="doIt"

Also, remove the return statement in your function and the return type to void and change the function name so it sticks to Java standards.

@Override
public void doIt(MenuItem item){
    TextView txt= (TextView)findViewById(R.id.ARMY );
    txt.setText("Done");
}

I also changed Item to item to stick with Java standards

Upvotes: 2

Related Questions