2
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 声明出了点问题,但找不到答案。

任何可以帮助我的人。

4

3 回答 3

4

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

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

}
于 2014-07-01T08:16:29.900 回答
1

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

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;
        }
    }
于 2013-06-29T07:59:44.370 回答
0

mCallInterface未启动。使用前先初始化

于 2013-06-29T07:17:25.863 回答