寻找MyPagerActivity和MyFragment类
出现错误无法找到GetResources(),并且不允许使用MyFragment的静态。代码早先运行得非常好,当我添加Floating Action Button类时,突然出现这些错误。我是Android编程的新手。请帮助。
package demo.demo2;
import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import demo.demo2.FloatingActionButton;
import demo.demo2.FloatingActionsMenu;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import tabs.SlidingTabLayout;
public class Main3Activity extends AppCompatActivity {
private ViewPager mPager;
private Toolbar toolbar;
Activity activity;
private SlidingTabLayout mTabs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Toolbar toolbar= (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationDrawerFragment drawerFragment=(NavigationDrawerFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
mTabs.setViewPager(mPager);
findViewById(R.id.pink_icon).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Main3Activity.this, "Clicked pink Floating Action Button", Toast.LENGTH_SHORT).show();
}
});
FloatingActionButton button = (FloatingActionButton) findViewById(R.id.setter);
button.setSize(FloatingActionButton.SIZE_MINI);
button.setColorNormalResId(R.color.pink);
button.setColorPressedResId(R.color.pink_pressed);
button.setIcon(R.drawable.ic_fab_star);
button.setStrokeVisible(false);
final View actionB = findViewById(R.id.action_b);
FloatingActionButton actionC = new FloatingActionButton(getBaseContext());
actionC.setTitle("Hide/Show Action above");
actionC.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
actionB.setVisibility(actionB.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
}
});
final FloatingActionsMenu menuMultipleActions = (FloatingActionsMenu) findViewById(R.id.multiple_actions);
menuMultipleActions.addButton(actionC);
final FloatingActionButton removeAction = (FloatingActionButton) findViewById(R.id.button_remove);
removeAction.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((FloatingActionsMenu) findViewById(R.id.multiple_actions_down)).removeButton(removeAction);
}
});
ShapeDrawable drawable = new ShapeDrawable(new OvalShape());
drawable.getPaint().setColor(getResources().getColor(R.color.white));
((FloatingActionButton) findViewById(R.id.setter_drawable)).setIconDrawable(drawable);
final FloatingActionButton actionA = (FloatingActionButton) findViewById(R.id.action_a);
actionA.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
actionA.setTitle("Action A clicked");
}
});
// Test that FAMs containing FABs with visibility GONE do not cause crashes
findViewById(R.id.button_gone).setVisibility(View.GONE);
final FloatingActionButton actionEnable = (FloatingActionButton) findViewById(R.id.action_enable);
actionEnable.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
menuMultipleActions.setEnabled(!menuMultipleActions.isEnabled());
}
});
FloatingActionsMenu rightLabels = (FloatingActionsMenu) findViewById(R.id.right_labels);
FloatingActionButton addedOnce = new FloatingActionButton(this);
addedOnce.setTitle("Added once");
rightLabels.addButton(addedOnce);
FloatingActionButton addedTwice = new FloatingActionButton(this);
addedTwice.setTitle("Added twice");
rightLabels.addButton(addedTwice);
rightLabels.removeButton(addedTwice);
rightLabels.addButton(addedTwice);
}
}
class MyPagerAdapter extends FragmentPagerAdapter {
String[] tabs;
public MyPagerAdapter(FragmentManager fm) {
super(fm);
tabs =getResources().getStringArray(R.array.tabs);
}
@Override
public Fragment getItem(int position) {
MyFragment myFragment=MyFragment.getInstance(position);
return myFragment;
}
@Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}
@Override
public int getCount() {
return 6;
}
}
public static class MyFragment extends Fragment {
private TextView textView;
public static MyFragment getInstance(int position) {
MyFragment myFragment = new MyFragment();
Bundle args=new Bundle();
args.putInt("position",position);
myFragment.setArguments(args);
return myFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout=inflater.inflate(R.layout.fragment_my,container,false);
textView=(TextView)layout.findViewById(R.id.position);
Bundle bundle=getArguments();
if (bundle!=null)
{
textView.setText("The Page Selected is" +bundle.getInt("position"));
}
return layout;
}
}
这是堆栈跟踪
执行任务:[clean,:app:generateDebugSources, :app:generateDebugAndroidTestSources,:app:compileDebugSources, :应用程序:compileDebugAndroidTestSources]
按需配置是一项孵化功能。 :清洁UP-TO-DATE :app:clean:app:preBuild UP-TO-DATE:app:preDebugBuild UP-TO-DATE :app:checkDebugManifest:app:preReleaseBuild UP-TO-DATE :应用:prepareComAndroidSupportAppcompatV72311Library :应用:prepareComAndroidSupportCardviewV72311Library :应用:prepareComAndroidSupportDesign2311Library :应用:prepareComAndroidSupportRecyclerviewV72311Library :应用:prepareComAndroidSupportSupportV42311Library :应用:prepareComGetbaseFloatingactionbutton1101Library :app:prepareDebugDependencies:app:compileDebugAidl :app:compileDebugRenderscript:app:generateDebugBuildConfig :app:generateDebugAssets UP-TO-DATE:app:mergeDebugAssets :app:generateDebugResValues UP-TO-DATE:app:generateDebugResources :app:mergeDebugResources:app:processDebugManifest :app:processDebugResources:app:generateDebugSources :app:preDebugAndroidTestBuild UP-TO-DATE :应用:prepareDebugAndroidTestDependencies :app:compileDebugAndroidTestAidl:app:processDebugAndroidTestManifest :应用:compileDebugAndroidTestRenderscript :应用:generateDebugAndroidTestBuildConfig :app:generateDebugAndroidTestAssets UP-TO-DATE :应用:mergeDebugAndroidTestAssets :app:generateDebugAndroidTestResValues UP-TO-DATE :应用:generateDebugAndroidTestResources :应用:mergeDebugAndroidTestResources :应用:processDebugAndroidTestResources :app:generateDebugAndroidTestSources:app:compileDebugJavaWithJavac C:\用户\宏基\桌面\ Aprajeeta \研究\ android_work \ DEMO2 \程序\ SRC \主\ java的\演示\ DEMO2 \ Main3Activity.java:151: 错误:找不到符号 tabs = getResources()。getStringArray(R.array.tabs); ^符号:方法getResources()location:class MyPagerAdapter注意:某些输入文件使用或覆盖a 弃用的API。注意:使用-Xlint重新编译:弃用以获取详细信息。 1 错误
FAILED
失败:构建因异常而失败。
出了什么问题:任务'执行失败':app:compileDebugJavaWithJavac'。
编译失败;有关详细信息,请参阅编译器错误输出。
尝试:使用--stacktrace选项运行以获取堆栈跟踪。使用--info或--debug选项运行以获得更多日志输出。
建立失败
总时间:39.934秒
XML文件
<?xml version="1.0" encoding="utf-8"?>
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="demo.demo2.Main3Activity"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/app_bar"
android:orientation="vertical"
tools:context="demo.demo2.Main3Activity">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
<tabs.SlidingTabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<demo.demo2.FloatingActionButton
android:id="@+id/pink_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_icon="@drawable/ic_fab_star"
fab:fab_colorNormal="@color/pink"
fab:fab_colorPressed="@color/pink_pressed"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="16dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/pink_icon"
android:text="Text below button"
android:layout_centerHorizontal="true"
style="@style/menu_labels_style"
android:layout_marginBottom="48dp"/>
<demo.demo2.AddFloatingActionButton
android:id="@+id/semi_transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/pink_icon"
fab:fab_plusIconColor="@color/white"
fab:fab_colorNormal="@color/blue_semi_transparent"
fab:fab_colorPressed="@color/blue_semi_transparent_pressed"
android:layout_centerHorizontal="true"
android:layout_marginBottom="16dp"/>
<demo.demo2.FloatingActionButton
android:id="@+id/setter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/semi_transparent"
android:layout_centerHorizontal="true"
android:layout_marginBottom="16dp"/>
<demo.demo2.AddFloatingActionButton
android:id="@+id/normal_plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
fab:fab_plusIconColor="@color/half_black"
fab:fab_colorNormal="@color/white"
fab:fab_colorPressed="@color/white_pressed"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"/>
<demo.demo2.FloatingActionsMenu
android:id="@+id/right_labels"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="@id/normal_plus"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
fab:fab_addButtonColorNormal="@color/white"
fab:fab_addButtonColorPressed="@color/white_pressed"
fab:fab_addButtonPlusIconColor="@color/half_black"
fab:fab_addButtonSize="mini"
fab:fab_labelStyle="@style/menu_labels_style"
fab:fab_labelsPosition="right">
<demo.demo2.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_colorNormal="@color/white"
fab:fab_title="Label on the right"
fab:fab_colorPressed="@color/white_pressed"/>
<demo.demo2.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_colorNormal="@color/white"
fab:fab_size="mini"
fab:fab_title="Another one on the right"
fab:fab_colorPressed="@color/white_pressed"/>
</demo.demo2.FloatingActionsMenu>
<demo.demo2.FloatingActionsMenu
android:id="@+id/multiple_actions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
fab:fab_addButtonColorNormal="@color/white"
fab:fab_addButtonColorPressed="@color/white_pressed"
fab:fab_addButtonPlusIconColor="@color/half_black"
fab:fab_labelStyle="@style/menu_labels_style"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp">
<demo.demo2.FloatingActionButton
android:id="@+id/action_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_colorNormal="@color/white"
fab:fab_title="Action A"
fab:fab_colorPressed="@color/white_pressed"/>
<demo.demo2.FloatingActionButton
android:id="@+id/action_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_colorNormal="@color/white"
fab:fab_title="Action with a very long name that won\'t fit on the screen"
fab:fab_colorPressed="@color/white_pressed"/>
</demo.demo2.FloatingActionsMenu>
<demo.demo2.FloatingActionsMenu
android:id="@+id/multiple_actions_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
fab:fab_addButtonColorNormal="@color/white"
fab:fab_addButtonColorPressed="@color/white_pressed"
fab:fab_addButtonSize="mini"
fab:fab_addButtonPlusIconColor="@color/half_black"
fab:fab_expandDirection="down"
fab:fab_labelStyle="@style/menu_labels_style"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp">
<demo.demo2.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_colorNormal="@color/white"
fab:fab_colorPressed="@color/white_pressed"
fab:fab_size="mini"/>
<demo.demo2.FloatingActionButton
android:id="@+id/button_remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_colorNormal="@color/white"
fab:fab_colorPressed="@color/white_pressed"
fab:fab_title="Click to remove"/>
<demo.demo2.FloatingActionButton
android:id="@+id/button_gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_colorNormal="@color/white"
fab:fab_colorPressed="@color/white_pressed"/>
<demo.demo2.FloatingActionButton
android:id="@+id/action_enable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_colorNormal="@color/white"
fab:fab_title="Set bottom menu enabled/disabled"
fab:fab_colorPressed="@color/white_pressed"/>
</demo.demo2.FloatingActionsMenu>
<demo.demo2.FloatingActionsMenu
android:id="@+id/multiple_actions_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/multiple_actions_down"
android:layout_toStartOf="@+id/multiple_actions_down"
android:layout_alignParentTop="true"
fab:fab_addButtonColorNormal="@color/white"
fab:fab_addButtonColorPressed="@color/white_pressed"
fab:fab_addButtonSize="mini"
fab:fab_addButtonPlusIconColor="@color/half_black"
fab:fab_addButtonStrokeVisible="false"
fab:fab_expandDirection="left"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp">
<demo.demo2.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_colorNormal="@color/white"
fab:fab_colorPressed="@color/white_pressed"/>
<demo.demo2.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_colorNormal="@color/white"
fab:fab_colorPressed="@color/white_pressed"
fab:fab_size="mini"/>
<demo.demo2.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
fab:fab_colorNormal="@color/white"
fab:fab_colorPressed="@color/white_pressed"
fab:fab_size="mini"/>
</demo.demo2.FloatingActionsMenu>
<demo.demo2.AddFloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/setter_drawable"
android:layout_above="@id/setter"
android:layout_centerHorizontal="true"/>
</LinearLayout>
<fragment
android:id="@+id/fragment_navigation_drawer"
android:layout_width="@dimen/nav_drawer_width"
android:layout_height="match_parent"
app:layout="@layout/fragment_navigation_drawer"
android:layout_gravity="start"
android:name="demo.demo2.NavigationDrawerFragment"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
答案 0 :(得分:0)
以下行
时出错mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
--- app:compileDebugJavaWithJavac C:\ Users \ acer \ Desktop \ Aprajeeta \ studies \ android_work \ Demo2 \ app \ src \ main \ java \ demo \ demo2 \ Main3Activity.java:151:error:找不到符号标签
请检查您的布局中是否没有ID“标签”。或名称不同