Okay so I’m a total beginner at programming and this is my first programming project. What I wanted to do is create a ‘Motivational Quotes’ app where it would consist of around 10 person with multiple quotes each. Just some simple view-flipping and randomizing array. I was in the process of coding the view-flipping part and then when I tried to run it in the emulator then it gives me the ‘the application has stopped unexpectedly’. Logcat shows ‘FATAL EXCEPTION Main’. Hope someone can help me in making this run, I’m stuck at a dead end and the due date’s closing in fast.
I tried searching around and most asks for src,main.xml and manifest.xml so I’ll just post them here:
src:
package first.motivationalquotes;
import java.util.Random;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ViewFlipper;
public class MotivationalQuotesActivity extends Activity {
/** Called when the activity is first created. */
Button next,previous;
ViewFlipper vf;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
next = (Button) findViewById(R.id.button1);
previous = (Button) findViewById(R.id.button2);
next.setOnClickListener((OnClickListener) this);
previous.setOnClickListener((OnClickListener) this);
vf=(ViewFlipper)findViewById(R.id.viewFlipper1);
}
public void randomize(View v)
{
Random ran = new Random();
String[] ButtonText = null;
Resources res = getResources();
ButtonText = res.getStringArray(R.array.Quotes);
String strRandom = ButtonText[ran.nextInt(ButtonText.length)];
System.out.println("Random string is : "+strRandom);
TextView tv = (TextView) findViewById(R.id.textView2);
tv.setText(ButtonText[ran.nextInt(ButtonText.length)]);
}
public void onClick(View v) {
if (v == next) {
vf.showNext();
}
if (v == previous) {
vf.showPrevious();
}
}
}
Next is the main.xml (only coded to 2 person as of now):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ViewFlipper
android:id="@+id/viewFlipper1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<!-- View 1- Einstein - person1 -->
<TableLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Albert Einstein"
android:layout_gravity="center"
android:textSize="28dp" >
</TextView>
</TableRow>
<TableRow>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/clear_einstein" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/einstein1"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
</TableLayout>
<!-- View 2- Tesla - person2 -->
<TableLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nikola Tesla"
android:layout_gravity="center"
android:textSize="28dp" >
</TextView>
</TableRow>
<TableRow>
<ImageView
android:id="@+id/imageView1"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/nikola_tesla" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/einstein1"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
</TableLayout>
<TableLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Albert Einstein"
android:layout_gravity="center"
android:textSize="28dp" >
</TextView>
</TableRow>
<TableRow>
<ImageView
android:id="@+id/imageView1"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/clear_einstein" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/einstein1"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
</TableLayout>
</ViewFlipper>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="button2Click"
android:clickable="true"
android:text="<Previous"
android:layout_gravity="center" />
<Button
android:id="@+id/aboutMeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="randomize"
android:text="Randomize"
android:layout_gravity="center"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="button1Click"
android:text="Next>"
android:layout_gravity="center"/>
</LinearLayout>
</LinearLayout>
then the manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="first.motivationalquotes"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MotivationalQuotesActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Logcat:
http://i56.photobucket.com/albums/g183/luq_man/Logcat.png
I think you must implement OnClickListener.
public MotivationalQuotesActivity extend Activity implements OnClickListener{
……
}