来自其他活动的Android来电活动无效

时间:2012-07-06 06:50:10

标签: android

我正在尝试从默认活动中调用一个活动但是当触发调用活动时,我的应用程序崩溃并关闭此消息07-06 11:54:59.291: E/MapActivity(366): Couldn't get connection factory client。这是我调用活动的当前代码

public class GMapsActivity extends MapActivity {
EditText password;
Button   login;
MapView mapView;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initcontrols();
}
public void initcontrols()
{
    password=(EditText)findViewById(R.id.pass);
    login=(Button)findViewById(R.id.login);
    mapView=(MapView)findViewById(R.id.map_view);
    login.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View v)
        {
             Intent myIntent = null;
             if(password.getText().toString().equals("admin")) {
             System.out.println("Entering");
             mapView.setVisibility(View.VISIBLE);
             login.setVisibility(View.INVISIBLE);
             password.setVisibility(View.INVISIBLE);
             myIntent = new Intent(v.getContext(), GMapsActivity1.class);

            } else {
            }
             startActivity(myIntent);
        }

    });

}



@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}


}

请有人帮我这样做

更新 这是我的第二个活动代码请检查是否需要更改

public class GMapsActivity1 extends MapActivity {

private MapView mapView;
EditText password;
Button   login;

JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb=null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


mapView = (MapView) findViewById(R.id.map_view);       
        mapView.setBuiltInZoomControls(true);

 ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//http post
try{
     HttpClient httpclient = new DefaultHttpClient();
     HttpPost httppost = new HttpPost("http://sml.com.pk/a/map.php");
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
     HttpResponse response = httpclient.execute(httppost);
     HttpEntity entity = response.getEntity();
     is = entity.getContent();
     }catch(Exception e){
         Log.e("log_tag", "Error in http connection"+e.toString());
    }
//convert response to string
try{
      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
       sb = new StringBuilder();
       sb.append(reader.readLine() + "\n");

       String line="0";
       while ((line = reader.readLine()) != null) {
                      sb.append(line + "\n");
        }
        is.close();
        result=sb.toString();
        }catch(Exception e){
              Log.e("log_tag", "Error converting result "+e.toString());
        }
//paring data
double LAT;
double LANG;
String INFO;
try{
      jArray = new JSONArray(result);
      JSONObject json_data=null;
      for(int i=0;i<jArray.length();i++){
             json_data = jArray.getJSONObject(i);
             LAT=json_data.getDouble("lat");
             LANG=json_data.getDouble("lang");
             INFO=json_data.getString("info");


//Overlay code
List<Overlay> mapOverlays = mapView.getOverlays();
                Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
        CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(drawable, this);

        //GeoPoint point = new GeoPoint(latitudeE6, longitudeE6);
        GeoPoint point = new GeoPoint((int)(LAT * 1e6),(int)(LANG * 1e6));
        OverlayItem overlayitem = new OverlayItem(point, "Shakarganj", INFO);
        itemizedOverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedOverlay);

        MapController mapController = mapView.getController();

        mapController.animateTo(point);
        mapController.setZoom(6);

//Overlay code

         }
      }
      catch(JSONException e1){
          Toast.makeText(getBaseContext(), "No Vehicles Found" ,Toast.LENGTH_LONG).show();
      } catch (ParseException e1) {
            e1.printStackTrace();
    }
}
@Override
protected boolean isRouteDisplayed() {
    return false;
}
}

这是My Manifest文件代码

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.javacodegeeks.android.googlemaps"
      android:versionCode="1"
      android:versionName="1.0">
      <uses-sdk android:minSdkVersion="10"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <activity android:name=".GMapsActivity"
                  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="GMapsActivity1"
           android:launchMode="singleTop">
            <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

        </activity>

        <uses-library android:name="com.google.android.maps" />


    </application>    

</manifest> 

UPDATE1

package com.javacodegeeks.android.googlemaps;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.Toast;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> {

    private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();

    private Context context;

    public CustomItemizedOverlay(Drawable defaultMarker) {
          super(boundCenterBottom(defaultMarker));
    }

    public CustomItemizedOverlay(Drawable defaultMarker, Context context) {
          this(defaultMarker);
          this.context = context;
    }

    @Override
    protected OverlayItem createItem(int i) {
        return mapOverlays.get(i);
    }

    @Override
    public int size() {
        return mapOverlays.size();
    }

    @Override
    protected boolean onTap(int index) {
        OverlayItem item = mapOverlays.get(index);
        //AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        //dialog.setTitle(item.getTitle());
        //dialog.setMessage(item.getSnippet());
        //dialog.show();
        //Toast.makeText(context, item.getSnippet(), 20);
        Toast.makeText(context, item.getSnippet(),  Toast.LENGTH_LONG).show();
        return true;
    }

    public void addOverlay(OverlayItem overlay) {
        mapOverlays.add(overlay);
        this.populate();
    }

}

8 个答案:

答案 0 :(得分:1)

请尝试以下代码:

    public class GMapsActivity extends MapActivity {
    EditText password;
    Button   login;
    MapView mapView;


    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

        password=(EditText)findViewById(R.id.pass);
        login=(Button)findViewById(R.id.login);
        mapView=(MapView)findViewById(R.id.map_view);


        login.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v)
            {
                 Intent myIntent = null;
                 if(password.getText().toString().equals("admin")) {
                 System.out.println("Entering");
                 mapView.setVisibility(View.VISIBLE);
                 login.setVisibility(View.INVISIBLE);
                 password.setVisibility(View.INVISIBLE);
                 myIntent = new Intent(GMapsActivity.this, GMapsActivity1.class);
                 startActivity(myIntent);
                } 

            }

        });

    }



    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }


    }

<强>更新

为什么你用这个:

<activity android:name="GMapsActivity1"
          android:launchMode="singleTop">
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />

</activity>

像这样使用:

<activity
            android:name=".GMapsActivity1" 
            android:launchMode="singleTop" >
</activity>

答案 1 :(得分:0)

确保您正在创建myIntent正确并且它不为空(在 else 中您没有做任何事情......)

同时更改以下内容的创建:

myIntent = new Intent(GMapsActivity.this, GMapsActivity1.class);

答案 2 :(得分:0)

 if(password.getText().toString().equals("admin")) {
             System.out.println("Entering");
             mapView.setVisibility(View.VISIBLE);
             login.setVisibility(View.INVISIBLE);
             password.setVisibility(View.INVISIBLE);
             myIntent = new Intent(v.getContext(), GMapsActivity1.class);

            } else {
            }
             startActivity(myIntent);

上面的代码说myIntent仅在密码为admin时才初始化,否则为..但是它的使用甚至没有初始化以启动活动..

答案 3 :(得分:0)

试试这段代码。

Intent myIntent = null;
if(password.getText().toString().equals("admin")) {
System.out.println("Entering");
mapView.setVisibility(View.VISIBLE);
login.setVisibility(View.INVISIBLE);
password.setVisibility(View.INVISIBLE);
myIntent = new Intent(GMapsActivity.this, GMapsActivity1.class);
startActivity(myIntent);
} else {
Toast.makeText(context, "password is invalid",  Toast.LENGTH_SHORT).show();
}

答案 4 :(得分:0)

而不是写

Intent myIntent = null;
myIntent = new Intent(v.getContext(), GMapsActivity1.class);

添加

Intent myIntent = new Intent(getApplicationContext(), GMapsActivity1.class);

Intent myIntent = new Intent(GMapsActivity.this, GMapsActivity1.class);

答案 5 :(得分:0)

你需要在清单文件中添加此活动,否则它将无法启动并且会崩溃,我认为你没有这样做......

 <activity android:name="GMapsActivity1"
           android:launchMode="singleTop"></activity>

答案 6 :(得分:0)

首先放入startActivity(myIntent);在if条件下并确保setContentView(...);在GMapsActivity1活动中正确定义的活动与清单文件中添加的GMapsActivity1一样好。 还有一件事可能是可能的,即你可以使用R.id ....而不是在此活动的layout.xml中定义。

答案 7 :(得分:0)

这是一篇旧帖子,但我找到了一个尚未发布的解决方案。

使用调用活动时,必须使用startActivityForResult进行可能导致您尝试获取调用活动的活动的意图。

示例:

Intent intent = new Intent(MainActivity.this,NextActivity.class); startActivityForResult(intent,123);