应用程序单击按钮时崩溃

时间:2015-02-27 07:53:42

标签: java android

我正在制作一个每次尝试调用另一个Activity时崩溃的Android应用程序。我试图阅读日志猫,但我不知道问题在哪里或为什么崩溃?

  

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调用的活动)

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结果:

> 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)
  

MainActivity的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: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>

2 个答案:

答案 0 :(得分:2)

它说SearchResults不会延伸Activity

由于您致电startActivitySearchResults传递SearchResults,因此Activity应延长{{1}}。

答案 1 :(得分:2)

Intent intent = new Intent(v.getContext(), SearchResults.class);

此行存在问题,SearchResults未延长Activity