Calling method of one another activity from another activity
Here is the code snippet,
class1.java
public class1 extends ListActivity {
public class1(){}
publ开发者_JAVA百科ic 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.
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
.
testMethod
needs a return type (such as void):
public void testMethod(){
//your code
}
Got the Solution, wrote the code in "onResume()" of second tab, so no need to call method.
精彩评论