我已经尝试搜索并排查了几个小时,但我仍然无法弄清楚为什么我的应用程序会关闭。
当我点击文本时,它会完美地切换并显示figure603.xml,然后出现强制关闭。
任何人都可以告诉我出了什么问题?
figure603.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/figure"/>
</LinearLayout>
figure603.java
package com.rufflez.swipeytabs;
import android.app.Activity;
import android.os.Bundle;
public class figure603 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.figure603);
}
}
Fragment_6.java
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.view.View.OnClickListener;
import com.actionbarsherlock.app.SherlockFragment;
public class Fragment_6 extends SherlockFragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v =inflater.inflate(R.layout.fragment_6, container, false); // inflate the layout
TextView tv = (TextView) v.findViewById(R.id.textView1); // initialize textview using inflated view object
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(getActivity(), figure603.class);
startActivity(intent);
}
});
return v;
}
}
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rufflez.swipeytabs"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" /><application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme">
<activity
android:name="com.rufflez.swipeytabs.MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="@string/app_name"
android:name=".figure603" ></activity>
</application>
</manifest>
logcat的
12-04 09:00:45.331: D/dalvikvm(2426): GC_FOR_ALLOC freed 48K, 4% free 2999K/3112K, paused 125ms, total 126ms
12-04 09:00:45.351: I/dalvikvm-heap(2426): Grow heap (frag case) to 4.062MB for 1127536-byte allocation
12-04 09:00:45.401: D/dalvikvm(2426): GC_FOR_ALLOC freed <1K, 3% free 4100K/4216K, paused 50ms, total 50ms
12-04 09:00:45.871: V/21st Polling:(2426): clicked
12-04 09:00:46.341: D/(2426): HostConnection::get() New Host Connection established 0xb8973b10, tid 2426
12-04 09:00:46.401: W/EGL_emulation(2426): eglSurfaceAttrib not implemented
12-04 09:00:46.421: D/OpenGLRenderer(2426): Enabling debug mode 0
12-04 09:00:50.921: V/21st Polling:(2426): clicked
12-04 09:00:53.121: D/dalvikvm(2426): GC_FOR_ALLOC freed 101K, 4% free 4492K/4656K, paused 48ms, total 60ms
12-04 09:00:53.191: I/dalvikvm-heap(2426): Grow heap (frag case) to 5.610MB for 1221624-byte allocation
12-04 09:00:53.301: D/dalvikvm(2426): GC_FOR_ALLOC freed 11K, 4% free 5674K/5852K, paused 115ms, total 115ms
12-04 09:00:53.821: W/EGL_emulation(2426): eglSurfaceAttrib not implemented
12-04 09:00:54.661: D/AndroidRuntime(2426): Shutting down VM
12-04 09:00:54.661: W/dalvikvm(2426): threadid=1: thread exiting with uncaught exception (group=0xb1a77b90)
12-04 09:00:54.701: E/AndroidRuntime(2426): FATAL EXCEPTION: main
12-04 09:00:54.701: E/AndroidRuntime(2426): Process: com.rufflez.swipeytabs, PID: 2426
12-04 09:00:54.701: E/AndroidRuntime(2426): java.lang.NullPointerException
12-04 09:00:54.701: E/AndroidRuntime(2426): at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1576)
12-04 09:00:54.701: E/AndroidRuntime(2426): at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1617)
12-04 09:00:54.701: E/AndroidRuntime(2426): at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:481)
12-04 09:00:54.701: E/AndroidRuntime(2426): at com.actionbarsherlock.app.SherlockFragmentActivity.onSaveInstanceState(SherlockFragmentActivity.java:126)
12-04 09:00:54.701: E/AndroidRuntime(2426): at android.app.Activity.performSaveInstanceState(Activity.java:1152)
12-04 09:00:54.701: E/AndroidRuntime(2426): at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1223)
12-04 09:00:54.701: E/AndroidRuntime(2426): at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3156)
12-04 09:00:54.701: E/AndroidRuntime(2426): at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3215)
12-04 09:00:54.701: E/AndroidRuntime(2426): at android.app.ActivityThread.access$1000(ActivityThread.java:135)
12-04 09:00:54.701: E/AndroidRuntime(2426): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1424)
12-04 09:00:54.701: E/AndroidRuntime(2426): at android.os.Handler.dispatchMessage(Handler.java:102)
12-04 09:00:54.701: E/AndroidRuntime(2426): at android.os.Looper.loop(Looper.java:137)
12-04 09:00:54.701: E/AndroidRuntime(2426): at android.app.ActivityThread.main(ActivityThread.java:4998)
12-04 09:00:54.701: E/AndroidRuntime(2426): at java.lang.reflect.Method.invokeNative(Native Method)
12-04 09:00:54.701: E/AndroidRuntime(2426): at java.lang.reflect.Method.invoke(Method.java:515)
12-04 09:00:54.701: E/AndroidRuntime(2426): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-04 09:00:54.701: E/AndroidRuntime(2426): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-04 09:00:54.701: E/AndroidRuntime(2426): at dalvik.system.NativeStart.main(Native Method)
12-04 09:00:56.741: I/Process(2426): Sending signal. PID: 2426 SIG: 9
TabsAdapter.java
package com.rufflez.swipeytabs;
import java.util.ArrayList;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.widget.Toast;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
public class TabsAdapter extends FragmentPagerAdapter implements ActionBar.TabListener , ViewPager.OnPageChangeListener{
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
private final String TAG = "21st Polling:";
static final class TabInfo{
private final Class<?> clss;
private final Bundle args;
TabInfo(Class<?> _class, Bundle _args){
clss = _class;
args = _args;
}
}
public TabsAdapter(SherlockFragmentActivity fa, ViewPager pager) {
super(fa.getSupportFragmentManager());
mContext = fa;
mActionBar = fa.getSupportActionBar();
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args){
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mActionBar.addTab(tab);
notifyDataSetChanged();
}
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
Log.v(TAG, "clicked");
Object tag = tab.getTag();
for (int i = 0; i<mTabs.size(); i++){
if (mTabs.get(i) == tag){
mViewPager.setCurrentItem(i);
}
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(mContext, "You've deselected a tab", Toast.LENGTH_SHORT).show();
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
@Override
public int getCount() {
return mTabs.size();
}
}
MainActivity.java
package com.rufflez.swipeytabs;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
public class MainActivity extends SherlockFragmentActivity {
private ViewPager mViewPager;
private TabsAdapter mTabsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.pager);
setContentView(mViewPager);
final ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setTitle("TASK 05-55-08-200-803");
mTabsAdapter = new TabsAdapter(this, mViewPager);
mTabsAdapter.addTab(bar.newTab().setText("A. General"), Fragment_1.class, null);
mTabsAdapter.addTab(bar.newTab().setText("B. References"), Fragment_2.class, null);
mTabsAdapter.addTab(bar.newTab().setText("C. Tools/Equipment"), Fragment_3.class, null);
mTabsAdapter.addTab(bar.newTab().setText("D. Location Zones"), Fragment_4.class, null);
mTabsAdapter.addTab(bar.newTab().setText("E. Prepare for Procedure"), Fragment_5.class, null);
mTabsAdapter.addTab(bar.newTab().setText("F. Procedure"), Fragment_6.class, null);
}
}