当我点击一个按钮(名为chooseDistance)时,我只是想开始另一个活动,我相信这是有效的,但我有一个if语句来检查一个值是否在此活动开始之前是否为空,我相信当我点击按钮时它正在工作,如果值为null,它不会启动活动。我已将活动包含在清单文件中。我在下面的代码中找不到错误:
setdestination.class:
package com.example.drivetext;
import java.util.ArrayList;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class setdestination extends Activity implements OnMapLongClickListener {
private GoogleMap map;
Location myLocation;
TextView tvLocInfo;
LatLng pointfinal;
ListView listview;
ArrayList<String> distancesendList;
String finaldistance;
String contactNo;
String message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setdestination);
Intent intent = getIntent();
contactNo = intent.getStringExtra("PhoneNumber");
message = intent.getStringExtra("TextMessage");
Toast.makeText(this, contactNo, Toast.LENGTH_LONG).show();
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
ListView distanceList=(ListView)findViewById(R.id.list);
distancesendList = new ArrayList<String>();
getdistances();
// Create The Adapter with passing ArrayList as 3rd parameter
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, distancesendList);
// Set The Adapter
distanceList.setAdapter(arrayAdapter);
// register onClickListener to handle click events on each item
distanceList.setOnItemClickListener(new OnItemClickListener()
{
// argument position gives the index of item which is clicked
public void onItemClick(AdapterView<?> arg0, View v,int position, long arg3)
{
String selecteddistance=distancesendList.get(position);
finaldistance = selecteddistance;
Toast.makeText(getApplicationContext(), "Distance Selected : "+selecteddistance, Toast.LENGTH_LONG).show();
}
});
tvLocInfo = (TextView)findViewById(R.id.locinfo);
FragmentManager myFragmentManager = getFragmentManager();
MapFragment myMapFragment = (MapFragment)myFragmentManager.findFragmentById(R.id.map);
map = myMapFragment.getMap();
map.setMyLocationEnabled(true);
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
//myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//myMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
//myMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
map.setOnMapLongClickListener(this);
Button chooseDistance = (Button) findViewById(R.id.btnchooseDistance);
chooseDistance.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if (finaldistance != null)
{
Intent intent3 = new Intent(getApplicationContext(), targetdistance.class);
intent3.putExtra("PhoneNumber", contactNo);
intent3.putExtra("TextMessage", message);
intent3.putExtra("Coordinate", pointfinal);
intent3.putExtra("SelectedDistance", finaldistance);
startActivity(intent3);
}
}
});
}
void getdistances()
{
distancesendList.add("100");
distancesendList.add("250");
distancesendList.add("500");
distancesendList.add("1000");
}
public void onMapLongClick(LatLng point) {
tvLocInfo.setText("New marker added@" + point.toString());
map.addMarker(new MarkerOptions().position(point).title(point.toString()));
pointfinal = point;
Toast.makeText(this, point.toString(), Toast.LENGTH_LONG).show();
}
}
我的targetdistance.class:
package com.example.drivetext;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class targetdistance extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.target);
Intent intent = getIntent();
String cNumber = intent.getStringExtra("PhoneNumber");
String txt = intent.getStringExtra("TextMessage");
String coord = intent.getStringExtra("Coordinate");
String seldis = intent.getStringExtra("SelectedDistance");
Toast.makeText(this, cNumber, Toast.LENGTH_LONG).show();
Toast.makeText(this, txt, Toast.LENGTH_LONG).show();
Toast.makeText(this, coord.toString(), Toast.LENGTH_LONG).show();
Toast.makeText(this, seldis, Toast.LENGTH_LONG).show();
}
}
最后我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.drivetext"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<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="AIzaSyCvVY0OSaoe6Y_MfpP2S6HZQpXT5jtUwMQ"/>
<activity
android:name="com.example.drivetext.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=".setdestination" android:label="Set Destination"> </activity>
<activity android:name=".targetdistance" android:label="Target Distance"> </activity>
</application>
</manifest>
target.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>