gpt4 book ai didi

java - SwipeyTabsSampleActivity 如何没有生成编译错误?

转载 作者:行者123 更新时间:2023-12-01 15:02:36 25 4
gpt4 key购买 nike

我试图从 android-playground project 开发的 SwipeyTabsSampleActivity 中制作另一个示例

这是示例代码,我的问题是为什么我在方法 getTab->view.setOnClickListener 中引用两个变量“mViewPager”和“position”时没有收到编译错误

这是完整的示例源代码。我首先尝试了示例代码,它编译并运行良好,但我不知道当我尝试根据我的需要自定义示例时。一切都很顺利(编译和运行),除了当我打算设置 onClickListener 并引用这两个变量时,我收到以下两个编译错误1-无法对非静态字段mViewPager进行静态引用2- 不能引用在不同方法中定义的内部类中的非最终变量位置

我认为这两个错误应该出现在示例代码中,而不仅仅是我的代码,但我不知道为什么?

/*
* Copyright 2011 Peter Kuterna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.peterkuterna.android.apps.swipeytabs;

import android.content.Context;
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.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class SwipeyTabsSampleActivity extends FragmentActivity {

private static final String [] TITLES = {
"CATEGORIES",
"FEATURED",
"TOP PAID",
"TOP FREE",
"TOP GROSSING",
"TOP NEW PAID",
"TOP NEW FREE",
"TRENDING",
};

private SwipeyTabs mTabs;
private ViewPager mViewPager;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_swipeytab);

mViewPager = (ViewPager) findViewById(R.id.viewpager);
mTabs = (SwipeyTabs) findViewById(R.id.swipeytabs);

SwipeyTabsPagerAdapter adapter = new SwipeyTabsPagerAdapter(this,
getSupportFragmentManager());
mViewPager.setAdapter(adapter);
mTabs.setAdapter(adapter);
mViewPager.setOnPageChangeListener(mTabs);
mViewPager.setCurrentItem(0);
}

private class SwipeyTabsPagerAdapter extends FragmentPagerAdapter implements
SwipeyTabsAdapter {

private final Context mContext;

public SwipeyTabsPagerAdapter(Context context, FragmentManager fm) {
super(fm);

this.mContext = context;
}

@Override
public Fragment getItem(int position) {
return SwipeyTabFragment.newInstance(TITLES[position]);
}

@Override
public int getCount() {
return TITLES.length;
}

public TextView getTab(final int position, SwipeyTabs root) {
TextView view = (TextView) LayoutInflater.from(mContext).inflate(
R.layout.swipey_tab_indicator, root, false);
view.setText(TITLES[position]);
view.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mViewPager.setCurrentItem(position);
}
});

return view;
}

}

}

最佳答案

getTab()SwipeyTabsSampleActivity 的非静态内部类的实例方法,因此它具有对其外部类实例的隐式引用,并且可以访问该实例的所有字段和方法,从而到达mViewPagerOnclickListenerSwipeyTabsSampleActivity 内部类的匿名内部类,因此也可以访问其两个外部类实例的所有字段和方法。

如果内部类是静态的,它将无法编译。如果 mViewPagergetTab() 方法的非最终局部变量,它也不会编译。

关于java - SwipeyTabsSampleActivity 如何没有生成编译错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13430744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com