我正在为我的 dashboard.xml 制作标签:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/WELCOME"
android:textSize="40dip"
android:gravity="center"
android:layout_marginTop="20dip"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/emailTextView"/>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/Logout_Me"
android:textSize="20dip"
android:textColor="#21dbd4"
android:textStyle="bold"
android:id="@+id/btnLogout"
android:layout_marginTop="80dip"
android:background="@null"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
但是我在这两个java文件中经常出错:TabsViewPagerFragmentActivity.java
package com.example.loginandregistration;
IMPORTS
public class TabsViewPagerFragmentActivity extends FragmentActivity {
// Declare Variables
ActionBar mActionBar;
ViewPager mPager;
Tab tab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from dashboard.xml
setContentView(R.layout.dashboard);
// Activate Navigation Mode Tabs
mActionBar = getActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Locate ViewPager in activity_main.xml
mPager = (ViewPager) findViewById(R.id.pager);
// Activate Fragment Manager
FragmentManager fm = getFragmentManager();
// Capture ViewPager page swipes
ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
// Find the ViewPager Position
mActionBar.setSelectedNavigationItem(position);
}
};
mPager.setOnPageChangeListener(ViewPagerListener);
// Locate the adapter class called ViewPagerAdapter.java
MyPageAdapter viewpageradapter = new MyPageAdapter(fm);
// Set the View Pager Adapter into ViewPager
mPager.setAdapter(viewpageradapter);
// Capture tab button clicks
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Pass the position on tab click to ViewPager
mPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
};
// Create first Tab
tab = mActionBar.newTab().setText("Tab1").setTabListener(tabListener);
mActionBar.addTab(tab);
// Create second Tab
tab = mActionBar.newTab().setText("Tab2").setTabListener(tabListener);
mActionBar.addTab(tab);
// Create third Tab
tab = mActionBar.newTab().setText("Tab3").setTabListener(tabListener);
mActionBar.addTab(tab);
}
}
(在第 46 行:
MyPageAdapter viewpageradapter = new MyPageAdapter(fm);
出现错误:
The constructor MyPageAdapter(FragmentManager) is undefined TabsViewPagerFragmentActivity.java /LoginAndRegistration/src/com/example/loginandregistration line 46 Java Problem
但是每当我添加一个构造函数时,我都会得到另一个错误,这会产生一个连续的错误循环)
和 MyPageAdapter:
package com.example.loginandregistration;
import android.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyPageAdapter extends FragmentPagerAdapter {
// Declare the number of ViewPager pages
final int PAGE_COUNT = 3;
public MyPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
switch (arg0) {
// Open FragmentTab1.java
case 0:
FragmentTab1 fragmenttab1 = new FragmentTab1();
return fragmenttab1;
// Open FragmentTab2.java
case 1:
FragmentTab2 fragmenttab2 = new FragmentTab2();
return fragmenttab2;
// Open FragmentTab3.java
case 2:
FragmentTab3 fragmenttab3 = new FragmentTab3();
return fragmenttab3;
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return PAGE_COUNT;
}
}
(行错误:18
public Fragment getItem(int arg0) {
23
FragmentTab1 fragmenttab1 = new FragmentTab1();
28
FragmentTab2 fragmenttab2 = new FragmentTab2();
33
FragmentTab3 fragmenttab3 = new FragmentTab3();
在第 18 行,它说返回类型无效并说将其更改为“(片段)” 在第 23、28、33 行,它说 FragmentTabs 无法解析为一种类型,但是当我放置我想要的类时作为 FragmentTabs(如下图所示:
// Open FragmentTab1.java
case 0:
CreatePic createpic = new CreatePic();
return createpic;
)我收到错误提示“CreatePic”未识别并且返回错误(当我单击快速修复时,它会创建错误循环)
我怎样才能解决这个问题?
添加 CREATEPIC.JAVA(顶部):
package com.example.loginandregistration;
IMPORTS
public class CreatePic extends View {
//drawing path
private Path drawPath;
//drawing and canvas paint
private Paint drawPaint, canvasPaint;
//initial color
private int paintColor = 0xFF660000, paintAlpha = 255;
//canvas
private Canvas drawCanvas;
//canvas bitmap
private Bitmap canvasBitmap;
//brush sizes
private float brushSize, lastBrushSize;
//erase flag
private boolean erase=false;
public CreatePic(Context context, AttributeSet attrs){
super(context, attrs);
setupDrawing();
}
//setup drawing
private void setupDrawing(){
//prepare for drawing and setup paint stroke properties
brushSize = getResources().getInteger(R.integer.medium_size);
lastBrushSize = brushSize;
drawPath = new Path();
drawPaint = new Paint();
drawPaint.setColor(paintColor);
drawPaint.setAntiAlias(true);
drawPaint.setStrokeWidth(brushSize);
drawPaint.setStyle(Paint.Style.STROKE);
drawPaint.setStrokeJoin(Paint.Join.ROUND);
drawPaint.setStrokeCap(Paint.Cap.ROUND);
canvasPaint = new Paint(Paint.DITHER_FLAG);
}
//size assigned to view
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
drawCanvas = new Canvas(canvasBitmap);
}
//draw the view - will be called after touch event
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
canvas.drawPath(drawPath, drawPaint);
}