2011-06-21 40 views
0

我曾嘗試通過拖動觸摸屏來切換屏幕代碼..但它給出了一個錯誤...任何人都可以請檢查我在代碼中做了什麼錯誤..and在通過拖動觸摸屏來切換屏幕

vf.setInAnimation(AnimationUtils.loadAnimation(此, R.anim.push_left_in))出現錯誤; vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));

爲.xml文件代碼

<ViewFlipper android:id="@+id/details" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="#ffffff"> 

     <TextView android:id="@+id/tv_country" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textColor="#000000" 
      android:textStyle="bold" 
      android:textSize="18px" 
      android:text="Country" > 
     </TextView> 
     <Spinner android:text="" 
      android:id="@+id/spinner_country" 
      android:layout_width="200px" 
      android:layout_height="55px"> 
     </Spinner> 
    </LinearLayout> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="#ffffff"> 

     <TextView android:id="@+id/tv_income" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textColor="#000000" 
      android:textStyle="bold" 
      android:textSize="18px" 
      android:text="Income" > 
     </TextView> 
     <EditText android:text="" 
      android:id="@+id/et_income" 
      android:layout_width="200px" 
      android:layout_height="55px"> 
     </EditText> 
    </LinearLayout> 

</ViewFlipper> 

代碼java文件

package com.examples.switchactivtybydragging; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.view.animation.*; 
import android.widget.ArrayAdapter; 
import android.widget.LinearLayout; 
import android.widget.Spinner; 
import android.widget.ViewFlipper; 

public class SwitchActivityByDraggingActivity extends Activity implements OnTouchListener { 
    /** Called when the activity is first created. */ 

    private float downXValue; 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     LinearLayout layMain = (LinearLayout) findViewById(R.id.layout_main); 
     layMain.setOnTouchListener((OnTouchListener) this); 
     Spinner spinnerCountries = (Spinner) findViewById(R.id.spinner_country); 
     @SuppressWarnings({ "unchecked", "rawtypes" }) 
     ArrayAdapter countryArrayAdapter = new ArrayAdapter(this, 
        android.R.layout.simple_spinner_dropdown_item, 
        new String[] { "Canada", "USA" }); 
     spinnerCountries.setAdapter(countryArrayAdapter); 
    } 
    public boolean onTouch(View arg0, MotionEvent arg1) { 

     // Get the action that was done on this touch event 
     switch (arg1.getAction()) 
     { 
      case MotionEvent.ACTION_DOWN: 
      { 
       // store the X value when the user's finger was pressed down 
       downXValue = arg1.getX(); 
       break; 
      } 

      case MotionEvent.ACTION_UP: 
      { 
       // Get the X value when the user released his/her finger 
       float currentX = arg1.getX();    

       // going backwards: pushing stuff to the right 
       if (downXValue < currentX) 
       { 
        // Get a reference to the ViewFlipper 
        ViewFlipper vf = (ViewFlipper) findViewById(R.id.details); 
        // Set the animation 
         vf.setAnimation(AnimationUtils.loadAnimation(this,R.anim.push_left_out)); 
         // Flip! 
         vf.showPrevious(); 
       } 

       // going forwards: pushing stuff to the left 
       if (downXValue > currentX) 
       { 
        // Get a reference to the ViewFlipper 
        ViewFlipper vf = (ViewFlipper) findViewById(R.id.details); 
        // Set the animation 
        vf.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.push_left_in)); 
         // Flip! 
        vf.showNext(); 
       } 
       break; 
      } 
     } 

     // if you return false, these actions will not be recorded 
     return true; 
    } 

} 
+0

請包括您所看到的錯誤 - 理想情況下是logcat的完整堆棧跟蹤。 –

+0

其實在我的Android sdk .xml文件中動畫是不可用..所以它沒有找到push_left_in.xml和push_left_out.xml,我用過..所以我怎麼能得到這個在我的SDK .. – AndroidDev

+0

你在哪裏把你的動畫文件? –

回答

2

你的問題是,你還沒有創建動畫。爲了讓您的代碼正常工作,您需要res/anim/push_left_in.xmlres/anim/push_left_out.xml中的動畫。這是沒有這些文件導致你的錯誤。

我寫了一篇關於Simple Animations的博客系列,可以幫助您瞭解如何創建這些文件,以及您需要如何創建這些文件。

+0

感謝馬克現在的工作.. – AndroidDev

+0

好馬克..感謝很多 – AndroidDev