2013-01-18 38 views
0

我想把一個ViewFlipper放入我的字符串數組中,這樣我就可以製作一種可能不在應用中的閃存卡應用。加載到模擬器上時,應用程序崩潰。我認爲我可能會把所有東西都排成一行,或者留下一些東西,這是錯誤的。讓一個ViewFlipper與字符串數組一起工作

package starting.bio; 

import java.util.Random; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.animation.AccelerateInterpolator; 
import android.view.animation.Animation; 
import android.view.animation.TranslateAnimation; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.ViewFlipper; 

public class Flashcard extends Activity implements OnClickListener{ 

    View.OnTouchListener gestureListener; 
    ViewFlipper flipper; 
    String facts[] = { 
//Biology ch. 9 

"Fermentation is a partial degradation of sugars that occurs without O2. Aerobic respiration consumes organic molecules and O2 and yields ATP", 
"Cellular respiration includes both aerobic and anaerobic respiration but is often used to refer to aerobic respiration", 
"Chemical reactions that transfer electrons between reactants are called oxidation-reduction reactions, or redox reactions. In oxidation, a substance loses electrons, or is oxidized. In reduction, a substance gains electrons, or is reduced (the amount of positive charge is reduced)", 
"The electron donor is called the reducing agent. The electron receptor is called the oxidizing agent. Some redox reactions do not transfer electrons but change the electron sharing in covalent bonds", 
"Harvesting of energy from glucose has three stages-Glycolysis (breaks down glucose into two molecules of pyruvate), The citric acid cycle (completes the breakdown of glucose), and Oxidative phosphorylation (accounts for most of the ATP synthesis).", 
"Oxidative phosphorylation accounts for almost 90% of the ATP generated by cellular respiration. A smaller amount of ATP is formed in glycolysis and the citric acid cycle by substrate-level phosphorylation", 
"Most cellular respiration requires O2 to produce ATP. Without O2, the electron transport chain will cease to operate. In that case, glycolysis couples with fermentation or anaerobic respiration to produce ATP", 
"All use glycolysis (net ATP = 2) to oxidize glucose and harvest chemical energy of food. In all three, NAD+ is the oxidizing agent that accepts electrons during glycolysis. The processes have different final electron acceptors: an organic molecule (such as pyruvate or acetaldehyde) in fermentation and O2 in cellular respiration. Cellular respiration produces 32 ATP per glucose molecule; fermentation produces 2 ATP per glucose molecule ", 
"Obligate anaerobes carry out fermentation or anaerobic respiration and cannot survive in the presence of O2. Yeast and many bacteria are facultative anaerobes, meaning that they can survive using either fermentation or cellular respiration.", 


}; 



private Animation inFromRightAnimation() { 

Animation inFromRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, 
Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f 
); 
inFromRight.setDuration(500); 
inFromRight.setInterpolator(new AccelerateInterpolator()); 
return inFromRight; 
} 
private Animation outToLeftAnimation() { 
Animation outtoLeft = new TranslateAnimation(
    Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f, 
    Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f 
); 
outtoLeft.setDuration(500); 
outtoLeft.setInterpolator(new AccelerateInterpolator()); 
return outtoLeft; 
} 

private Animation inFromLeftAnimation() { 
Animation inFromLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, 
Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f 
); 
inFromLeft.setDuration(500); 
inFromLeft.setInterpolator(new AccelerateInterpolator()); 
return inFromLeft; 
} 
private Animation outToRightAnimation() { 
Animation outtoRight = new TranslateAnimation(
    Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, +1.0f, 
    Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f 
); 
outtoRight.setDuration(500); 
outtoRight.setInterpolator(new AccelerateInterpolator()); 
return outtoRight; 
} 

TextView display, display1; 
TextView counter; 
Button begin; 
Button next; 
Button previous; 
Button random; 
Random myRandom; 

int index = facts.length; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

flipper = (ViewFlipper) findViewById(R.id.flipper); 
Button button1 = (Button) findViewById(R.id.Button01); 
Button button2 = (Button) findViewById(R.id.Button02); 

button1.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View view) { 
     flipper.setInAnimation(inFromRightAnimation()); 
     flipper.setOutAnimation(outToLeftAnimation()); 
     flipper.showNext();  
    } 
}); 

button2.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View view) { 
     flipper.setInAnimation(inFromLeftAnimation()); 
     flipper.setOutAnimation(outToRightAnimation()); 
     flipper.showPrevious();  
    } 
}); 

display = (TextView) findViewById(starting.bio.R.id.tvResults); 
    counter = (TextView) findViewById(starting.bio.R.id.tvCounter); 
    next = (Button) findViewById(starting.bio.R.id.Next); 
    previous = (Button) findViewById(starting.bio.R.id.Previous); 
    random = (Button) findViewById(starting.bio.R.id.Random); 

    next.setOnClickListener(this); 
    previous.setOnClickListener(this); 
    random.setOnClickListener(this); 

    myRandom = new Random(); 


} 



private void showDisplay() { 
    display.setText(facts[index]); 
    counter.setText(String.valueOf(index + 1) + "/" 
      + String.valueOf(facts.length)); 
} 

public void onClick(View arg0) { 
    // TODO Auto-generated method stub 
    switch (arg0.getId()) { 
    case starting.bio.R.id.Next: 
     index++; 
     if (index > facts.length - 1) { 
      index = 0; 
     } 
     showDisplay(); 

     break; 

    case starting.bio.R.id.Previous: 
     index--; 
     if (index < 0) { 
      index = facts.length - 1; 
     } 
     showDisplay(); 
     break; 

    case starting.bio.R.id.Random: 
     index = (myRandom.nextInt(facts.length) - 1); 
     if (index < 0) { 
      index = facts.length - 1; 
     } 
     showDisplay(); 
     break; 

    } 

} 







} 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/flipper" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <include 
     android:id="@+id/first" 
     layout="@layout/first_view" /> 

    <include 
     android:id="@+id/second" 
     layout="@layout/second_view" /> 

</ViewFlipper> 

first_view.xml

<RelativeLayout android:id="@+id/LinearLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical"> 

<TextView android:text="This is the first view" 
android:id="@+id/TextView01" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:layout_gravity="center_horizontal"> 
</TextView> 

<TextView 
    android:id="@+id/tvResults" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/Next" 
    android:layout_marginTop="58dp" 
    android:gravity="center" 
    android:text="Touch Next or Previous to begin Biology!!!" 
    android:textColor="@android:color/black" 
    android:textSize="32dp" 
    android:textStyle="bold" /> 

<Button 
    android:id="@+id/Button01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/Next" 
    android:layout_marginLeft="33dp" 
    android:layout_marginTop="18dp" 
    android:text="Click for Second view " /> 

<Button 
    android:id="@+id/Next" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:background="@drawable/arrow" 
    android:text="Next" 
    android:textStyle="bold" /> 

<Button 
    android:id="@+id/Random" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_toRightOf="@+id/TextView01" 
    android:background="@drawable/bigred" 
    android:text="Random" 
    android:textStyle="bold" /> 

<Button 
    android:id="@+id/Previous" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:background="@drawable/arrowl" 
    android:text="Previous" 
    android:textStyle="bold" /> 

</RelativeLayout> 

second_view.xml

​​

回答

1

難道您發佈layout.xml,想怎麼看你設置你的小部件

編輯:

看你的XML,我沒有看到一個小部件ID

starting.bio.R.id.tvCounter
 
    counter = (TextView) findViewById(starting.bio.R.id.tvCounter); 

+0

得到它,感謝尋找它。 – jacobohunter

+0

好的,謝謝,我知道這是比較模糊的,但你覺得我可以用一個flashcard應用程序呢?還是我走錯了方向? – jacobohunter

+0

使用視圖腳蹼很好地顯示兩個不同的視圖。它可以用來製作閃存卡應用程序。我不知道你的閃存卡應用程序的要求,但你有兩個不同的視圖與不同的控制(按鈕)。當我開發應用程序時,我嘗試限制按鈕的數量,以及不給用戶一個乾淨直觀的GUI。林不知道我是否回答你的問題,如果我沒有抱歉大聲笑。 – GKproggy