gpt4 book ai didi

java - 单击按钮时应用程序崩溃

转载 作者:行者123 更新时间:2023-12-01 18:17:33 24 4
gpt4 key购买 nike

我正在制作一个 Android 应用程序,每次我尝试调用另一个 Activity 时都会崩溃。我正在尝试读取日志猫,但我看不到问题出在哪里或为什么会崩溃?

MainActivity.java

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnSearch = (Button) findViewById(R.id.btn_search);
btnSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), SearchResults.class);
startActivityForResult(intent, 0);
}
});

}
}

SearchResults.java

(这是从 MainActivity.java 调用的 Activity )

public class SearchResults extends MainActivity {

private String userAlias;
private String Title;
private String amount;
private String currency;
private String thumbnailImageURL;
private String tradeSummary;

public String getuserAlias() {
return userAlias;
}
public void setuserAlias(String userAlias) {
this.userAlias = userAlias;
}
// public String getTitle() {
// return Title;
// }
public void setTitle(String Title) {
this.Title = Title;
}
public String getamount() {
return amount;
}
public void setamount(String amount) {
this.amount = amount;
}
public String getthumbnailImageURL() {
return thumbnailImageURL;
}
public void setthumbnailImageURL(String thumbnailImageURL) {
this.thumbnailImageURL = thumbnailImageURL;
}

public String getcurrency() {
return currency;
}
public void setcurrency(String currency) {
this.currency = currency;
}
public String gettradeSummary() {
return tradeSummary;
}
public void settradeSummary(String tradeSummary) {
this.tradeSummary = tradeSummary;
}
@Override
public String toString() {
return "SearchRestuls [name=" + userAlias + ", link=" + thumbnailImageURL + ", about="
+ Title + ", imgUrl=" + thumbnailImageURL + ", amount=" + amount + "]";
}
}

LogCat results:

> 02-27 07:49:31.048    1623-1623/com.ryan.bobdemo E/AndroidRuntime﹕
> FATAL EXCEPTION: main
> Process: com.ryan.bobdemo, PID: 1623
> java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ryan.bobdemo/com.ryan.bobdemo.SearchResults}:
> java.lang.ClassCastException: com.ryan.bobdemo.SearchResults cannot be
> cast to android.app.Activity
> at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
> at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
> at android.app.ActivityThread.access$800(ActivityThread.java:135)
> at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
> at android.os.Handler.dispatchMessage(Handler.java:102)
> at android.os.Looper.loop(Looper.java:136)
> at android.app.ActivityThread.main(ActivityThread.java:5001)
> at java.lang.reflect.Method.invokeNative(Native Method)
> at java.lang.reflect.Method.invoke(Method.java:515)
> at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
> at dalvik.system.NativeStart.main(Native Method)
> Caused by: java.lang.ClassCastException: com.ryan.bobdemo.SearchResults cannot be cast to android.app.Activity
> at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
> at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
>             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
>             at android.app.ActivityThread.access$800(ActivityThread.java:135)
>             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
>             at android.os.Handler.dispatchMessage(Handler.java:102)
>             at android.os.Looper.loop(Looper.java:136)
>             at android.app.ActivityThread.main(ActivityThread.java:5001)
>             at java.lang.reflect.Method.invokeNative(Native Method)
>             at java.lang.reflect.Method.invoke(Method.java:515)
>             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
>             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
>             at dalvik.system.NativeStart.main(Native Method)

XML Of MainActivity (As requested)

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="#FFF"
tools:context=".MainActivity">

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/search_text"
android:textSize="30dp"
android:hint="Type here"
android:layout_row="0"
android:layout_column="0"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/btn_search" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:id="@+id/btn_search"
android:layout_row="0"
android:layout_column="1"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:onClick="search_dummy"/>

<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@mipmap/bob_logo_banner_sml"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"/>

</RelativeLayout>

最佳答案

上面写着SearchResults不扩展 Activity

既然您调用startActivity路过SearchResults到它,所以你的SearchResults应该延长Activity .

关于java - 单击按钮时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28760043/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com