传递变量

时间:2015-06-14 19:32:57

标签: java android

我正在关注MVP设计模式。我从View中调用Presenter中的方法,该方法为网络请求执行异步任务。到目前为止我已经调试过,它成功地将JSON作为String返回,但是当我想将它传递给从Presenter到View的方法时,我得到了这个错误:

尝试调用虚拟方法' android.view.View android.view.Window.findViewById(int)'在空对象引用上

这是我的代码:

查看:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mediated);
    TextView welcome_txt = (TextView) findViewById(R.id.welcome_txtview);
    MediatedPresenter mediatedPresenter = new MediatedPresenter();
    mediatedPresenter.createConnection();
}

public void getResult(String result)
{
    TextView welcome_txt = (TextView) findViewById(R.id.welcome_txtview);
    welcome_txt.setText(result);
}

主讲人:

public void createConnection() {

    new NetworkRequest(new ResultFromAsync() {
        @Override
        public void taskCompleted(String result) {

                MediatedActivity mediatedActivity = new MediatedActivity();
                mediatedActivity.getResult(result);
        }
    }).execute();

}

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="uk.co.bbc.avtestharnesssmp.MediatedActivity"
android:background="#3498db">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Welcome"
    android:id="@+id/welcome_txtview"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:textColor="#80ffffff" />
</RelativeLayout>

BTW:我在这一行得到了错误&#34; mediatedActivity.getResult(result);&#34;只是为了澄清我试图将taskCompleted的结果发送到View中的getResult方法。

有谁知道我做错了什么?

0 个答案:

没有答案
相关问题