Last Updated
Viewed 23 Times

I'm making a small game using Android studio wich have 3 activities : Menu , Main(Game) , WinScreen . When I try to go from Menu to Main or WinScreen to Main it works perfectly fine but when I try to launch an activity from the Main activity I get an error.

I have a method in a java file that checks if the player won and if that's the case it is supposed to launch the WinScreen activity.

boolean checkWin(GameBoard gameboard){
       if(compareTabs(gameboard) == true){
           System.out.println("Win !");
           Intent i = new Intent(MainActivity.this,WinActivity.class);
           startActivity(i);
           return true;
       }
       else{
           return false;
       }
}

And this is the error I get : error: not an enclosing class: MainActivity

This method is located in a file called GameBoard , GameBoard is used by the GameView Class who is launched at the start of the MainActivity

I know there is hundreds of posts similar to mine but I've pretty much tried everything I've found already and nothing seems to work and I'm pretty sure it's a really dumb issue .

I've already tried things like Intent i = new Intent(this,WinActivity.class);

I am unable to append a string of text from a non activity class to an EditText view.

I have tried passing a view as a parameter to the constructor of the class.

The Basic problem is that I am unable to use findViewById in a non-Activity class.

I know this is a silly question, but I have tried a lot but simply not able to get that.

My code sample is :

    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.printtesting;

import android.app.Activity;
import android.widget.EditText;
import java.math.BigInteger;
import java.util.Random;
import java.util.Vector;
//import javax.swing.JOptionPane;

/**
 *
 * @author HP
 */
public class keyGenerator {

    /**
     * @param args the command line arguments
     */

      static  Vector check = new Vector();


      static protected BigInteger p= new BigInteger("161");
      static protected BigInteger q= new BigInteger("47");
      static protected Random s = new Random();
      static protected BigInteger n = new BigInteger("1");
      static protected BigInteger trails;
      static protected BigInteger lambda ;
      static protected BigInteger nsq  = new BigInteger("1");
      static protected BigInteger g = new BigInteger("1");
      static protected BigInteger temp = new BigInteger("1");

    static protected int maxbit;
    private static BigInteger two = new BigInteger("2");

    public keyGenerator() {
      //  p = new BigInteger(7,1,s);
                System.out.println(p.toString());
      /*  while(!isPrime(p) && ( (p.compareTo(two)==1) || (p.compareTo(two))==0) )
        {
                p = new BigInteger(7,1,s);

        System.out.println(p.toString());
        }*/
        System.out.println("P is " + p);


    //    q = new BigInteger(7,1,s);
      /*  while(!isPrime(q) && ( (q.compareTo(two)==1) || (q.compareTo(two))==0) )
        {
                q = new BigInteger(7,1,s);
        }*/
        System.out.println("Q is " + q);




        // TODO code application logic here

       // BigInteger oth = new BigInteger("132312"); 
       generateKey();

    }


    protected  void generateKey()
    {
   EditText et = (EditText) Activity.findViewByID(R.string.te);
    // N=pq
    n=n.multiply(p);
    n=n.multiply(q);
    ....
}

<summary>

Is there an elegant way of getting the result of an activity from a non-activity class, or is it always going to be a hack? Would I be better avoiding that situation if possible?

</summary>

I have an activity, that has an instance of a session for a web service, which will generally be pre-authenticated (the session key will be saved in SharedPreferences). However, the first time it runs, I will need to pop up a log in activity, which will return the username and password to the session, which will then talk to the server and get a session key.

Ideally, I would like to have the non-activity session class call startActivityForResult, but I'm not sure it's possible. I've seen various questions about how it can be done:

How to get the result from OnActivityResult inside another class?(outside of the activity

use startActivityForResult from non-activity

But they all seem a little hacky. I've also seen this, which suggests that passing round contexts is not the best idea anyway:

http://android-developers.blogspot.co.uk/2009/01/avoiding-memory-leaks.html

Another option is to save the username and password to sharedpreferences and retrieve it in the other class, but then I'd have to have some way of figuring out when it had been saved, and it all seems to be getting rather messy.

Another option is to make the session a non-displaying activity. Seems like a large overhead though.

My project is still in planning stages, so I'm after best practice. What is the most elegant way to do this?

I have a map view activity that shows a few OverlayItems. In the onTap method of an overlay, I want to trigger a new activity that shows this overlay photo as a fullscreen, for example.

When I do this inside my overlay class:

Intent intent = new Intent();
intent.setClass(getApplicationContext, FullscreenView.class);
startActivity(intent);

.. it can't find an application context, as I am not in the scope of an activity.

When I add a method to my main activity, let's say startFullscreen:

public static void startFullscreen() {
    if (sCurrentPhoto != null) {
        Intent intent = new Intent();
        intent.setClass(getApplicationContext(), FullscreenView.class);
        startActivity(intent);          
    }
}

I can not call getApplicationContext() and startActivity(), because I am in a static context. I need the static method however to call it in the Overlay class like MainView.startFullscreen().

Put simply: How can I start an Activity from a non-Activity class?

Similar Question 5 (1 solutions) : Correctly starting an intent from non-activity class

Similar Question 6 (1 solutions) : Override not working in non-activity class

Similar Question 7 (1 solutions) : getSharedPreferences error in non-activity class

cc