Issues with migrating to P3 in Eclipse

edited November 2015 in Android Mode

I am migrating my project from P2 to P3 in Eclipse and experiencing a few issues. As such I have a few questions:

1: Is the following still the correct way to declare the MainActivity class?

public class MainActivity extends PApplet{
}

2: Eclipse is telling me that the following method is apparently final and so I cannot override it to set the right renderer:

public String sketchRenderer() {
    return OPENGL; 
}

How, then, do I set the right renderer?

3: Eclipse is telling me:

The method getSharedPreferences(String, int) is undefined for the type MainActivity

for the following snippet:

SharedPreferences sharedPref = getSharedPreferences("Data", 
        Context.MODE_PRIVATE);

This was not the case for P2. All other methods within MainActivity that worked with P2 that were called like this from MainActivity:

this.method();

give the same error in P3. The error disappears when I modify the code to the following:

SharedPreferences sharedPref = getActivity().getSharedPreferences("Data", 
        Context.MODE_PRIVATE);

Will this solve the issue?

4: Using P2, I was able to override Android's onStart() method using:

@ Override
protected void onStart() {
    super.onStart();
}

This is no lnger possible, since Eclipse is telling me:

Cannot reduce the visibility of the inherited method from PApplet

Changing it to:

@ Override
public void onStart() {
    super.onStart();
}

solves the issue, but why could I override the method in P2 and not in P3?

Thank you in advance for your answers.

Answers

  • edited November 2015

    @witt221===

    -1= as for me, yes

    -2= i read here https://raw.githubusercontent.com/processing/processing/master/build/shared/revisions.txt that > For those familiar with them, the sketchWidth(), sketchHeight(), sketchRenderer(), etc methods should no longer be used. This new implementation of the settings() method is a simpler solution.

    -3 = you are in a fragment, so you have to call getActivity() (as you have done with getSharedPreferences()

    • 4= the error message is very clear: using "protected" you "reduce" the visibility of onStart() to this class or subclasses but this method is an inherited one so you cannot do that...
  • Thank you for your answer. I applied changes to the code, as you specified. Whenever I try to launch the application, I get the following error:

    E/AndroidRuntime(2133): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.yo1/com.example.yo1.MainActivity}: java.lang.ClassCastException: com.example.yo1.MainActivity cannot be cast to android.app.Activity E/AndroidRuntime(2133): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2250) E/AndroidRuntime(2133): ... 10 more

    Do you know where this comes from?

  • I suspect the error is in connection with the following difference between P2 and P3:

    For P2:

    public class PApplet extends Activity implements PConstants, Runnable {}
    

    For P3:

    public class PApplet extends Fragment implements PConstants, Runnable {}
    

    Hence, in P2, PApplet was an activity. In P3, it is a Fragment.

    Please let me know if you think this is a correct approach for solving the issue:

    PApplet being an activity in P2 and a Fragment in P3 would suggest that, in P3, MainActivity should not extend PApplet. Instead, a different class (let's call it P) should be created that extends PApplet. There we can override draw(), mousePressed() etc. to behave like we want it to. P is instantiated in MainActivity, in OnCreate(), I guess. What I find problematic about this is that probably functions like draw() and mousePressed() will not be called when they need to be because there is no looper (calling draw() continuously and mousepressed() when screen is touched) set for P.

    I would be really grateful if you could enlighten me as to how to use Procsessing 3 Android Mode in Eclipse.

    Thanks in advance!

  • Is there anyone who created projects in Eclipse using Processing 3 willing to help me? Thanks!

  • Thanks @GoToLoop, but those threads cover only the following:

    • Using Processing 2 in Eclipse for Desktop applications
    • Using Processing 3 in Eclipse for Desktop applications
    • Using Processing 2 in Eclipse for Android applications

    What I am looking for is:

    • Using Processing 3 in Eclipse for Android applications

    Do you know of any resources covering this topic?

    Thanks.

  • edited November 2015 Answer ✓

    @witt221===

    -1: in order to understand how i solved that in a rather weird way you have to launch p3 android mode WITHOUT eclipse, p5 IDE; then write some lines of code; then save && export as android project (left menu).

    -2: go and have a look at the folder created by P5:: you can see the "structure" created with folders like "assets" "res" &&so on as in eclipse standard; go to the src folder, here you can find 2 java files; first one is called MainActivity: open it and read it:: you understand what P5 is doing "under the table"== it creates an activity then creates a fragment for PApplet; if you go to the layout folder you find this fragment layout, called "main.xml". At this point you can understand a lot of things:: a) as you are no more in and activity but in a fragment a lot of methods must be called through ".getActivity" (even if P3 says that this method does not exist!!!) b) you have also to look at the lifeCycle for fragments which is not the same that for an activity c) you understand why you cannot create in a simple way some android project using P3 in eclipse:: in order to do that you have to IMITATE what P3 does itself:

    -3: so, FIRSTLY you create an Activity (in eclipse) -

    -4: SECONDLY add a layout (FrameLayout) to your main layout (linear or RelativeLayout + frameLayout element, depends of your goal) BUT as for the FrameLayout element you have to give it an R.id the same as P3 does by its own ways (look at the java code in src). So::

                      <FrameLayout
                            android:id="@+id/container"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
    
                             />
    

    -5: you are quite ready now: you create a new class with eclipse::

        public class MonSketch extends PApplet {
    
        @ Override
            public void settings() {
    
                size(900, 1200, P2D);
            }
    
            @ Override
            public void setup() {
              textSize(48);
        text("Hello World!!!", 500,600);
            }
    
            @ Override
            public void draw() {
                //do something
            }
        }
    

    -6: FINALLY you go again to your mainActivity and add (in the onCreate() your sketch as a fragment, using FragmentManager in the standard way....

        FragmentManager fragmentManager = getFragmentManager();
        Fragment frag= new MonSketch();
        fragmentManager.beginTransaction()
                      .replace(R.id.container, frag) //here the R.id!!!
                      .commit();
    

    PS: of course you have to add imports and jars as usually.

  • @akenaton This is exactly what I was looking for. Thank you.

Sign In or Register to comment.