我正在尝试使用Activity一次保存2个碎片。其中一个片段是,让我们称之为第一个片段,正在显示它包含一个OnClickListener。我希望该监听器在Activity中调用一个方法,该方法包含将第二个片段与另一个片段交换的片段。片段都是以编程方式添加的。我有以下代码似乎给我一个错误。
我是android新手所以代码很邋and,主要用于测试,所以我会尝试解释发生了什么。
这是包含片段的Activity的代码。 活动设法成功添加到Fragments,但是,在按下all_jokes文本视图时会收到错误。
package com.Ayman.jokeit;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class BrowseActivity extends FragmentActivity{
AllJokes afrag;
OneLiners olfrag;
selectFragment sfrag ;
RaceJokes rfrag;
YoMammaJokes yfrag ;
LongJokes lfrag ;
FragmentTransaction stransaction;
FragmentTransaction jtransaction;
FragmentTransaction atransaction;
FragmentManager jmanager;
FragmentManager smanager;
FragmentManager amanager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
afrag = new AllJokes();
olfrag = new OneLiners();
sfrag = new selectFragment();
rfrag = new RaceJokes();
yfrag = new YoMammaJokes();
lfrag = new LongJokes();
setContentView(R.layout.browse);
//Creats object for fragments
//steps for adding fragment to view
smanager = getFragmentManager();
jmanager = getFragmentManager();
stransaction = smanager.beginTransaction();
stransaction.add(R.id.browse_layout, sfrag, "selectFragment");
stransaction.commit();
//steps for adding fragment to view
jtransaction = jmanager.beginTransaction();
jtransaction.add(R.id.browse_layout, olfrag, "onelinerFragment");
jtransaction.commit();
}
public void addAllJokes(){
afrag = new AllJokes();
atransaction = amanager.beginTransaction();
atransaction.replace(R.id.browse_layout, afrag, "afrag");
atransaction.commit();
}
}
这是使用OnClickListener的Fragment的代码。
OnClickListener调用片段中的方法all_jokes(下面的代码),该方法引用活动中的addalljokes方法(上面的代码)。这似乎导致错误。我尝试了多种潜在的解决方案无济于事。请帮助找到解决方案,如果可能的话解释推理。 (除了All_Jokes之外,没有任何OnClickListener可以工作。
package com.Ayman.jokeit;
import com.Ayman.jokeit.AllJokes;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Fragment;
import android.widget.TextView;
public class selectFragment extends Fragment {
BrowseActivity browseActivity = new BrowseActivity();
private TextView all_jokes;
private TextView one_liners;
private TextView yo_mamma_jokes;
private TextView race_jokes;
private TextView long_jokes;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View selectFragmentView = inflater.inflate(R.layout.select_view, container, false);
all_jokes = (TextView) selectFragmentView.findViewById(R.id.all_TextView);
one_liners = (TextView) selectFragmentView.findViewById(R.id.one_Liners_TextView);
yo_mamma_jokes = (TextView) selectFragmentView.findViewById(R.id.yo_Mama_Jokes_TextView);
race_jokes = (TextView) selectFragmentView.findViewById(R.id.race_Jokes_TextView);
long_jokes = (TextView) selectFragmentView.findViewById(R.id.long_Jokes_TextView);
all_jokes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
all_jokes();
}
});
one_liners.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
one_liners();
}
});
yo_mamma_jokes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
yo_mamma_jokes();
}
});
race_jokes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
race_jokes();
}
});
long_jokes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
long_jokes();
}
});
return selectFragmentView;
}
public void all_jokes(){
BrowseActivity browseActivity = (BrowseActivity) getActivity();
browseActivity.addAllJokes();
}
public void one_liners(){
}
public void yo_mamma_jokes(){
}
public void race_jokes(){
}
public void long_jokes(){
}
}
先谢谢你。
编辑*
收到以下错误
01-15 15:01:16.323 6393-6393/com.Ayman.jokeit E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.Ayman.jokeit, PID: 6393
java.lang.NullPointerException
at com.Ayman.jokeit.BrowseActivity.addAllJokes(BrowseActivity.java:61)
at com.Ayman.jokeit.selectFragment.all_jokes(selectFragment.java:90)
at com.Ayman.jokeit.selectFragment$1.onClick(selectFragment.java:39)
at android.view.View.performClick(View.java:4424)
at android.view.View$PerformClick.run(View.java:18383)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4998)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
您可以使用界面在片段和关联活动之间提供通信。您应该将Interface转换为第一个片段的onAttach方法中的活动。 (有关详细信息,请参阅此link)
关联活动应该实现该接口,例如
public interface FragmentSwitcher {
void switchSecondFragment();
}
在该接口的方法回调中(在Activity中)执行替换片段事务。用另一个替换第二个片段。
有任何问题吗?问下面
答案 1 :(得分:0)
atransaction = amanager.beginTransaction();
amanager未实例化。并且你正在使用它...空指针可能是这行atransaction.replace(R.id.browse_layout, afrag, "afrag");
的结果...检查出来