我的第一个应用程序 我想在android中使用Swipe + Tiles Navigation。我想在每个片段/或平铺上显示不同的字符串。我使用switch case来使用基于int位置的不同字符串。该应用程序检出没有错误,但在我运行它时继续崩溃。尝试使用if else但现在我被卡住了。帮助将深表感谢。
MainActivity.java
package 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 ERROR SHOT
http://tinypic.com/r/1zmgo55/5
提前致谢
答案 0 :(得分:0)
您正在尝试将ARG_SECTION_NUMBER解析为一个字符串整数。
如果您使用的是eclipse,则可以在Logcat中检查异常日志。 窗口>显示视图>其他> logcat的
修改: - 强> 由于ARG_SECTION_NUMBER,您将收到NumberFormatException。
在DummySectionFragment
的onCreateView()中添加这些行Bundle bundle=getArguments();
int position = bundle.getInt(ARG_SECTION_NUMBER);