Android,旋转屏幕时崩溃活动

时间:2014-04-12 11:02:26

标签: android fragment screen-rotation

请知道为什么我的应用程序在这个片段中rotating screen时崩溃了?当我有空白fragments时,它工作得很完美,但是当我用fragment填充我的listview时,它就会崩溃,但只有当应用加载了fullfill fragment时才会崩溃。

这是我的片段

public class PoznamkaFragment extends Fragment {

private DatabaseOp mDbHelper;
String username;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
    View v = inflater.inflate(R.layout.poznamka_frag, container, false);

    ListView listview = (ListView) v.findViewById(R.id.listPoznamka);

    mDbHelper = new DatabaseOp(getActivity());
    mDbHelper.open();

    showUserSettings(); 
    Cursor notesCursor = mDbHelper.fetchAllNotes(username);

    final PoznamkyCursorAdapter adapter = new PoznamkyCursorAdapter(getActivity(), notesCursor);
    listview.setAdapter(adapter);

    listview.setOnItemClickListener(new OnItemClickListener() 
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int stlpec, long arg3) 
        {
            // TODO Auto-generated method stub
            Cursor cur = (Cursor) adapter.getItem(stlpec);
            String odosli = cur.getString(cur.getColumnIndex("_id"));

            Intent intent = new Intent(getActivity(),PoznamkaShower.class);
            intent.putExtra(PoznamkaShower.ODOSLI, odosli);

            startActivity(intent);
            //Toast.makeText(getActivity(), odosli, Toast.LENGTH_SHORT).show();

        }
    });

    /*for (notesCursor.moveToFirst(); !notesCursor.isAfterLast(); notesCursor.moveToNext()) 
    {
        // do what you need with the cursor here
        Log.d("Poznamky", notesCursor.getString(notesCursor.getColumnIndex("nazov")));
    }*/

    return v;
}

@Override
public void onResume() 
{
    super.onResume();

    ListView listview = (ListView) getActivity().findViewById(R.id.listPoznamka);

    mDbHelper = new DatabaseOp(getActivity());
    mDbHelper.open();

    Cursor notesCursor = mDbHelper.fetchAllNotes(username);
    final PoznamkyCursorAdapter adapter = new PoznamkyCursorAdapter(getActivity(), notesCursor);
    listview.setAdapter(adapter);
}

@Override
public void onPause() 
{
    super.onPause();
    mDbHelper.close();
}

public static PoznamkaFragment newInstance() {

    PoznamkaFragment f = new PoznamkaFragment();
    return f;
}

private void showUserSettings() 
{
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());

    username = sharedPrefs.getString("prefUsername", "NULL");
}

}

这是MainActivity

public class MainActivity extends FragmentActivity {

public static final String PREZIVKA = "prezivka";

MyPagerAdapter mMyPagerAdapter;

ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent i = getIntent();
    String prezivka = getResources().getString(R.string.logged)+" "+i.getStringExtra(PREZIVKA);

    if (prezivka.length()>0)
        Toast.makeText (this, prezivka, Toast.LENGTH_SHORT).show();

    mMyPagerAdapter = new MyPagerAdapter(
            getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mMyPagerAdapter);

}

@Override
public void onResume ()
{
    super.onResume();

    mMyPagerAdapter = new MyPagerAdapter(
            getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mMyPagerAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class MyPagerAdapter extends FragmentPagerAdapter {

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {

        switch(position){
             case 0:
                 return UlohaFragment.newInstance();
             case 1:
                 return PredmetFragment.newInstance();
             case 2:
                 return PoznamkaFragment.newInstance();
             case 3:
                 return SviatokFragment.newInstance();
             default: 
                 return UlohaFragment.newInstance();
        }
    }

    @Override
    public int getCount() {
        // Show 4 total pages.
        return 4;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:
            return getString(R.string.title_section1).toUpperCase(l);
        case 1:
            return getString(R.string.title_section2).toUpperCase(l);
        case 2:
            return getString(R.string.title_section3).toUpperCase(l);
        case 3:
            return getString(R.string.title_section4).toUpperCase(l);
        }
        return null;
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.action_pridat:
            openPridaj();
            return true;
        case R.id.action_hladat:
            openHladaj();
            return true;
        case R.id.action_settings:
            openSettings();
            return true;
        case R.id.action_app:
            openApp();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

public void openPridaj()
{
    Intent intent = new Intent(this, PridajActivity.class);
    startActivity(intent);  
}

public void openHladaj()
{
    Intent intent = new Intent(this, HladajActivity.class);
    startActivity(intent);  
}

public void openSettings()
{
    Intent intent = new Intent(this, NastaveniaActivity.class);
    startActivity(intent);  
}

public void openApp()
{
    Intent intent = new Intent(this, AppActivity.class);
    startActivity(intent);
}
}

这是LogCat

04-12 12:55:09.190: E/AndroidRuntime(8569): FATAL EXCEPTION: main
04-12 12:55:09.190: E/AndroidRuntime(8569): java.lang.RuntimeException: Unable to resume activity {sk.tomino.login/sk.tomino.login.MainActivity}: java.lang.NullPointerException
04-12 12:55:09.190: E/AndroidRuntime(8569):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3052)
04-12 12:55:09.190: E/AndroidRuntime(8569):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3093)
04-12 12:55:09.190: E/AndroidRuntime(8569):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
04-12 12:55:09.190: E/AndroidRuntime(8569):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4086)
04-12 12:55:09.190: E/AndroidRuntime(8569):     at android.app.ActivityThread.access$700(ActivityThread.java:151)
04-12 12:55:09.190: E/AndroidRuntime(8569):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
04-12 12:55:09.190: E/AndroidRuntime(8569):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-12 12:55:09.190: E/AndroidRuntime(8569):     at android.os.Looper.loop(Looper.java:155)
04-12 12:55:09.190: E/AndroidRuntime(8569):     at android.app.ActivityThread.main(ActivityThread.java:5520)
04-12 12:55:09.190: E/AndroidRuntime(8569):     at java.lang.reflect.Method.invokeNative(Native Method)
04-12 12:55:09.190: E/AndroidRuntime(8569):     at java.lang.reflect.Method.invoke(Method.java:511)
04-12 12:55:09.190: E/AndroidRuntime(8569):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
04-12 12:55:09.190: E/AndroidRuntime(8569):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
04-12 12:55:09.190: E/AndroidRuntime(8569):     at dalvik.system.NativeStart.main(Native Method)
04-12 12:55:09.190: E/AndroidRuntime(8569): Caused by: java.lang.NullPointerException
04-12 12:55:09.190: E/AndroidRuntime(8569):     at sk.tomino.login.PoznamkaFragment.onResume(PoznamkaFragment.java:80)
04-12 12:55:09.190: E/AndroidRuntime(8569):     at android.support.v4.app.Fragment.performResume(Fragment.java:1543)
04-12 12:55:09.190: E/AndroidRuntime(8569):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:963)
04-12 12:55:09.190: E/AndroidRuntime(8569):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
04-12 12:55:09.190: E/AndroidRuntime(8569):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
04-12 12:55:09.190: E/AndroidRuntime(8569):     at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:1894)
04-12 12:55:09.190: E/AndroidRuntime(8569):     at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:466)
04-12 12:55:09.190: E/AndroidRuntime(8569):     at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:455)
04-12 12:55:09.190: E/AndroidRuntime(8569):     at android.app.Activity.performResume(Activity.java:5161)
04-12 12:55:09.190: E/AndroidRuntime(8569):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3035)
04-12 12:55:09.190: E/AndroidRuntime(8569):     ... 13 more

2 个答案:

答案 0 :(得分:2)

我认为你在 PoznamkaFragment 的第80行获得NullPointerException,请参阅:

  

PoznamkaFragment.onResume(PoznamkaFragment.java:80)

不确定但要避免这种情况,请尝试使用getView()方法而不是getActivity():

ListView listview = (ListView) getView().findViewById(R.id.listPoznamka);  

您还可以按如下方式为ListView创建全局变量:

// just below the fragment declaration "public class fragment extends..."
ListView listview;

// then in onResume  
listview = (ListView) getView().findViewById(R.id.listPoznamka);

// and in onCreateView do the same
listview = (ListView) v.findViewById(R.id.listPoznamka);

答案 1 :(得分:-1)

android:configChanges="keyboard|orientation" 

在应用程序代码中尝试清单:

<application
android:allowBackup="true"
android:configChanges="keyboard|orientation"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

由于我的英语,我没有详细说明。这是因为如果您想要在方向改变时不会发生任何变化。