我試圖使用活動一次容納2個片段。其中一個片段是,我們將其稱爲第一個片段,正在顯示的片段包含OnClickListener。我希望這個監聽器在Activity中調用一個包含碎片的方法,這個碎片會將第二片碎片與另一碎片交換。碎片全部以編程方式添加。我有以下代碼似乎給我一個錯誤。如何在片段中使用交換片段
我是新來的android,所以代碼是馬虎,主要是爲了測試,所以我會嘗試解釋發生了什麼。
這是包含片段的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的片段的代碼。
OnClickListener在引用活動內的addalljokes方法(上面的代碼)的片段(以下代碼)內調用方法all_jokes。這似乎導致錯誤。我已經嘗試了多種可能的解決方案無濟於事。請幫助找到解決方案,如果可能的話解釋推理。 (該OnClickListeners都不意味着除了All_Jokes一個工作。
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)
什麼是錯誤? – Tobrun
nullpointexception –
@AymanElmubark發佈stacktrace並使用接口作爲活動的回調 – Raghunandan