Question

I am getting the error on "onActivityResult" stating "onActivityResult(int, int, Intent) is undefined for the type Object"

I've looked at people that have had the same problem and they've said that it's caused due to poorly placed curley braces. I have tried to re-position them but keep getting more and more errors.

hopefully someone could help me out with this:

package com.website.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.util.Log;
import android.widget.EditText;

public class ListmenuActivity extends Activity {

@Override
protected void onCreate(Bundle saveInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(saveInstanceState);
    setContentView(R.layout.activity_listmenu);

    EditText CPU = (EditText) findViewById(R.id.autoCompleteTextView4);
    CPU.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent CPUList = new Intent(getApplicationContext(),
                    CPUList.class);
            startActivityForResult(CPUList, 1);

        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {

        if (requestCode == 1)
            ;

        Intent i = getIntent();
        String product = data.getStringExtra("product");
        EditText CPU = ((EditText) findViewById(R.id.autoCompleteTextView4));
        CPU.setText(product);

    }

    EditText RAMList = (EditText) findViewById(R.id.autoCompleteTextView1);
    RAMList.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent RAMList = new Intent(getApplicationContext(),
                    RAMList.class);
            startActivityForResult(RAMList,1);

        }

        protected void onActivityResult(int requestCode, int resultCode,
                Intent data) {

            super.onActivityResult(requestCode, resultCode, data);

            if (resultCode == RESULT_OK) {

                if (requestCode == 1)
                    ;

                Intent i = getIntent();
                String product = data.getStringExtra("product");
                EditText RAM = ((EditText) findViewById(R.id.autoCompleteTextView4));
                RAM.setText(product);

            }
        }

    });

}

}
Was it helpful?

Solution

RAMList.setOnClickListener(new View.OnClickListener() {
    // ...
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // ...
    }
});

This is not going to work as you expect. That onActivityResult method will never be called.

onActivityResult is a method of an Activity, invoked by the Android runtime whenever that activity receives the result of another activity which it launched through startActivity(ForResult). You are trying to define it on a class solely implementing the View.OnClickListener interface, which does not have an onActivityResult method. It does not override any method of a superclass or interface, it's just a new method which has no special purpose at all.

You are passing that class instance to setOnClickListener, which is only concerned with calling onClick - as that is in the View.OnClickListener interface. It doesn't know that there's an onActivityResult method in that object, and it simply doesn't care.

If you want to get this working, you need to put that code in the onActivityResult of your ListmenuActivity. Since that class inherits Activity, its onActivityResult will be called for all activities launched by startActivity(ForResult). You'll need to differentiate between your two separate launches so you know what to do for the given result. That's why Android provides you with a requestCode: give your different launches different requestCodes and you'll be able to separate them when you receive their results in onActivityResult.

Thus, your ListmenuActivity.onActivityResult method should look like:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        // Handle result of CPUList
    } else if (requestCode == 2) {
        // Handle result of RAMList
    }
    super.onActivityResult(requestCode, resultCode, data);
}

You would launch your subactivities with startActivityForResult(CPUList, 1) and startActivityForResult(RAMList, 2).

(Side note: it's very confusing if you name two different variables RAMList - an EditText and an Intent - in the same scope.)

OTHER TIPS

I think you don't need to call:

super.onActivityResult(requestCode, resultCode, data);

and this code do nothing:

if (requestCode == 1)
    ;

You can remove it.

there is already onActivityResult() delared no need to define it two times.

another thing is you had mention 2nd onActivityResult() in onClickListener() which is wrong.

Wrong Points in your code

  1. onClickListener() can not have onActivityResult() in it.
  2. you don't need to do findViewById() at onActivityResult() as you had already done it at onCreate().

problem of missing brackates

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   super.onActivityResult(requestCode, resultCode, data);
   // your code
    if (requestCode == 1) {
        Intent i = getIntent();
        String product = data.getStringExtra("product");
        CPU.setText(product);
    }
}

you just need to replace this onActivityResult() with these one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top