0

For instance, the mainActivity.java file is really clustered and to keep it clean i created a second .java(class) where i will execute some code upon a button press. I cannot figure out how to do it at all. And i am not sure what search terms to use either so i apologize if this has been covered.

Heres what i have in my "test" application.

I have a main activity with a single button on it.

package com.test.secondclass;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

    Button startButton;
    final Intent second = new Intent(getApplicationContext(), testClass.class);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startButton = (Button)findViewById(R.id.button1);

        startButton.setOnClickListener(new View.OnClickListener() {

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

            }
        });
    }

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

}

Now here is the "second" class that i made, now remember this is very short i am just using this for an exercise program before implementing it into my actual program.

package com.test.secondclass;

import android.app.Activity;
import android.widget.Toast;

public class testClass extends Activity{

    public void onCreate(){
        Toast.makeText(getApplicationContext(), "Second class thinger started", Toast.LENGTH_LONG).show();
    }
}

And if i try this i get a force close immediately. If i comment out the "intent" part at the very beginning of my main activity then the program runs. But it doesnt do what i want. obviously. Thanks everyone

Seth
  • 1,769
  • 4
  • 26
  • 39
  • Oh i also thought about using a service, but how would i close the service after it executed the code that i want? – Seth Nov 18 '12 at 23:51
  • I think you are mixing a lot of concepts here, and they are related either to your programming skills or lack of comprehension of the Android SDK. An Activity is a full Form, a new window so to speak. Having methods on a second class does not require said class to be an Activity, just...a class. From there, it all depends on your design, you can either go with a final class with all static methods, or a instanced object who receives the activity as a parameter in the constructor. If this last paragraph sounds like chinese maybe you should consider going back to the OO programming basics. – MLProgrammer-CiM Nov 18 '12 at 23:57
  • You should really be calling/using the superclass' constructor in your testClass. Since you're extending Activity, you should implement your onCreate the same way you did in your MainActivity class. Also, going back to the first issue of not correctly implementing onCreate()..you need to provide a layout resource that the Activity will inflate by calling setContentView() – Jade Byfield Nov 19 '12 at 00:47

3 Answers3

0

Add an OnClickListener to send whatever information to testClass, as shown below (untested):

button.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        Intent secondIntent = new Intent();
        secondIntent.setClassName(myPackageName, "testClass");
        startActivityForResult(secondIntent, REQUEST_CODE);
});

References here and here.

Community
  • 1
  • 1
hd1
  • 33,938
  • 5
  • 80
  • 91
0

Here's my viewpoint: you are receiving a forced close due the fact that you is trying to open user interface methods (such the Toast) with no context.

IMPORTANT: I'm assuming that you already defined both classes in the manifest.xml file!

Before explaining, I'll make a brief: Toast: this class opens a quick message, receiving as main arguments the context, the message and the time-to-show; The context: it is the "environment" where to show. Something like a visual scope, that defines the resources you have. In most cases, you can setup it with setContentView method.

Issue Explanation, in my opinion: The "crash" occurs because you opens the Toast message with no context. An activity is a UI control very similar to a viewpage. If you call a new activity, its very like to call a new page, and so, a new context. In the seccond activity, I haven't see any context. I think that you was assuming that the context is preserved from the first activity, but it ins't because its a new activity.

How to fix:

  • In the seccond class, define a layout view with setContentView, or...
  • Reimplement your seccond class as a Service, and call it through startService, or...
  • Define an AIDL mechams (similar to previous fix, but more sophisticated and complex, as it enables async method calls).

Hope it has helped in some way.

Marcelo
  • 2,075
  • 5
  • 21
  • 38
0

Thank you ALL for the answers!! I was actually able to do what i wanted by using the "stopSelf()" command after i displayed the Toast message. I implemented a service class and when i press the button the testClass.java class gets called and runs the "toast" message then immediately exits by the "stopSelf()" command. I made sure of this by including an "onDestroy()" method which also displayed a simple toast message confirming that the service was stopping :). I usually do stuff like this using threads but it was making the main activity really messy no matter how much formatting i did. So i wanted to have a seperate class i could use.

And to the commenter EfEs, i come from programming in C# language for windows. Android is a new playground for me and im still learning. i think im doing quite well but wasnt sure how to do what i asked. But i figured it out then. And thanks for clearing up that an Activity in android is like a "WindowsForm" in C# where it is completely new GUI for the user. I didnt know that. But thanks to all for helping me with your posts!

Seth
  • 1,769
  • 4
  • 26
  • 39