【问题标题】:Android sliding tabs, Fragment_Collection_Object ErrorAndroid滑动标签,Fragment_Collection_Object报错
【发布时间】:2014-12-04 13:10:22
【问题描述】:

我正在关注 Android Developers 网站为我的项目添加滑动选项卡到我的 Android 应用程序,我遇到了这个错误并且不理解问题,我知道这与调用正确的 Fragment 布局有关,我我也在按照教程告诉我的做,有没有人有任何信息来解决这个问题,谢谢。

package com.test.finalproject;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Intent;
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.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements View.OnClickListener {

DemoCollectionPagerAdapter mCollectionPagerAdapter;
ViewPager mViewPager;


Button buttonLogin;
Button buttonRegister;
Button buttonTheme;
Button buttonMaps;

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

    final android.app.ActionBar actionBar = getActionBar();

    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);



 // Create a tab listener that is called when the user changes tabs.
    ActionBar.TabListener tabListener = new ActionBar.TabListener() {
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
            // show the given tab
        }

        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
            // hide the given tab
        }

        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
            // probably ignore this event
        }
    };

    // Add 3 tabs, specifying the tab's text and TabListener
    for (int i = 0; i < 3; i++) {
        actionBar.addTab(
                actionBar.newTab()
                        .setText("Tab " + (i + 1))
                        .setTabListener(tabListener));
    }




    buttonLogin = (Button)findViewById(R.id.buttonLogin);
    buttonLogin.setOnClickListener(this);
    buttonRegister = (Button)findViewById(R.id.buttonRegister);
    buttonRegister.setOnClickListener(this);
    buttonTheme = (Button)findViewById(R.id.buttonTheme);
    buttonTheme.setOnClickListener(this);
    buttonMaps = (Button)findViewById(R.id.buttonMaps);
    buttonMaps.setOnClickListener(this);

    mCollectionPagerAdapter =
            new DemoCollectionPagerAdapter(
                    getSupportFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mCollectionPagerAdapter);

}




public class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
    public DemoCollectionPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new DemoObjectFragment();
        Bundle args = new Bundle();
        // Our object is just an integer :-P
        args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        return 100;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "OBJECT " + (position + 1);
    }
}



public static class DemoObjectFragment extends Fragment {
    public static final String ARG_OBJECT = "object";

    @Override
    public View onCreateView(LayoutInflater inflater,
            ViewGroup container, Bundle savedInstanceState) {
        // The last two arguments ensure LayoutParams are inflated
        // properly.
        View rootView = inflater.inflate(
             R.layout.fragment_collection_object, container, false);
        Bundle args = getArguments();
        ((TextView) rootView.findViewById(android.R.id.text1)).setText(
                Integer.toString(args.getInt(ARG_OBJECT)));
        return rootView;
    }
}
public void onCreate1(Bundle savedInstanceState) {


    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setOnPageChangeListener(
            new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    // When swiping between pages, select the
                    // corresponding tab.
                    getActionBar().setSelectedNavigationItem(position);
                }
            });

}




private void buttonLoginClick()
{
    startActivity (new Intent("MainApp"));
}

private void buttonRegisterClick()
{
    startActivity (new Intent("TextToo"));
}

private void buttonThemeClick()
{
    startActivity (new Intent("SettingsTheme"));
}

private void buttonMapsClick()
{
    startActivity (new Intent("GoogleMaps"));
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}



@Override
public void onClick(View v) {
    switch (v.getId())
    {
        case R.id.buttonLogin:
            buttonLoginClick();
            break;

    }
    switch (v.getId())
    {
    case R.id.buttonRegister:
        buttonRegisterClick();
        break;
    }
    switch (v.getId())
    {
    case R.id.buttonTheme:
        buttonThemeClick();
        break;
    }

    switch (v.getId())
    {
    case R.id.buttonMaps:
        buttonMapsClick();
        break;
        }
    }
}

错误在第 126 行。

错误是:描述资源路径位置类型 fragment_collection_object 无法解析或不是字段 MainActivity.java /Login/src/com/test/finalproject 第126行Java问题

【问题讨论】:

  • 您可能希望突出显示导致错误的代码以及您遇到的错误。
  • 我编辑了问题,上面写着 .layout.fragment_collection_object, container, false);
  • 听起来好像缺少 xml,但我想它更复杂。
  • 您可能想使用 sdk 演示而不是尝试尝试使用该教程。他们通常需要背景知识。

标签: android tabs fragment


【解决方案1】:

您遇到的问题是由于 project/res 的 layout 文件夹中缺少 xml 文件

转到http://developer.android.com/training/implementing-navigation/lateral.html
单击“下载示例应用程序”并解压缩。将 EffectiveNavigation\res\layout\fragment_collection_object.xml 添加到您的项目\res\layout。这个,我想会解决问题的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多