我在运行我的应用时遇到问题,但我无法找出问题所在!我创建了第一个有两个按钮的屏幕。这个“主页”屏幕是chordsApp.java。如果我按下第一个按钮,我想去track1.java,如果按第二个按钮,我想去track2.java!该程序运行,但当我试图按下第一个或第二个按钮时,它会弹出一个错误并告诉我强制关闭!!这是代码:
ChordApp.java
package com.example.ChordsApp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ChordsApp extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.button);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), track1.class);
startActivityForResult(myIntent,0);
}
});
Button next1 = (Button) findViewById(R.id.button1);
next1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), track2.class);
startActivityForResult(myIntent, 0);
}
});
}
}
track1.java
package com.example.ChordsApp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class track1 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Button next = (Button) findViewById(R.id.button3);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
}
track2.java
package com.example.ChordsApp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class track2 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
Button next1 = (Button) findViewById(R.id.button4);
next1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/pic"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/guitar"
android:layout_gravity="center"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<Button
android:text="track1"
android:id="@+id/button"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px" />
<Button
android:text="track2"
android:id="@+id/button1"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px" />
</LinearLayout>
</LinearLayout>
main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="hi"/>
<Button android:text="Home"
android:id="@+id/button3"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px"
android:layout_gravity="center_horizontal"
>
</Button>
</LinearLayout>
main3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="hi"/>
<Button android:text="Home"
android:id="@+id/button4"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px"
android:layout_gravity="center_horizontal"
>
</Button>
</LinearLayout>