0

为什么xml布局不起作用?我打开布局页面时收到错误消息,请帮助我,什么问题,我不明白,有什么想法吗?谢谢你的回答...

我的 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.support.v4.view.ViewPager
    android:id="@+id/myfivepanelpager"
    android:layout_width="match_parent"
    android:layout_height="440dp" >
</android.support.v4.view.ViewPager>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="46dp"
    android:background="@drawable/bck2" >

    <Button
        android:id="@+id/buttonprevious"
        android:layout_width="62dp"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/lefticon" />

    <Button
        android:id="@+id/buttonnext"
        android:layout_width="62dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/righticon" />
</RelativeLayout>

</LinearLayout>

Logcat 错误信息;

10-29 16:26:13.422: E/AndroidRuntime(2187): FATAL EXCEPTION: main
10-29 16:26:13.422: E/AndroidRuntime(2187): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.hayvansesleri/com.hayvansesleri.Pictures}:     java.lang.ClassCastException: android.widget.Button cannot be cast to     android.support.v4.view.ViewPager
10-29 16:26:13.422: E/AndroidRuntime(2187):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
10-29 16:26:13.422: E/AndroidRuntime(2187):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
10-29 16:26:13.422: E/AndroidRuntime(2187):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
10-29 16:26:13.422: E/AndroidRuntime(2187):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
10-29 16:26:13.422: E/AndroidRuntime(2187):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-29 16:26:13.422: E/AndroidRuntime(2187):     at android.os.Looper.loop(Looper.java:137)
10-29 16:26:13.422: E/AndroidRuntime(2187):     at android.app.ActivityThread.main(ActivityThread.java:4441)
10-29 16:26:13.422: E/AndroidRuntime(2187):     at java.lang.reflect.Method.invokeNative(Native Method)
10-29 16:26:13.422: E/AndroidRuntime(2187):     at java.lang.reflect.Method.invoke(Method.java:511)
10-29 16:26:13.422: E/AndroidRuntime(2187):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-29 16:26:13.422: E/AndroidRuntime(2187):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-29 16:26:13.422: E/AndroidRuntime(2187):     at dalvik.system.NativeStart.main(Native Method)
10-29 16:26:13.422: E/AndroidRuntime(2187): Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to android.support.v4.view.ViewPager
10-29 16:26:13.422: E/AndroidRuntime(2187):     at com.hayvansesleri.Pictures.onCreate(Pictures.java:38)
10-29 16:26:13.422: E/AndroidRuntime(2187):     at android.app.Activity.performCreate(Activity.java:4465)
10-29 16:26:13.422: E/AndroidRuntime(2187):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-29 16:26:13.422: E/AndroidRuntime(2187):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
10-29 16:26:13.422: E/AndroidRuntime(2187):     ... 11 more

图片.class;

public class Pictures extends Activity implements OnPageChangeListener,OnClickListener  {
protected static final Context Context = null;

private static final int count = 23;
private AdController myController;
SoundManager snd; 

View view=null;

final Activity act = this;  
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInsta,mknceState);
    setContentView(R.layout.picturespage);


 MyPagerAdapter adapter = new MyPagerAdapter();
 final ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
 myPager.setAdapter(adapter);
 myPager.setCurrentItem(12); 
 myPager.setOnPageChangeListener(this);
 snd = new SoundManager(this);
 this.setVolumeControlStream(AudioManager.STREAM_MUSIC);

 Button next = (Button) findViewById(R.id.buttonnext);
 next.setOnClickListener(new View.OnClickListener(){

     public void onClick(View v) {
         myPager.setCurrentItem(myPager.getCurrentItem()+1);
     }
 }); 

 Button previous = (Button) findViewById(R.id.buttonprevious);
 previous.setOnClickListener(new View.OnClickListener(){

     public void onClick(View v) {
         myPager.setCurrentItem(myPager.getCurrentItem()-1);
     }
 }); 
 };
4

1 回答 1

5

juned 可能是对的,请尝试在您的项目上运行 «ant clean»。有时 R.java 会在构建系统没有注意到的情况下变得过时。例如,您可以在您的 xml 文件中交换 Button 和 ViewPager 的顺序,但是在 R.id 类中生成的 id 保持错误的顺序。«ant release»/«ant debug» 之前的 «ant clean» 修复了该问题。

于 2012-10-29T14:17:59.740 回答