来自另一个类的Android Interface类显示空指针异常

时间:2013-06-29 07:16:11

标签: java android interface callback android-asynctask

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

任何可以帮助我的人。

3 个答案:

答案 0 :(得分: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);

}

答案 1 :(得分: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;
        }
    }

答案 2 :(得分:0)

mCallInterface未启动。在使用

之前初始化它