0

I have a problem using a method from another class that Extends from an Activity. I have a code that needs to repeat in every single Activity, So i created a class with this code and I get an error when I tryied to use the method at other class For exmple:

First Class:

public class first extends Activity
    public void onCreate(){
        second s = new second();
        s.myMethod();
    }

Second Class:

public class second extends Activity
    public void myMethod(){}

This way I get NullPointerExption.

First Class:

public class first extends Activity
    public void onCreate(){
        Context context = getApplicationContext();
        second s = ((second)context);
        s.myMethod(context );
    }

Second Class:

public class second extends Activity
    Context context ;
    public void myMethod(Context context){
        this.context = context;
    }

This way I get InvocationTargetException.

This is my code:

MainActivity - First Class:

public class MainActivity extends Activity {

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


    public void ClickMe(View  V) {
        Toast.makeText(this, "ClickMe", Toast.LENGTH_SHORT).show();
        Context context = getApplicationContext();
        menuSetup myMenu = ((menuSetup)context);
        myMenu.SetMenuListView();

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

    public boolean onOptionsItemSelected(MenuItem item) {// Action Bar buttons
        switch (item.getItemId()) {
        case R.id.action_settings:
            SlidingDrawer myS = (SlidingDrawer) findViewById(R.id.slidingDrawer1);
            myS.animateToggle();
            break;
        }
        return super.onOptionsItemSelected(item);
    }
}

menulList - Second Class:

public class menuSetup extends Activity {
    MenuAdapter adapter;
    ListView myList;
    int myPosiition;

    public void SetMenuListView() {
        myList = (ListView) findViewById(R.id.listView1);
        adapter = new MenuAdapter(this);
        myList.setAdapter(adapter);

        myList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v,  int position, long id) {
                myPosiition = position;
                StartActivityMethod();

            }
        });

    }

    private void StartActivityMethod() {
        switch (myPosiition) {
        case 0:
            Intent item2 = new Intent(this, Item2.class);
            startActivity(item2);
            finish();
            break;
        case 1:
            Intent item3 = new Intent(this, Item3.class);
            startActivity(item3);
            finish();
            break;
        case 2:
            Toast.makeText(this, "Missing Page", Toast.LENGTH_SHORT).show();
            break;

        }
    }

}
Shachar87
  • 149
  • 1
  • 1
  • 11

1 Answers1

2

You should not create an instance of a Activity class. Its not a normal class. It has a lifecycle of it own. You only declared Activity in manifest.

Instead make a Uitlity class and you can pass the Context to the constructor of Utility class and use it there.

Quoting Raghav Sood

By treating an Activity as a normal Java class, you end up with a null context. As most methods in an Activity are called on its Context, you will get a null pointer exception, which is why your app crashes.

Can i Create the object of a activity in other class?

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • You could have also quoted hundreds of comments and many answers posted by me (if you can find them) - I've lost count of how many there are. I really wish the Android dev tools would flag an error at compile time whenever anyone tries to create an `Activity` using `new` and also any time they try to add a `public static` field or variable to an `Activity`. – Squonk May 07 '14 at 15:25
  • @Squonk right but i bookmarked Raghav's answer. I guess i should have posted it as a comment. Next time i find a few more and post all of them as links – Raghunandan May 07 '14 at 15:26
  • No it's not necessary as a comment and is a valid answer. I upvoted it. – Squonk May 07 '14 at 15:28
  • I edited my question to my full code. I need to extend from activity to do some actions. U didnt really understand what did you said. – Shachar87 May 07 '14 at 15:31
  • @Squonk i understand your point i too have seen a lot of similar posts. Well i thought i would be duplicating since i could have posted a link and voted to close the question as duplicate. May be a lint warning would help. – Raghunandan May 07 '14 at 15:31
  • You right, I check lots of post like this, and tryied diffrent ways. But nothing worked for me. So I opened a new post. I stack with this problem a few days. – Shachar87 May 07 '14 at 18:13
  • @Shachar87 you can't instantiate activity class. So what is that you want. Activity has a ui. So your design is wrong – Raghunandan May 07 '14 at 18:14
  • What do you suggest I do? What I trying to do is SlidingDrawer with ListView. this SlidingDrawer need to be on all Activitys. – Shachar87 May 07 '14 at 18:34
  • @Shachar87 use a drawer in one Activity. extend all other activitiees from that activity. – Raghunandan May 08 '14 at 04:04
  • I've been a fool, How I didn't realize it was so EASY. Just exdens from menuSetup and activated the method with super.SetMenuListView(); – Shachar87 May 10 '14 at 15:04