从片段到活动通信的数据不起作用

时间:2013-01-18 13:03:09

标签: android android-fragments

我已经有一段时间了,经过几次失败的尝试,我非常接近实现我所追求的目标。我从这个网站挖掘了各种答案,最有帮助的是: -

Passing data between a fragment and its container activity

我已经将所有Harlo的代码片段实现到我的程序中,但是我遇到了一个问题。

当我运行应用程序时,我点击列表项以加载“Core”片段,然后我崩溃了“不幸的应用程序停止了”。

我的片段代码如下,我突出显示了违规行,我还包含了logcat的相关部分。

非常感谢任何帮助。

谢谢

加里

import android.app.Activity;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.view.View.OnClickListener;


public class CoreFragment extends Fragment{

int index;
Button Button1,Button2,Button3;
String Str;
OnDataPass dataPasser;

@Override
public void onAttach(Activity a) {
    super.onAttach(a);
        dataPasser = (OnDataPass) a;
}


public static CoreFragment newInstance(int index) {
    CoreFragment coreFragment = new CoreFragment();
    coreFragment.index = index;
    return coreFragment;
}


public interface OnDataPass {
    public void onDataPass(String data);
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    if (container == null) {
        // We have different layouts, and in one of them this
        // fragment's containing frame doesn't exist. The fragment
        // may still be created from its saved state, but there is
        // no reason to try to create its view hierarchy because it
        // won't be displayed. Note this is not needed -- we could
        // just run the code below, where we would create and return
        // the view hierarchy; it would just never be used.
        return null;
    }
    RelativeLayout mRelativeLayout = (RelativeLayout) inflater.inflate(R.layout.corefragment,
            container, false);

    Button Button1 = (Button) mRelativeLayout.findViewById(R.id.Button1);
    Button1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        Str = "I ";
        passData(Str);
            }

       public void passData(String data) {
            data = Str;
            dataPasser.onDataPass(data);
        }

    });
    return mRelativeLayout;
}
}//end

logcat的

01-18 12:19:18.430: W/dalvikvm(1601): threadid=1: thread exiting with uncaught exception (group=0x40acf228)
01-18 12:19:18.440: E/AndroidRuntime(1601): FATAL EXCEPTION: main
01-18 12:19:18.440: E/AndroidRuntime(1601): java.lang.ClassCastException: com.epsilonitsystems.fragger.MainActivity cannot be cast to com.epsilonitsystems.fragger.CoreFragment$OnDataPass
01-18 12:19:18.440: E/AndroidRuntime(1601):     at com.epsilonitsystems.fragger.CoreFragment.onAttach(CoreFragment.java:25)

你好TNR, 谢谢你,因为我说片段是新的,他们证明了真正的痛苦!我添加了你建议的那条线,现在我崩溃了! LogCat在下面。感谢您的帮助到目前为止,我很乐意将这一个排序!!

01-23 15:30:40.659: W/dalvikvm(3098): threadid=1: thread exiting with uncaught exception (group=0x40acf228)
01-23 15:30:40.749: E/AndroidRuntime(3098): FATAL EXCEPTION: main
01-23 15:30:40.749: E/AndroidRuntime(3098): java.lang.NullPointerException
01-23 15:30:40.749: E/AndroidRuntime(3098):     at com.epsilonitsystems.fragger.MainActivity.onDataPass(MainActivity.java:33)
01-23 15:30:40.749: E/AndroidRuntime(3098):     at com.epsilonitsystems.fragger.CoreFragment$1.passData(CoreFragment.java:69)
01-23 15:30:40.749: E/AndroidRuntime(3098):     at com.epsilonitsystems.fragger.CoreFragment$1.onClick(CoreFragment.java:62)
01-23 15:30:40.749: E/AndroidRuntime(3098):     at android.view.View.performClick(View.java:3549)
01-23 15:30:40.749: E/AndroidRuntime(3098):     at android.view.View$PerformClick.run(View.java:14393)
01-23 15:30:40.749: E/AndroidRuntime(3098):     at android.os.Handler.handleCallback(Handler.java:605)
01-23 15:30:40.749: E/AndroidRuntime(3098):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-23 15:30:40.749: E/AndroidRuntime(3098):     at android.os.Looper.loop(Looper.java:154)
01-23 15:30:40.749: E/AndroidRuntime(3098):     at android.app.ActivityThread.main(ActivityThread.java:4945)
01-23 15:30:40.749: E/AndroidRuntime(3098):     at java.lang.reflect.Method.invokeNative(Native Method)
01-23 15:30:40.749: E/AndroidRuntime(3098):     at java.lang.reflect.Method.invoke(Method.java:511)
01-23 15:30:40.749: E/AndroidRuntime(3098):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-23 15:30:40.749: E/AndroidRuntime(3098):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-23 15:30:40.749: E/AndroidRuntime(3098):     at dalvik.system.NativeStart.main(Native Method)

再次感谢。

2 个答案:

答案 0 :(得分:1)

您正在尝试键入一个活动到接口。所以你得到了ClassCastException。检查您的MainActivity是否正在实施onDataPass界面。如果它实现了onDataPass接口,那么问题就解决了。希望它有效。

更新答案:

更改您的onClick(),如下所示。您已撰写passData()但从未将其命名为将数据传递给onDataPass()这将100%解决您的问题。

@Override
public void onClick(View v) {
 Str = "I ";
 passData(Str);
}

答案 1 :(得分:1)

我不确定你为什么这么复杂。你为什么不这样做:

public void onClick(View v) {
    ((OnDataPass) CoreFragment.this.getActivity()).passData("I");
}

显然,活动必须是:

public class ActivityThatIncludesCoreFragment implements OnDataPass {    
    ....
}