我是Android开发的新手。我正在尝试制作一个应用程序,其中列出了各种各样的广告。当某人点击其中一个项目时,将打开第二个布局,其中包含有关该项目的地图和信息:
我通过阅读json来获取列表和信息,我通过php脚本动态生成(这里没有问题)。我也从json获取该项目的地址,并尝试对该位置进行地理编码。我生成第一个布局没有错误,但是当我尝试进入第二个布局时,应用程序崩溃,看起来是地图中的零点异常。该应用程序没有map元素。 这是一些代码:
来自主要活动:
@Override
protected void onPostExecute(final List<AdModels> s) {
super.onPostExecute(s);
dialog.dismiss();
if(s != null) {
AdAdapter adapter = new AdAdapter(getApplicationContext(), R.layout.row, s);
lvAds.setAdapter(adapter);
lvAds.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AdModels admodel = s.get(position);
Intent intent = new Intent(MainActivity.this, MapsActivity.class);
intent.putExtra("adInfo", new Gson().toJson(admodel));
startActivity(intent);
}
});
} else {
Toast.makeText(getApplicationContext(), "Not able to fetch data from server, please check url.", Toast.LENGTH_SHORT);
}
}
这是MapsActivity
package dk.forsoegsperson.fp;
import android.location.Address;
import android.location.Geocoder;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.TextView;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import dk.forsoegsperson.fp.Models.AdModels;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private TextView overskrift;
private TextView navn;
private TextView tlf;
private TextView email;
private TextView intro;
private WebView annonce;
String forsoeg_address;
String forsoeg_over;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
// setting up text views and stuff
setUpUIViews();
Bundle bundle = getIntent().getExtras();
if(bundle != null){
String json = bundle.getString("adInfo");
AdModels admodel = new Gson().fromJson(json, AdModels.class);
forsoeg_address = admodel.getAddress();
forsoeg_over = admodel.getOverskrift();
String coded_ad = admodel.getAnnonce();
String decoded_ad = Html.fromHtml(coded_ad).toString();
overskrift.setText(admodel.getOverskrift());
intro.setText(admodel.getIntro());
navn.setText(admodel.getNavn());
email.setText(admodel.getEmail());
tlf.setText("Tlf: " + admodel.getTlf());
annonce.loadData(decoded_ad, "text/html; charset=UTF-8", null);
}
}
private void setUpUIViews() {
overskrift = (TextView)findViewById(R.id.dOverskrift);
navn = (TextView)findViewById(R.id.dNavn);
tlf = (TextView)findViewById(R.id.dTlf);
email = (TextView)findViewById(R.id.dEmail);
intro = (TextView)findViewById(R.id.dIntro);
annonce = (WebView)findViewById(R.id.dAnnonce);
}
@Override
public void onMapReady(GoogleMap googleMap) {
GoogleMap mMap = googleMap;
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
if (forsoeg_address != null && forsoeg_address != "") {
try {
List<Address> addressList = geocoder.getFromLocationName(forsoeg_address, 1);
if (addressList != null && addressList.size() > 0) {
Address addres_loc = addressList.get(0);
LatLng pt = new LatLng(addres_loc.getLatitude(), addres_loc.getLongitude());
mMap.addMarker(new MarkerOptions().position(pt).title(forsoeg_over));
mMap.moveCamera(CameraUpdateFactory.newLatLng(pt));;
}
} catch (IOException e) {
e.printStackTrace();
}
}
else {
LatLng pt = new LatLng(56.2639, 9.5018);
mMap.addMarker(new MarkerOptions().position(pt).title(forsoeg_over));
mMap.moveCamera(CameraUpdateFactory.newLatLng(pt));
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}
}
这是activity_detail的xml;
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/map"
xmlns:tools="http://schemas.android.com/tools"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="150dp"
tools:context="dk.forsoegsperson.fp.MapsActivity"/>
<TextView
android:textSize="18dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dOverskrift"
android:layout_alignParentTop="true"
android:layout_marginTop="160dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:divider="@null"
android:dividerHeight="0dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dIntro"
android:layout_below="@+id/dOverskrift"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:divider="@null"
android:dividerHeight="0dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dNavn"
android:layout_below="@+id/dIntro"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:divider="@null"
android:dividerHeight="0dp"/>
<TextView
android:autoLink="email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dEmail"
android:layout_below="@+id/dNavn"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:divider="@null"
android:dividerHeight="0dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dTlf"
android:layout_below="@+id/dEmail"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:divider="@null"
android:dividerHeight="0dp"/>
<WebView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dAnnonce"
android:layout_below="@+id/dTlf"
android:layout_centerHorizontal="true"
android:divider="@null"
android:dividerHeight="0dp"/>
</RelativeLayout>
</ScrollView>
这是清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dk.forsoegsperson.fp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".detail" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_detail"></activity>
</application>
</manifest>
错误日志:
07-22 09:09:29.478 2789-2971/dk.forsoegsperson.fp E/Surface: getSlotFromBufferLocked: unknown buffer: 0xa073c340
07-22 09:09:36.693 2789-2789/dk.forsoegsperson.fp E/SysUtils: ApplicationContext is null in ApplicationStatus
07-22 09:09:36.892 2789-2789/dk.forsoegsperson.fp E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY)
07-22 09:09:36.892 2789-2789/dk.forsoegsperson.fp E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY)
07-22 09:09:37.071 2789-2789/dk.forsoegsperson.fp E/DataReductionProxySettingListener: No DRP key due to exception:java.lang.ClassNotFoundException: com.android.webview.chromium.Drp
07-22 09:09:37.352 2789-2789/dk.forsoegsperson.fp E/AndroidRuntime: FATAL EXCEPTION: main
Process: dk.forsoegsperson.fp, PID: 2789
java.lang.RuntimeException: Unable to start activity ComponentInfo{dk.forsoegsperson.fp/dk.forsoegsperson.fp.MapsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
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:5417)
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.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
at dk.forsoegsperson.fp.MapsActivity.onCreate(MapsActivity.java:51)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
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:5417)
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)
我好几天都在看这个。为什么应用程序崩溃了?请注意,我只在模拟器上运行此功能,并为此软件包提供了正确的Google API密钥。
(编辑:添加错误日志)
答案 0 :(得分:0)
将xml文件代码更改为:
<fragment
android:id="@+id/map"
xmlns:tools="http://schemas.android.com/tools"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="150dp"
tools:context="dk.forsoegsperson.fp.MapsActivity"/>
我将MapFragment更改为SupportMapFragment类