Reputation: 1823
public class MainActivity extends Activity Implements ICallInterface {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onTaskCompleted(Boolean result) {
if (result) {
Log.d(TAG, "1111111111111 11111111111 True condition ");
} else {
Log.d(TAG, "22222222222222222222 False condition ");
}
}
public interface ICallInterface {
void onTaskCompleted(Boolean result);
}
public class AsycData extends AsyncTask<String, Void, Void> {
ICallInterface mCallInterface;
// AsyncTask Data
// AsyncTask onPost
@Override
protected void onPostExecute(Void result) {
// call interface, set true value & call first activity.
mCallInterface.onTaskCompleted(true);
}
}
Getting null pointer exception at time of call.
I know something went wrong with my AsycData declaration but couldn't find ans.
Any who could help me .
Upvotes: 2
Views: 4318
Reputation: 56
I have modified your code, now it working and in log printing true value. Now what you want to do you can do with code. if it's work for you then please give up votes.
public class MainActivity extends Activity implements ICallInterface {
SecondClass second;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// second.execute();
second = new SecondClass(this);
second.execute();
}
/*
*
* @Override public void onTaskCompleted(Boolean result) { // TODO
* Auto-generated method stub if (result) {
* Log.d("tag","1111111111111 11111111111 True condition ");
* System.out.println(result); // System.out.println(str);
* Toast.makeText(getApplicationContext(), " "+result,
* Toast.LENGTH_LONG).show();
*
*
* } else { Log.d("tag","22222222222222222222 False condition "); }
*
* }
*/
@Override
public void onTaskCompleted(String result) {
// TODO Auto-generated method stub
System.out.println(result);
// System.out.println(str);
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG)
.show();
}
}
public class SecondClass extends AsyncTask<String, String, String> {
ICallInterface mCallInterface;
public SecondClass(ICallInterface mcaCallInterface) {
// TODO Auto-generated constructor stub
this.mCallInterface = mcaCallInterface;
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String str = "Hello";
return str;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
mCallInterface.onTaskCompleted(result);
super.onPostExecute(result);
}
}
public interface ICallInterface {
void onTaskCompleted(String result);
}
Upvotes: 4
Reputation: 3869
I have modified your code, now it woking and in log printing true value. Now what you want to do you can do with code. if it's work for you then please give up votes.
public class MainActivity extends Activity implements ICallInterface {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new AsycData(MainActivity.this).execute();
}
@Override
public void onTaskCompleted(Boolean result) {
if (result) {
Log.d("TAG", "1111111111111 11111111111 True condition ");
} else {
Log.d("TAG", "22222222222222222222 False condition ");
}
}
}
interface ICallInterface {
void onTaskCompleted(Boolean result);
}
class AsycData extends AsyncTask<String, Void, Void> {
Activity activity;
public AsycData(MainActivity activity) {
this.activity = activity;
}
@Override
protected void onPostExecute(Void result) {
((ICallInterface) activity).onTaskCompleted(true);
}
@Override
protected Void doInBackground(String... arg0) {
return null;
}
}
Upvotes: 1