我正在android studio中创建一个应用程序,当我开始我的一个活动时,应用程序崩溃并说它无法实例化活动。
我检查了清单文件,活动名称用红色加下划线,它说“X不是具体的类 - 验证Android XML文件中的资源。”
任何帮助将不胜感激,我不知道为什么这不起作用。我是新手,所以我真的很抱歉,如果这是一个愚蠢的问题/请原谅我,我敢肯定,代码很差。
这是我运行应用程序时遇到的错误
// HTML
<div class="img-container" data-src="imgurl1">
</div>
<div class="img-container" data-src="imgurl2">
</div>
<div class="img-container" data-src="imgurl3">
</div>
// CSS
.img-container{
opacity:0;
width:100%;
}
.loaded {
opacity:1;
}
// JS
window.onload = function(){
var container= document.querySelectorAll('img-container');
for(var i=0;i<container.length;i++){
var temp = container[i];
var img = Image();
img.src = temp.dataset.src;
img.onload = function(){
temp.classList.add('loaded');
};
temp.appendChild(img);
}
}
以下是该活动的代码
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)];
}
}
谢谢!
答案 0 :(得分:2)
删除带有类的abstract关键字
答案 1 :(得分:0)
日志表明您的结果活动无法实例化。您已将此标记为抽象类 - 尝试在Java中实例化抽象类的对象是错误的。
除非您打算扩展此类并实现具体方法然后实例化该活动,否则请删除abstract关键字。
你的活动中也有很多静态方法 - 这是代码味道。
答案 2 :(得分:-1)
抽象类无法实例化
abstract public class Results extends AppCompatActivity
implements GooglePlacesInterface { //, GoogleApiClient.OnConnectionFailedListener {