java - 让 ViewFlipper 使用字符串数组

标签 java android eclipse 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>

第二 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 second view"
android:id="@+id/TextView02"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal">
</TextView>

<Button
    android:id="@+id/Button02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/TextView02"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="39dp"
    android:text="Click for first view " />

<TextView
    android:id="@+id/tvResults"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/Button02"
    android:layout_marginTop="20dp"
    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/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" />

</RelativeLayout>

最佳答案

您可以发布您的layout.xml吗,想看看您如何设置您的小部件

编辑:

查看您的 xml,我没有看到带有 ID 的小部件

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

关于java - 让 ViewFlipper 使用字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14408793/

相关文章:

java - 我的时间介于开始时间和结束时间之间

android - 如何在 android actionbar compat 上强制溢出菜单?

java - Android:无法使用 null 启动服务:java.lang.NullPointerException

android - 如何在代码中检测 7"Android 平板电脑

Java项目在eclipse中运行但导出时失败

java - Android 绑定(bind)服务问题

Java 对具有重复元素的集合进行排序。如果重复,按出现顺序排序

java - 如何在eclipse自动导入中使用Maven依赖项

java - 通过JUNIT运行程序时如何打印值(用于调试)

java - 如何为Executors.newScheduledThreadPool设置RemoveOnCancelPolicy(5)