Reputation: 181
I have 2 class in my project, first extend activity and other extend other class. please help how to call the method in main activity from other class not activity? here my code :
Extend Activity Class
public class Isign extends Activity {
private static final String TAG = "Sample::Activity";
public Isign() {
Log.i(TAG, "Instantiated new " + this.getClass());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "onCreate");
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
a = new IsignView(this);
LinearLayout lv = new LinearLayout(this);
lv.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tv = new TextView(this);
tv.setTextSize(20);
tv.setText("RESULT : ");
tv.setLayoutParams(textViewParams);
lv.addView(tv);
lv.addView(a);
setContentView(lv);}
public void update_kom(String d){
Log.i("asd", "asd");
tv.setText("");
tv.setText("RESULT : " + d);
}
Not Extend Activity Class
public class IsignView extends IsignCvViewBase {
public IsignView(Context context) {
super(context);
try {
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "Failed to load cascade. Exception thrown: " + e);
}
}
By the way I Have used this way to call method from main activity :
new Isign().update_kom("bottle");
and i get this error LogCat :
07-05 06:52:22.290: E/AndroidRuntime(2708): FATAL EXCEPTION: Thread-12
07-05 06:52:22.290: E/AndroidRuntime(2708): java.lang.NullPointerException
07-05 06:52:22.290: E/AndroidRuntime(2708): at
org.baharsan.isign.Isign.update_kom(Isign.java:86)
07-05 06:52:22.290: E/AndroidRuntime(2708): at
org.baharsan.isign.view.IsignView.processFrame(IsignView.java:353)
07-05 06:52:22.290: E/AndroidRuntime(2708): at
org.baharsan.isign.cv.IsignCvViewBase.run(IsignCvViewBase.java:101)
07-05 06:52:22.290: E/AndroidRuntime(2708): at
org.baharsan.isign.view.IsignView.run(IsignView.java:557)
07-05 06:52:22.290: E/AndroidRuntime(2708): at
java.lang.Thread.run(Thread.java:1019)
Thank you for your Response, i am sorry if this question repeated while i can't find similiar question like my problem. other problem just show me how to call method in other class from main activity but this question is opposite. "how to call method in main activity from other class?"
thank you so much :)
Upvotes: 0
Views: 4402
Reputation: 30168
While RSenApps' answer works, it's not the best idea to make a View work only inside a certain activity. Instead, declare an interface in your View. Then have the Activity implement that interface and give it to the View, either via the constructor or in a setter. The View can just call the method on the instance of the interface when whatever event you are interested in happens.
Also, note that you can never, ever, call new
on an Activity yourself, Activities are always managed by the framework.
Upvotes: 2
Reputation: 131
call the "WantedActivity" by writeing this in the "CallActivity"
public class CallActivity extends Activity {
//The Activity we wnt to call
private WantedActivity stringname;
stringname is used to deal with the WantedActivity . also you can remove private
Upvotes: 0
Reputation: 4567
Easiest way is to pass reference of the activity to your other class instead of Context. For example
public IsignView(Isign activityReference) {
super((Context) activityReference);
activityReference.update_kom("bottle");
try {
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "Failed to load cascade. Exception thrown: " + e);
}
}
Upvotes: 1