无法实例化活动-Android Studio [英] Could not instantiate activity - android studio

查看:60
本文介绍了无法实例化活动-Android Studio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在android studio中制作一个应用程序,当我开始一项活动时,该应用程序崩溃并说无法实例化活动.

I'm making an application in android studio and when starting one of my activities the app crashes and says that it could not instantiate the activity.

我检查了清单文件,活动名称用红色下划线标出,并说"X不是具体的类-验证Android XML文件中的资源."

I checked the manifest file and the activity name was underlined red and it said "X is not a concrete class - Validate resources inside Android XML files."

任何帮助将不胜感激,我不知道为什么这不起作用.我是开发的新手,如果这是一个愚蠢的问题,请您原谅我可怜的代码.

Any help would be appreciated, I have no idea why this isn't working. I'm new to developing so I'm really sorry if this is a stupid question/please forgive my, I'm sure, poor code.

这是我运行应用程序时遇到的错误

Here's the error I'm getting when I run the application

    07-12 10:01:50.260 918-3861/? I/ActivityManager: START u0 {cmp=com.example.daniel.digit/.Results (has extras)} from uid 10226 on display 0
07-12 10:01:50.276 27379-27379/com.example.daniel.digit E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: com.example.daniel.digit, PID: 27379
                                                                          java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.daniel.digit/com.example.daniel.digit.Results}: java.lang.InstantiationException: java.lang.Class<com.example.daniel.digit.Results> cannot be instantiated
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                              at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                              at android.os.Looper.loop(Looper.java:148)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5422)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                           Caused by: java.lang.InstantiationException: java.lang.Class<com.example.daniel.digit.Results> cannot be instantiated
                                                                              at java.lang.Class.newInstance(Native Method)
                                                                              at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                              at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                              at android.os.Looper.loop(Looper.java:148) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5422) 
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
07-12 10:01:50.281 918-4161/? W/ActivityManager:   Force finishing activity com.example.daniel.digit/.Results
07-12 10:01:50.284 918-4161/? W/ActivityManager:   Force finishing activity com.example.daniel.digit/.Questions
07-12 10:01:50.339 918-32759/? I/OpenGLRenderer: Initialized EGL, version 1.4
07-12 10:01:50.682 3271-14700/? W/MessageQueue: Handler (akgv) {896094c} sending message to a Handler on a dead thread
                                                java.lang.IllegalStateException: Handler (akgv) {896094c} sending message to a Handler on a dead thread
                                                    at android.os.MessageQueue.enqueueMessage(MessageQueue.java:543)
                                                    at android.os.Handler.enqueueMessage(Handler.java:631)
                                                    at android.os.Handler.sendMessageAtTime(Handler.java:600)
                                                    at android.os.Handler.sendMessageDelayed(Handler.java:570)
                                                    at android.os.Handler.post(Handler.java:326)
                                                    at rbv.a(:com.google.android.gms:1065)
                                                    at akox.a(:com.google.android.gms:140)
                                                    at akuj.a(:com.google.android.gms:374)
                                                    at aksn.a(:com.google.android.gms:2077)
                                                    at akll.a(:com.google.android.gms:93)
                                                    at akmw.a(:com.google.android.gms:262)
                                                    at akmx.run(:com.google.android.gms:2049)
                                                    at akgv.handleMessage(:com.google.android.gms:233)
                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                    at android.os.Looper.loop(Looper.java:148)
                                                    at android.os.HandlerThread.run(HandlerThread.java:61)
07-12 10:01:50.785 918-931/? W/ActivityManager: Activity pause timeout for ActivityRecord{85c8890 u0 com.example.daniel.digit/.Results t49287 f}

这是该活动的代码

package com.example.daniel.digit;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.places.PlacePhotoMetadata;
import com.google.android.gms.location.places.PlacePhotoMetadataBuffer;
import com.google.android.gms.location.places.PlacePhotoMetadataResult;
import com.google.android.gms.location.places.Places;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

import se.walkercrou.places.GooglePlaces;
import se.walkercrou.places.GooglePlacesInterface;
import se.walkercrou.places.Param;
import se.walkercrou.places.Place;
import se.walkercrou.places.Price;
import se.walkercrou.places.exception.GooglePlacesException;

abstract public class Results extends AppCompatActivity
        implements GooglePlacesInterface { //, GoogleApiClient.OnConnectionFailedListener {

    //Variables via intent from Questions
    Intent resultsIntent = getIntent();
    public String APIKEY = resultsIntent.getStringExtra("API_KEY"); //API Key
    public String RestaurantType = resultsIntent.getStringExtra("RestaurantType"); //Restaurant Type\
    public String Coordinates = resultsIntent.getStringExtra("Coordinates"); //Restaurant Type
    public Integer CostValue = resultsIntent.getIntExtra("CostValue", 0); //Cost Value
    public String API_URL = resultsIntent.getStringExtra("URI"); //API Request URI from URL Builder

    //Variables for view and card elements
    private TextView TextViewName1;
    private TextView TextViewName2;
    private ImageView Restaurant1Image;
    private ImageView Restaurant2Image;

    //Static variables
    public static int price;
    public static double Lat;
    public static double Lng;
    public static double Rating;
    public static String PlaceId;
    public static String Name;
    public static String PhotoRef;

    public GooglePlaces client;
    public List<Place> placesList;
    public String query;
    public String maxcost;
    public String mincost;
    public int counter = 0;
    //private GoogleApiClient mGoogleApiClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_results);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        //TextView variables
        TextViewName1 = (TextView) findViewById(R.id.restaurant1Name);
        TextViewName2 = (TextView) findViewById(R.id.restaurant2Name);

        //ImageView variables
        Restaurant1Image = (ImageView) findViewById(R.id.restaurant1Photo);
        Restaurant2Image = (ImageView) findViewById(R.id.restaurant2Photo);

        //sets query and mincost and maxcost
        query = (RestaurantType.equals("NO_TYPE") || RestaurantType.equals("Anything")) ? "Restaurant" : RestaurantType + "+Restaurant";
        mincost = (CostValue == 0) ? "0" : Integer.toString(CostValue - 2);
        maxcost = (CostValue == 0) ? "4" : Integer.toString(CostValue - 1);
        GooglePlaces client = new GooglePlaces(APIKEY);

        List<Place> placesList = client.getPlacesByQuery(query, Param.name("location").value(Coordinates),
                Param.name("minprice").value(mincost), Param.name("maxprice").value(maxcost),
                Param.name("opennow").value(true), Param.name("radius").value(MAXIMUM_RADIUS),
                Param.name("rankby=distance").value(true), Param.name("key").value(APIKEY));
    }

    public void btnRefreshOnClick (View v) {
        //run restaurantDetails twice
        restaurantDetails(counter);
        restaurantDetails(counter+1);

        //increase counter by 2
        counter = counter + 2;
    }

    public void restaurantDetails(int counter) {
        try {
            parse(client, placesList, getRequestHandler().get(API_URL), counter);
        } catch(Exception e) {
            throw new GooglePlacesException(e);
        }
        TextView restaurantName = ((counter%2) == 0) ? TextViewName1 : TextViewName2;
            //Assign name to TextView
            restaurantName.setText(Name);
}

   protected static void checkStatus(String statusCode, String errorMessage) {
        GooglePlacesException e = GooglePlacesException.parse(statusCode, errorMessage);
        if (e != null)
            throw e;
    }

    public static String parse(GooglePlaces client, List<Place> places, String str, int counter) {
        try {
            // parse json
            JSONObject json = new JSONObject(str);

            // check root elements
            String statusCode = json.getString(STRING_STATUS);
            checkStatus(statusCode, json.optString(STRING_ERROR_MESSAGE));
            if (statusCode.equals(STATUS_ZERO_RESULTS))
                return null;

            JSONArray results = json.getJSONArray(ARRAY_RESULTS);
            parseTextSearch(client, places, results, counter);

            return json.optString(STRING_NEXT_PAGE_TOKEN, null);
        }
        catch(Exception e){
            throw new GooglePlacesException(e);
        }
    }

    public static void parseTextSearch(GooglePlaces client, List<Place> places, JSONArray results,int counter) {
        try {
            if (counter >= results.length())
                return;

            JSONObject result = results.getJSONObject(counter);

            //Location
            JSONObject location = result.getJSONObject(OBJECT_GEOMETRY).getJSONObject(OBJECT_LOCATION);
            double Lat = location.getDouble(DOUBLE_LATITUDE);
            double Lng = location.getDouble(DOUBLE_LONGITUDE);

            String PlaceId = result.getString(STRING_PLACE_ID);
            String Name = result.optString(STRING_NAME);
            String PhotoRef = result.getString(STRING_PHOTO_REFERENCE);
            double Rating = result.optDouble(DOUBLE_RATING, -1);

            boolean priceDefined = result.has(INTEGER_PRICE_LEVEL);
            Price price = Price.NONE;
            if (priceDefined) {
                price = Price.values()[result.getInt(INTEGER_PRICE_LEVEL)];
            }
}

如果是相关的,则清单文件

And if it's relevent, the manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.daniel.digit">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:replace="android:icon,android:theme">
        <activity
            android:name=".SplashActivity"
            android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
        </activity>
        <activity
            android:name=".Questions"
            android:label="@string/title_activity_questions"
            android:parentActivityName=".MainActivity"
            android:theme="@style/AppTheme.NoActionBar">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.daniel.digit.MainActivity" />
        </activity>

        <uses-library
            android:name="com.android.volley"
            android:required="false" />

        <activity
            android:name=".Results"
            android:label="@string/title_activity_results"
            android:parentActivityName=".Questions"
            android:theme="@style/AppTheme.NoActionBar">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.daniel.digit.Questions"/>
        </activity>
        <meta-data android:name="com.google.android.geo.API_KEY" android:value="AIzaSyDvyih-7txW-4ajp8whWMZj5RsuUfORIoo"/>
    </application>

</manifest>

推荐答案

使用类删除抽象关键字

这篇关于无法实例化活动-Android Studio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆