我正在尝试从谷歌地方的androidhive做一个教程。附近的地方显示在列表视图中但是当我点击按钮将我带到应用程序时,应用程序强制关闭noclassdeferror。
MainAcitivity.java
package com.androidhive.googleplacesandmaps;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MainActivity extends Activity {
// flag for Internet connection status
Boolean isInternetPresent = false;
// Connection detector class
ConnectionDetector cd;
// Alert Dialog Manager
AlertDialogManager alert = new AlertDialogManager();
// Google Places
GooglePlaces googlePlaces;
// Places List
PlacesList nearPlaces;
// GPS Location
GPSTracker gps;
// Button
Button btnShowOnMap;
// Progress dialog
ProgressDialog pDialog;
// Places Listview
ListView lv;
// ListItems data
ArrayList<HashMap<String, String>> placesListItems = new ArrayList<HashMap<String,String>>();
// KEY Strings
public static String KEY_REFERENCE = "reference"; // id of the place
public static String KEY_NAME = "name"; // name of the place
public static String KEY_VICINITY = "vicinity"; // Place area name
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cd = new ConnectionDetector(getApplicationContext());
// Check if Internet present
isInternetPresent = cd.isConnectingToInternet();
if (!isInternetPresent) {
// Internet Connection is not present
alert.showAlertDialog(MainActivity.this, "Internet Connection Error",
"Please connect to working Internet connection", false);
// stop executing code by return
return;
}
// creating GPS Class object
gps = new GPSTracker(this);
// check if GPS location can get
if (gps.canGetLocation()) {
Log.d("Your Location", "latitude:" + gps.getLatitude() + ", longitude: " + gps.getLongitude());
} else {
// Can't get user's current location
alert.showAlertDialog(MainActivity.this, "GPS Status",
"Couldn't get location information. Please enable GPS",
false);
// stop executing code by return
return;
}
// Getting listview
lv = (ListView) findViewById(R.id.list);
// button show on map
btnShowOnMap = (Button) findViewById(R.id.btn_show_map);
// calling background Async task to load Google Places
// After getting places from Google all the data is shown in listview
new LoadPlaces().execute();
/** Button click event for shown on map */
btnShowOnMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(MainActivity.this,
PlacesMapActivity.class);
// Sending user current geo location
i.putExtra("user_latitude", Double.toString(gps.getLatitude()));
i.putExtra("user_longitude", Double.toString(gps.getLongitude()));
// passing near places to map activity
i.putExtra("near_places", nearPlaces);
// staring activity
startActivity(i);
}
});
/**
* ListItem click event
* On selecting a listitem SinglePlaceActivity is launched
* */
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String reference = ((TextView) view.findViewById(R.id.reference)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
SinglePlaceActivity.class);
// Sending place refrence id to single place activity
// place refrence id used to get "Place full details"
in.putExtra(KEY_REFERENCE, reference);
startActivity(in);
}
});
}
/**
* Background Async Task to Load Google places
* */
class LoadPlaces extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage(Html.fromHtml("<b>Search</b><br/>Loading Places..."));
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting Places JSON
* */
protected String doInBackground(String... args) {
// creating Places class object
googlePlaces = new GooglePlaces();
try {
// Separeate your place types by PIPE symbol "|"
// If you want all types places make it as null
// Check list of types supported by google
//
String types = "cafe|restaurant"; // Listing places only cafes, restaurants
// Radius in meters - increase this value if you don't find any places
double radius = 1000; // 1000 meters
// get nearest places
nearPlaces = googlePlaces.search(gps.getLatitude(),
gps.getLongitude(), radius, types);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* and show the data in UI
* Always use runOnUiThread(new Runnable()) to update UI from background
* thread, otherwise you will get error
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed Places into LISTVIEW
* */
// Get json response status
String status = nearPlaces.status;
// Check for all possible status
if(status.equals("OK")){
// Successfully got places details
if (nearPlaces.results != null) {
// loop through each place
for (Place p : nearPlaces.results) {
HashMap<String, String> map = new HashMap<String, String>();
// Place reference won't display in listview - it will be hidden
// Place reference is used to get "place full details"
map.put(KEY_REFERENCE, p.reference);
// Place name
map.put(KEY_NAME, p.name);
// adding HashMap to ArrayList
placesListItems.add(map);
}
// list adapter
ListAdapter adapter = new SimpleAdapter(MainActivity.this, placesListItems,
R.layout.list_item,
new String[] { KEY_REFERENCE, KEY_NAME}, new int[] {
R.id.reference, R.id.name });
// Adding data into listview
lv.setAdapter(adapter);
}
}
else if(status.equals("ZERO_RESULTS")){
// Zero results found
alert.showAlertDialog(MainActivity.this, "Near Places",
"Sorry no places found. Try to change the types of places",
false);
}
else if(status.equals("UNKNOWN_ERROR"))
{
alert.showAlertDialog(MainActivity.this, "Places Error",
"Sorry unknown error occured.",
false);
}
else if(status.equals("OVER_QUERY_LIMIT"))
{
alert.showAlertDialog(MainActivity.this, "Places Error",
"Sorry query limit to google places is reached",
false);
}
else if(status.equals("REQUEST_DENIED"))
{
alert.showAlertDialog(MainActivity.this, "Places Error",
"Sorry error occured. Request is denied",
false);
}
else if(status.equals("INVALID_REQUEST"))
{
alert.showAlertDialog(MainActivity.this, "Places Error",
"Sorry error occured. Invalid Request",
false);
}
else
{
alert.showAlertDialog(MainActivity.this, "Places Error",
"Sorry error occured.",
false);
}
}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
logcat的:
07-03 12:27:10.101: E/AndroidRuntime(10539): FATAL EXCEPTION: main
07-03 12:27:10.101: E/AndroidRuntime(10539): java.lang.NoClassDefFoundError: com.androidhive.googleplacesandmaps.PlacesMapActivity
07-03 12:27:10.101: E/AndroidRuntime(10539): at com.androidhive.googleplacesandmaps.MainActivity$1.onClick(MainActivity.java:110)
07-03 12:27:10.101: E/AndroidRuntime(10539): at android.view.View.performClick(View.java:2538)
07-03 12:27:10.101: E/AndroidRuntime(10539): at android.view.View$PerformClick.run(View.java:9152)
07-03 12:27:10.101: E/AndroidRuntime(10539): at android.os.Handler.handleCallback(Handler.java:587)
07-03 12:27:10.101: E/AndroidRuntime(10539): at android.os.Handler.dispatchMessage(Handler.java:92)
07-03 12:27:10.101: E/AndroidRuntime(10539): at android.os.Looper.loop(Looper.java:130)
07-03 12:27:10.101: E/AndroidRuntime(10539): at android.app.ActivityThread.main(ActivityThread.java:3689)
07-03 12:27:10.101: E/AndroidRuntime(10539): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 12:27:10.101: E/AndroidRuntime(10539): at java.lang.reflect.Method.invoke(Method.java:507)
07-03 12:27:10.101: E/AndroidRuntime(10539): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
07-03 12:27:10.101: E/AndroidRuntime(10539): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-03 12:27:10.101: E/AndroidRuntime(10539): at dalvik.system.NativeStart.main(Native Method)
07-03 12:27:10.101: E/AndroidRuntime(10539): Caused by: java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
07-03 12:27:10.101: E/AndroidRuntime(10539): at dalvik.system.DexFile.defineClass(Native Method)
07-03 12:27:10.101: E/AndroidRuntime(10539): at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:207)
07-03 12:27:10.101: E/AndroidRuntime(10539): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:200)
07-03 12:27:10.101: E/AndroidRuntime(10539): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
07-03 12:27:10.101: E/AndroidRuntime(10539): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
07-03 12:27:10.101: E/AndroidRuntime(10539): ... 12 more
显示noclassdeferror的行号110是:
Intent i = new Intent(MainActivity.this,
PlacesMapActivity.class);
清单是:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidhive.googleplacesandmaps"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- Add Google Map Library -->
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- SinglePlaceActivity -->
<activity android:name=".SinglePlaceActivity" android:label="Place Details">
</activity>
<!-- PlacesMapActivity -->
<activity android:name=".PlacesMapActivity" android:label="Near Places Map View">
</activity>
</application>
<!-- Internet Permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Network State Permissions -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Access Location -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>
请帮助!!
答案 0 :(得分:0)
我的经验是我使用了这种因果方法,我得到了答案,试试这个,希望你帮助这个。
map xml:
<fragment
android:id="@+id/src_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
活动类:
public class MapPlace extends FragmentActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.searchresult);
FragmentManager fmanager = getSupportFragmentManager();
Fragment fragment = fmanager.findFragmentById(R.id.src_map);
SupportMapFragment supportmapfragment = (SupportMapFragment)fragment;
map = supportmapfragment.getMap();
}
}
清单文件中的:使用正常预设,如网络,wifistate,networkstate,finelocation和粗略位置。另外在清单文件中添加这些模块。
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="use your API key" />