首先我想说我使用我在每个中引用的2个Intent来改变它们之间的活动,我也可以用意图移动到这个类但是当我尝试从这个类中启动它时它会崩溃。
这是我收到错误的类。
package com.example.wince.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
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 java.util.ArrayList;
/**
* Created by Wince on 31.05.2016.
*/
public class RotaGoster extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
final Intent rotaIntent = new Intent(this, com.example.wince.myapplication.Rota.class);
final Intent kayitIntent = new Intent(this, com.example.wince.myapplication.RotaKayit.class);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rota);
String isim = getIntent().getExtras().getString("DB_name");
VeriTabani VT = new VeriTabani(this);
Button Kayit = (Button) findViewById(R.id.button6);
Button Rota = (Button) findViewById(R.id.button7);
int i = 0;
ArrayList<Yer> yerler = new ArrayList<>();
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map2);
mapFragment.getMapAsync(this);
yerler.addAll(VT.getAllYer());
for (i = 0; i < yerler.size(); i++) {
if (yerler.get(i).equals(isim)) {
mMap.addMarker(new MarkerOptions().position(new LatLng(VT.getYer(i).getLat(), VT.getYer(i).getLng())).title(i + "'inci nokta"));
}
}
Kayit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(kayitIntent);
finish();
}
});
Rota.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(rotaIntent);
finish();
}
});
//dbden verileri çekmek gerek
// db işlemleri ve gösterim burda yapılıcak.
}
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
}
}
这是android manifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".RotaKayit"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Rota"></activity>
<activity android:name=".RotaGoster"></activity>
</application>
这是错误的logcat
06-05 15:51:55.032 10275-10275/com.example.wince.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.wince.myapplication, PID: 10275
android.content.ActivityNotFoundException: Unable to find explicit activity class {/com.example.wince.myapplication.Rota}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1636)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1430)
at android.app.Activity.startActivityForResult(Activity.java:3532)
at android.app.Activity.startActivityForResult(Activity.java:3493)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:842)
at android.app.Activity.startActivity(Activity.java:3735)
at android.app.Activity.startActivity(Activity.java:3703)
at com.example.wince.myapplication.RotaGoster$2.onClick(RotaGoster.java:55)
at android.view.View.performClick(View.java:4633)
at android.view.View$PerformClick.run(View.java:19274)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5593)
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:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
像这样改变代码;把意图声明放在oncreate方法似乎解决了问题
package com.example.wince.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
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 java.util.ArrayList;
/**
* Created by Wince on 31.05.2016.
*/
public class RotaGoster extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rota);
String isim = getIntent().getExtras().getString("DB_name");
VeriTabani VT = new VeriTabani(this);
Button Kayit = (Button) findViewById(R.id.button6);
Button Rota = (Button) findViewById(R.id.button7);
int i = 0;
final Intent rotaIntent = new Intent(this, Rota.class);
final Intent kayitIntent = new Intent(this, RotaKayit.class);
ArrayList<Yer> yerler = new ArrayList<>();
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map2);
mapFragment.getMapAsync(this);
yerler.addAll(VT.getAllYer());
for (i = 0; i < yerler.size(); i++) {
if (yerler.get(i).equals(isim)) {
mMap.addMarker(new MarkerOptions().position(new LatLng(VT.getYer(i).getLat(), VT.getYer(i).getLng())).title(i + "'inci nokta"));
}
}
Kayit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(kayitIntent);
finish();
}
});
Rota.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(rotaIntent);
finish();
}
});
//dbden verileri çekmek gerek
// db işlemleri ve gösterim burda yapılıcak.
}
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
}
}
答案 1 :(得分:0)
您在实例外部引用了this
,因此您只能将Intents声明为字段,但必须在onCreate
或更高版本中初始化它们。
他们也不需要是最终的。