0

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

<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>
4

1 に答える 1

1

あなたのlayout.xmlを投稿していただけますか?ウィジェットのセットアップ方法を知りたいです

編集:

あなたのxmlを見ると、IDを持つウィジェットが表示されません

starting.bio.R.id.tvCounter
    カウンター = (TextView) findViewById (starting.bio.R.id.tvCounter);
    

于 2013-01-18T22:56:43.113 に答える