0

我的第一个应用程序我正在尝试在 android 中使用 Swipe + Tiles Navigation。我想在每个片段/或 tile 上显示不同的字符串。我使用 switch case 根据 int position 使用不同的字符串。该应用程序检查时没有错误,但在我运行它时不断崩溃。尝试使用 if else 但现在我被卡住了。帮助将不胜感激。

MainActivity.java

包 com.example.swipetile;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {

int position; // Setting global variables .. trying to solve the issue at the end 
String ARG_SECTION_NUMBER;

SectionsPagerAdapter mSectionsPagerAdapter;

ViewPager mViewPager;

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

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

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

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

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a DummySectionFragment (defined as a static inner class
        // below) with the page number as its lone argument.
        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 5);
        fragment.setArguments(args);
        return fragment;
    }

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

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

/**
 * A dummy fragment representing a section of the app, but that simply
 * displays dummy text.
 */
public static class DummySectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Create a new TextView and set its text to the fragment's section
        // number argument value.
        TextView textView = new TextView(getActivity());
        textView.setGravity(Gravity.CENTER);

        /*
        textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
        return textView;
        */


        // Original Procedure

        /*
         textView.setText(Integer.toString(getArguments().getInt(
         ARG_SECTION_NUMBER))); return textView;
         */


        //  public CharSequence getPageTitle(int position)

        CharSequence  getItem(int position) {

              if (position == 0) {
                textView.setText(getString(R.string.hello_world));
                return textView;

            } else if (position == 1) {
                textView.setText(getString(R.string.hello_world)); 
                return textView;

            } else if (position == 2) {
                textView.setText(getString(R.string.hello_world)); 
                return textView;

            } else if (position == 3) {
                textView.setText(getString(R.string.hello_world)); 
                return textView;

            } else {
                textView.setText(getString(R.string.hello_world)); 
                return textView;
            } 



      // the one that had no errors but closed  
        /*
        int position = Integer.parseInt(ARG_SECTION_NUMBER);
        switch (position)
          { 
           case 1: 
                  textView.setText(getString(R.string.hello_world));
                  return textView;


          case 2: 
                 textView.setText(getString(R.string.hello_world)); 
                 return textView;


          case 3:
                textView.setText(getString(R.string.hello_world));
                return textView;


          default:
                textView.setText(getString(R.string.hello_world));
                return textView; 

          }
         */

        /*textView.setText(getString(R.string.hello_world));
        return textView;
        */
    }
}

}

我尝试注释掉顶部的位置 int 和字符串 ARG_SECTION_NUMBER; 然后尝试使用switch语句..相同的结果..没有错误..应用程序在我运行时就死了。

LOGCAT 错误提示

http://tinypic.com/r/1zmgo55/5

提前致谢

4

1 回答 1

0

您正在尝试将 ARG_SECTION_NUMBER 解析为一个整数,它是一个字符串。

如果您使用的是 eclipse,您可以在 Logcat 中检查异常日志。窗口 > 显示视图 > 其他 > Logcat

编辑:- 由于 ARG_SECTION_NUMBER,您会收到 NumberFormatException。

在 DummySectionFragment 的 onCreateView() 中添加这些行

Bundle bundle=getArguments();
int position = bundle.getInt(ARG_SECTION_NUMBER);
于 2013-05-05T06:04:12.197 回答