这段代码有什么问题?它应该导入文本,并让一个按钮将您发送到另一个活动。我导入文本的部分工作得很好。
谢谢您的帮助!
public class Butik_resultat extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_butik_resultat);
//Modtager din søgning, som du gjorde i Butik.class
TextView modtag_butik = (TextView) findViewById(R.id.modtag_butik);
modtag_butik.setText("Du søgte efter denne butik: " + getIntent().getExtras().getString("SEND_BUTIK"));
addButtonlistener();
}
void addButtonlistener() {
// lytter til knap
Button send_til_kort = (Button) findViewById(R.id.kort);
send_til_kort.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent send_til_kort = new Intent(v.getContext(), Butik_kort.class);
v.getContext().startActivity(send_til_kort);
}
});
}
这是 LogCat 输出:
06-13 14:51:25.629: E/AndroidRuntime(20585): FATAL EXCEPTION: main
06-13 14:51:25.629: E/AndroidRuntime(20585): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.Butik_resultat}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
06-13 14:51:25.629: E/AndroidRuntime(20585): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
06-13 14:51:25.629: E/AndroidRuntime(20585): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
06-13 14:51:25.629: E/AndroidRuntime(20585): at android.app.ActivityThread.access$600(ActivityThread.java:127)
06-13 14:51:25.629: E/AndroidRuntime(20585): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
06-13 14:51:25.629: E/AndroidRuntime(20585): at android.os.Handler.dispatchMessage(Handler.java:99)
06-13 14:51:25.629: E/AndroidRuntime(20585): at android.os.Looper.loop(Looper.java:137)
06-13 14:51:25.629: E/AndroidRuntime(20585): at android.app.ActivityThread.main(ActivityThread.java:4441)
06-13 14:51:25.629: E/AndroidRuntime(20585): at java.lang.reflect.Method.invokeNative(Native Method)
06-13 14:51:25.629: E/AndroidRuntime(20585): at java.lang.reflect.Method.invoke(Method.java:511)
06-13 14:51:25.629: E/AndroidRuntime(20585): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
06-13 14:51:25.629: E/AndroidRuntime(20585): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
06-13 14:51:25.629: E/AndroidRuntime(20585): at dalvik.system.NativeStart.main(Native Method)
06-13 14:51:25.629: E/AndroidRuntime(20585): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
06-13 14:51:25.629: E/AndroidRuntime(20585): at com.example.test.Butik_resultat.addButtonlistener(Butik_resultat.java:31)
06-13 14:51:25.629: E/AndroidRuntime(20585): at com.example.test.Butik_resultat.onCreate(Butik_resultat.java:24)
06-13 14:51:25.629: E/AndroidRuntime(20585): at android.app.Activity.performCreate(Activity.java:4465)
06-13 14:51:25.629: E/AndroidRuntime(20585): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-13 14:51:25.629: E/AndroidRuntime(20585): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
06-13 14:51:25.629: E/AndroidRuntime(20585): ... 11 more
06-13 14:51:27.939: I/Process(20585): Sending signal. PID: 20585 SIG: 9
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Butik_resultat" >
<TextView
android:id="@+id/modtag_butik"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="TextView"
android:textSize="20sp"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/modtag_butik"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:gravity="center_horizontal"
android:hint="@string/ingen_resultat"
android:text="@string/ingen_resultat" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/kort"
android:layout_alignBottom="@+id/kort"
android:layout_alignLeft="@+id/textView3"
android:hint="@string/kvickly_ry"
android:text="@string/kvickly_ry"
android:textSize="15sp" />
<Button
android:id="@+id/kort"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/textView3"
android:hint="@string/kort"
android:text="@string/kort"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/kort"
android:layout_centerHorizontal="true"
android:layout_marginBottom="11dp"
android:text="Nærmeste Butik"
android:textSize="20sp"
tools:ignore="HardcodedText" />