-2

I know here exists a lot of topics with this header but I am new on android developing and tried many things but don't reach a solution.

I have a MainClass with a TextView. The MainClass calls a Method in a second Class ("Check"). The second Class should only create an intent with a string which i print out in textview of the mainClass. (Later it should be more complex but first I have to get that).

Here my Code of MainClass:

    package com.projektarbeit;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.TextView;

    public class MainActivity extends Activity {

        public static TextView output = null;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            //Variable initialisieren
            output = (TextView) findViewById(R.id.output);
            //App gestartet ausgeben
            output.setText("App started");

            Check check1 = new Check();
            check1.doCheck();

            Intent intent = getIntent();
            String myString = intent.getStringExtra("Test");
            output.setText(myString);
        }
    }

Here my Code of CheckClass:

    package com.projektarbeit;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;

    public class Check extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_check);
        }

        public void doCheck (){

            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            intent.putExtra("Test", "This is a test");
            startActivity(intent);

        }

    }

Am I totally wrong or what is my mistake?

3 Answers3

4

Why you are using Intent, What exactly you want to do?

If you really want to check only string then following Piece of code will work:

 public String doCheck (){ 

   return "This is a test";

 } 

And to set this text in your textView you just have to call this method like this:

    output.setText(check1.doCheck());
Anjali
  • 1
  • 1
  • 13
  • 20
  • because the method doCheck is in an other acivity than the TextView. And I want to handle the result in the view of the MainActivity! – HobbyCoder Nov 11 '15 at 13:30
0
Check check1 = new Check();  
check1.doCheck();

You should not instantiate an Activity.
To start Check from your MainActivity you should do something like this:

Intent intent = new Intent(MainActivity.this, Check.class);
startActivity(intent);

Also, as Md. Dinar has suggested, don't forget to declare the Activity on your AndroidManifest file.

Community
  • 1
  • 1
rlukas
  • 16
  • 2
-1

Have you added your activities in android manifest? If yes Try

Intent intent = new Intent(Check.this, MainActivity.class);

instead of

Intent intent = new Intent(getApplicationContext(), MainActivity.class);