Mehmet Ethem SULAN :: Android :: How to call An Android Activity class from non-activity class

nullpointerexception non activity class hatası ile karşılaştığınızda aşağıdaki şekıilde çözebilirsiniz.
Android içinde normal bir Java(Non-Activity and Non-View) sınıfından herhangi bir Activity çağırmak istediğimizde aşağıdaki gibi iki şekilde çağırabiliriz.

1. Context göndereceğimiz sınıfın yapıcısında this ile göndermektir.

MyMainActivity.java

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

public class MyMainActivity extends Activity {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        MyTestClass myTestClass=new MyTestClass(this);
        myTestClass.startSecondActivity();
    }
}

MyTestClass.java

import android.content.Context;
import android.content.Intent;

public class MyTestClass {

	Context context;

	public MyTestClass(Context context) {
		super();
		this.context = context;
	}
	public void startSecondActivity()
	{
		Intent intent=new Intent(context, SecondActivity.class);
		context.startActivity(intent);
	}
	
}

SecondActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class SecondActivity extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Toast.makeText(getApplicationContext(), "Second activity", Toast.LENGTH_LONG).show();
	}

}

2. yol ise intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); şeklinde SecondActivity i yeni bir task ile başlatmaktır. Bu şekilde yeni task ile başlattığımız da hem MyTestClass myTestClass=new MyTestClass(this); hem de MyTestClass myTestClass=new MyTestClass(getApplicationContext()); start edebiliyoruz.

MyMainActivity.java

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

public class MyMainActivity extends Activity {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        MyTestClass myTestClass=new MyTestClass(getApplicationContext());
        myTestClass.startSecondActivity();
    }
}

MyTestClass.java

import android.content.Context;
import android.content.Intent;

public class MyTestClass {

	Context context;

	public MyTestClass(Context context) {
		super();
		this.context = context;
	}
	public void startSecondActivity()
	{
		Intent intent=new Intent(context, SecondActivity.class);
//Yeni task ile başlattık.
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
	}
	
}

SecondActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class SecondActivity extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Toast.makeText(getApplicationContext(), "Second activity", Toast.LENGTH_LONG).show();
	}

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.call"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="4" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".MyMainActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="SecondActivity"></activity>
    </application>

</manifest>

Download CallActivity.zip source code

Comments

There is one comment for this post.

  1. The infected by bugs Blogsite on Haziran 18, 2012 6:26 pm

    Hi there, There’s no doubt that your web site might be having web browser compatibility issues. Whenever I look at your web site in Safari, it looks fine however, if opening in I.E., it has some overlapping issues. I just wanted to give you a quick heads up! Apart from that, wonderful site!

Write a Comment

Let me know what you think?