1

Here is the code snippet,

class1.java

public class1 extends ListActivity {


   public class1(){}

   public testMethod()
   {
         Toast.makeText(getApplicationContext(),"Inside Method",Toast.LENGTH_SHORT).show();
   }

}

class2.java

public class2 extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

          class1 c = new class1();
          c.testMethod();

    }

}

I want to call "testMethod" from class2, Currently it is giving following error,

08-04 22:59:27.428: ERROR/AndroidRuntime(1224): FATAL EXCEPTION: main
08-04 22:59:27.428: ERROR/AndroidRuntime(1224): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.assistant/com.assistant.AssistantActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.assistant/com.assistant.addNew}: java.lang.NullPointerException

Thanks.

I have two tabs,

Tab1 extends Activity (class2.java) => Contains form to add entry in DB

Tab2 extends ListActivity (class1.java) => Contains list of the added entries

I want to refresh the Tab2 whenever any entry is added through Tab1 and I have added method in class2.java to fill the list with updated DB entries.

Now, I want to call that method from class1.java after the entry is added.

Gaurang
  • 65
  • 2
  • 12

3 Answers3

0

Consider making that method a static method. Although I'm not sure if you are allowed to instantiate an Activity like that, did you remember to add them both to the Android manifest?

Perhaps getApplicationContext() is returning null.

pqn
  • 1,522
  • 3
  • 22
  • 33
  • Yes, I have tried using static method, but in the method I have to use some instance attributes, So can't use static method for this case. Otherwise, it's nice solution. Thanks. – Gaurang Aug 05 '11 at 03:08
0

testMethod needs a return type (such as void):

public void testMethod(){
   //your code
}
Phil
  • 35,852
  • 23
  • 123
  • 164
0

Got the Solution, wrote the code in "onResume()" of second tab, so no need to call method.

Gaurang
  • 65
  • 2
  • 12