我在這裏遇到了從片段內的按鈕開始活動的問題。 這裏是我的代碼:從片段開始活動會導致NullPointerException
ViewPagerAdapter.java
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class ViewPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public int getCount() {
return this.fragments.size();
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
}
ViewPagerActivity.java
import java.util.List;
import java.util.Vector;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import com.package.here.Class1Overview;
import com.package.here.Class1Overview;
import com.package.here.Class1Overview;
public class ViewPagerActivity extends FragmentActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.viewpager);
ViewPager pager = (ViewPager)super.findViewById(R.id.my_viewpager);
pager.setAdapter(initialisePaging());
}
private ViewPagerAdapter initialisePaging() {
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, Class1Overview.class.getName()));
fragments.add(Fragment.instantiate(this, Class2Overview.class.getName()));
fragments.add(Fragment.instantiate(this, Class3Overview.class.getName()));
return new ViewPagerAdapter(super.getSupportFragmentManager(), fragments);
}
}
Class1Overview.java(這裏是按鈕)
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import com.package.here.R;
public class FuelOverview extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
View v = (LinearLayout)inflater.inflate(R.layout.class_one_layout, container, false);
View AddBtn = v.findViewById(R.id.btn_add);
((Button) AddBtn).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(getActivity(), ActivityToStart.class);
startActivity(intent);
}
});
return v;
}
}
這是我logcat的:
01-05 19:39:00.913: E/AndroidRuntime(10557): Uncaught handler: thread main exiting due to uncaught exception
01-05 19:39:01.034: E/AndroidRuntime(10557): java.lang.RuntimeException: Unable to pause activity {com.package.here/com.package.here.ViewPagerActivity}: java.lang.NullPointerException
01-05 19:39:01.034: E/AndroidRuntime(10557): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3162)
01-05 19:39:01.034: E/AndroidRuntime(10557): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3119)
01-05 19:39:01.034: E/AndroidRuntime(10557): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3102)
01-05 19:39:01.034: E/AndroidRuntime(10557): at android.app.ActivityThread.access$2400(ActivityThread.java:119)
01-05 19:39:01.034: E/AndroidRuntime(10557): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1870)
01-05 19:39:01.034: E/AndroidRuntime(10557): at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 19:39:01.034: E/AndroidRuntime(10557): at android.os.Looper.loop(Looper.java:123)
01-05 19:39:01.034: E/AndroidRuntime(10557): at android.app.ActivityThread.main(ActivityThread.java:4363)
01-05 19:39:01.034: E/AndroidRuntime(10557): at java.lang.reflect.Method.invokeNative(Native Method)
01-05 19:39:01.034: E/AndroidRuntime(10557): at java.lang.reflect.Method.invoke(Method.java:521)
01-05 19:39:01.034: E/AndroidRuntime(10557): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-05 19:39:01.034: E/AndroidRuntime(10557): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-05 19:39:01.034: E/AndroidRuntime(10557): at dalvik.system.NativeStart.main(Native Method)
01-05 19:39:01.034: E/AndroidRuntime(10557): Caused by: java.lang.NullPointerException
01-05 19:39:01.034: E/AndroidRuntime(10557): at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1576)
01-05 19:39:01.034: E/AndroidRuntime(10557): at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1617)
01-05 19:39:01.034: E/AndroidRuntime(10557): at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:481)
01-05 19:39:01.034: E/AndroidRuntime(10557): at android.app.Activity.performSaveInstanceState(Activity.java:1022)
01-05 19:39:01.034: E/AndroidRuntime(10557): at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1180)
01-05 19:39:01.034: E/AndroidRuntime(10557): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3144)
01-05 19:39:01.034: E/AndroidRuntime(10557): ... 12 more
01-05 19:39:01.423: I/dalvikvm(10557): threadid=7: reacting to signal 3
ViewPager本身是工作的罰款。這似乎是暫停FragmentActivity的錯誤,但我無法弄清楚。我發現的唯一的事情是這個職位上谷歌http://code.google.com/p/android/issues/detail?id=19917
我還通過谷歌的示例代碼必須確保我主要是使用相同的技術......
編輯:以確保ClickListener的作品,我試着onClick方法中的吐司 - 完美運作。
只是想出了以下內容: FragmentActivity崩潰時: - 按home鍵 - 打開設備 的方向 - 開始Avtivity - 調用完成工作正常 因此,目前這適用於我: 覆蓋onSaveInstanceState方法,而無需調用超級方法,你很好。除了事實,實例狀態沒有保存:D所以這不是一個真正的解決方案,但可以確定問題。 – Juke 2012-01-05 21:07:35
我剛剛遇到了同樣的問題 - 對於我來說,問題是由於我在項目中添加了一個空白布局的新片段而引起的。看起來在代碼的某個地方,如果在onSaveInstanceState期間沒有任何內容保存到bundle中,它必須導致一個空指針異常。在我的佈局中添加一些視圖修復了這個問題(花了幾個小時將我的頭撞到了牆上)。我正在使用Android支持庫v4修訂版10. – 2012-09-05 04:26:09
@Juke是否有可能使用碎片來獲得TabViewActivity的相同行爲,我想在選擇任何選項卡的碎片區域中打開一個新的活動? – CoDe 2012-10-06 07:07:53