Getting a "must override superclass method" when compiling facebooks sample code - java

I'm trying to compile the sample code from the facebook sdk. Now I'm getting errors on all the
lines below #Override
example on the following lines
#Override
public void startActivityForResult(Intent intent, int requestCode)
I get a error that says:
the method startActivityForResult of type new must override super class type
could I just delete all the #Override

Whatever class you are extending is assumed to have a method declaration of startActivityForResult with exact return type and parameters. Check if you are following it. You can remove #Override and it will work, but by that you are saying, this method doesn't exist in your super class. Verify the class you perhaps extending. If this method is supposed to be invoked and you do not, something else will break.

Method signature looks fine. You don't extend android.app.Activity class.
public class MyActivity extends Activity {
...

Related

Extending Kotlin class by Java requires me to reimplement already implemented method

The simplest code to demonstrate the issue is this:
Main interface in Kotlin:
interface Base <T : Any> {
fun go(field: T)
}
Abstract class implementing it and the method:
abstract class Impl : Base<Int> {
override fun go(field: Int) {}
}
Java class:
public class JavaImpl extends Impl {
}
It should work, but it doesn't. The error is
Class 'JavaImpl' must either be declared abstract or implement abstract method 'go(T)' in 'Base'
If the JavaImpl class was in Kotlin, it would work. Also if the T was cast to String or Integer or any object, it would work too. But not with Int.
Is there any clever solution apart from using Integer and suppressing hundreds of warnings in Kotlin subclasses?
Update: created the issue.
Looking at the byte code we can see, that the Impl-class basically has produced the following function:
public go(I)V
where the parameter is a primitive integer. Also a synthetic bridge-function (go(Object)) is generated, which however would also be generated on the Java-side for such generic functions.
On the Java side however it doesn't suffice to have something like public void go(int field) in place. Now we need that go(Integer field)-function, which isn't present.
For me that sound's like an interop-problem that should probably be reported and linked back here again. Actually having had some time to investigate, there seem to be some issues already: KT-17159 and KT-30419, but also KT-5128 seem to relate to this problem. The kotlin compiler knows how to deal with this and doesn't need any further information about it in the class-file (i.e. the implementing Kotlin class knows, that it doesn't need to implement something like fun go(field : Int?)). For the Java-side such counterpart does not exist. I wonder whether this could even be fixed nicely with the compiler/byte-code or whether this will remain a specific interop-problem.
Some workarounds to deal with that problem (in case this is deliberate and not a real problem):
Add an additional function as follows to Impl:
fun go(field : Int?) = go(field ?: error("Actually the given field should never be null"))
// or simply:
fun go(field : Int?) = go(field!!)
That way you would not need to implement it. However then you would also expose that nullable function to the Kotlin side, which you probably don't want.
For that specific purpose it may seem more convenient to do it the other way around:
declare the class and the interface in Java and use it on the Kotlin side. That way you could still declare something like
abstract class KotlinClass : JavaInterface<Int> {
override fun go(field : Int) { // an IDE might suggest you to use Int? here...
// ...
}
}
You can use javap to analyze the problem, showing members of compiled interface and classes.
javap Base
public interface Base<T> {
public abstract void go(T);
}
javap Impl
public abstract class Impl implements Base<java.lang.Integer> {
public void go(int);
public void go(java.lang.Object);
public Impl();
}
So, the problem is exactly that pointed out by #Roland: in order to satisfay the contract requested by the Base interface, the Java compiler needs a public void go(java.lang.Integer) method but the method generated by Kotlin compiler has int as parameter.
If you implement the interface in Java, with something like
class JI implements Base<Integer> {
#Override
public void go(#NotNull Integer field) {
}
}
you can analyze its compiled version with javap obtaining
javap JI
class JI implements Base<java.lang.Integer> {
org.amicofragile.learning.kt.JI();
public void go(java.lang.Integer);
public void go(java.lang.Object);
}
So, if you plan to use Kotlin class Impl as superclass of Java classes, the solution is simply to use <Integer>, not <Int>, as type parameter: Int is a Kotlin class, translated to int by the compiler; Integer is the Java class you usually use in Java code.
Changing your example to
abstract class Impl : Base<Integer> {
override fun go(field: Integer) {}
}
public class JavaImpl extends Impl {
}
the JavaImpl Java class compiles without errors.

Passing "this" from Java Interface to another Class

Giving the following class of my android Project :
Preview extends SurfaceView implements SurfaceHolder.Callback
and
A extends Doc
I don't really know how to ask and I know that this is not really good but I want that from Preview, I call an abstract method of Doc. In this Doc's method, I have to call a method of the previous Object of Preview.
This is an example :
From Preview.java :
Doc _doc = new A();
private void myMethod() {
this._doc.process(this)
}
From A.java :
#Override
public void process(Preview p) {
p.processA();
}
The problem is that I got an error :
The method process(Preview) in the type Doc is not applicable for the arguments (new Camera.PreviewCallback(){})
However, I can't change this judging by the fact that I want to call the method from Preview. I tried many thing such as cast etc. None of them works.
Thanks for your help !
PS : I am on Eclipse under Windows.
Assuming you are calling A.process(this) from an anonymous inner class (of type Camera.PreviewCallback I presume, hence the error message), you have to write A.process(Preview.this), since a standalone this refers to the inner class and not to the Preview instance.
The method process(Preview) in the type Doc is not applicable for the arguments (new Camera.PreviewCallback(){})
It simply means you are passing the object of Camera.PreviewCallback but in your method public void process(Preview p) You want an object of Preview.
If you have written this code in side an anonymous class, then this won't point to the Preview class. It will point to the object of inner anonymous class.
Thus you need to write A.process(Preview.this)

Method undefined error in Android Activity

I'm trying to make a simple Android application based on a guide. I am using the code below, but it is giving me several errors. It is complaining while trying to override the onCreate() method. The exact error is below. Can anyone explain what mistake I am making here?
package com.bignerdranch.android.geoquiz;
import android.os.Bundle;
public class CheatActivity {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cheat);
}
}
The exact errors:
The method onCreate(Bundle) is undefined for the type Object
The method setContentView(int) is undefined for the type CheatActivity
The method onCreate(Bundle) of type CheatActivity must override or implement
a supertype method
import android.app.Activity;
public class CheatActivity extends Activity {
You should extend from Activity class. Because you are just creating new class without any methods which can be overridden from parent class.
As Anatol said, you have to add extends Activity.
If you didn't know that, the only method you have is implemented when extending from Activity.
I would recommend you to create Activities with your IDE's wizard. And you will avoid having to add them manually to the manifest and adding unimplemented methods.

new to android understanding listener = (OnItemSelectedListener) activity

I was going through the vogella's tutorial and came across this code:
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof OnItemSelectedListener) {
listener = (OnItemSelectedListener) activity;
} else {
throw new ClassCastException(activity.toString()+ " must implemenet MyListFragment.OnItemSelectedListener");
}
}
I am not sure what it does. Here is what i do know:
i know that onAttach is called when the fragment has been associated with the activity.
i know that OnItemSelectedListener is an interface
I know that it checks if the activity correctly implements this interface, but not sure what the following code does : listener = (OnItemSelectedListener) activity
all i understand is that it type casts it to become OnItemSelectedListener... but what else does it do ?
An interface is specified on the class declaration. An Activity class can extend Activity and implement the OnItemSelectedListener.
public class MyActivity extends Activity implements OnItemSelectedListener { }
The code only knows that it is of type Activity when onAttach is called, but you want an OnItemSelectedListener, so you need to cast activity. Thats what the listener = (OnItemSelectedListener)activity code does, it casts the instance activity. So in order to avoid a generic ClassCastException, a check is performed first and if activity is not of the correct type, an exception with a more specific error text is thrown.
There's nothing particuarly special going on, the cast enables you to call methods defined in that interface. You don't get any new instances or something, listener and activity is the same object.
It is actually useless outside the tutorial project you were reading on vogella. This code in particular checks if the FragmentActivity trying to be associated with the parent activity implements OnItemSelectedListener and the reason it is doing so is because if an action that doesn't implement all the properties of OnItemSelectedListener is attached to the AdapterView it will raise an exception because the Dalvik Virtual machine won't be able to handle the request to execute that particular action properly.
The following line:
litener = (OnItemSelectedListener) activity;
converts the object of type FragmentActivity to an object of type OnItemSelectedListener and copies that value to the listener variable which is most likely an attribute of the class that implements the onAttach(Activity activity) method in question.

Cannot set the view from activity android

I am facing a strange problem suddenly in android. I am creating layout .xml file in res>layout but from a class that extends activity, I can not access it. I have done this several a time but I can not figure out this strange problem. I restarted the eclipse, even the computer but no. When I go for setting the contentView it says something as below :
The method onCreate(Bundle) of type MainActivity must override or implement a supertype method
That is let I have created an xml file at res>layout named "whynot.xml" and from a activity when I try to set it like
setContentView(R.layout.whynot);
It shows red mark below and the above message.
you have to extends Activity and call super.onCreate(savedInstanceState) in the onCreate. For each callback of the Activity's lifecycle is mandatory to call its super
As the error says,
must override or implement a supertype method
you need the super call like
#Override
protected void onCreate(Bundle bundle)
{
super.onCreate(bundle); // here
setContentView(...);
...
}

Categories