好的,所以我是编程的初学者,这是我的第一个编程项目。我想做的是创建一个“励志名言”应用程序,其中包含大约 10 个人,每个人都有多个引号。只是一些简单的视图翻转和随机化数组。我正在编写视图翻转部分的代码,然后当我尝试在模拟器中运行它时,它给了我“应用程序已意外停止”。Logcat 显示“FATAL EXCEPTION Main”。希望有人可以帮助我完成这项工作,我陷入了死胡同,截止日期很快就结束了。
我试着四处搜索,大多数都要求 src、main.xml 和 manifest.xml,所以我将它们发布在这里:
源代码:
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();
}
}
}
接下来是 main.xml(目前仅编码为 2 人):
<?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>
然后是 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