gpt4 book ai didi

java - 来自另一个类的Android接口(interface)类显示空指针异常

转载 作者:太空狗 更新时间:2023-10-29 16:41:18 25 4
gpt4 key购买 nike

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);

}
}

调用时出现空指针异常。

我知道我的 AsycData 声明出了点问题,但找不到答案。

任何可以帮助我的人。

最佳答案

我已经修改了您的代码,现在它可以正常工作并在日志中打印真实值。现在你想做什么你可以用代码来做。如果它适合您,请放弃投票。

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);

}

关于java - 来自另一个类的Android接口(interface)类显示空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17377742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com