用于工作但现在崩溃的应用程序代码

时间:2013-06-18 06:00:29

标签: java android eclipse

Thing是我的应用程序用于完美运行但后来我想更改包名称和类名称,所以我创建了另一个项目并复制粘贴我的代码并在那里进行更改。 eclipse中没有错误,但应用程序崩溃了!!这是我的应用程序的最新消息:

public class LocationMobileUser implements LocationListener {

 Context gContext;
   boolean gps_enabled=false;
   boolean network_enabled=false;

    public LocationMobileUser(Context gContext){
        this.gContext = gContext;

   }

    public static final String URL =
            "http://www.ip2phrase.com/ip2phrase.asp?template=%20%3CISP%3E";

    public void XML (View view) {
        GetXMLTask task = new GetXMLTask();
        task.execute(new String[] { URL });
    }

    public class GetXMLTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            String output = null;
            for (String url : urls) {
                output = getOutputFromUrl(url);
            }
            return output;
        }

        public String getOutputFromUrl(String url) {
            StringBuffer output = new StringBuffer("");
            int x =1;
            try {
                InputStream stream = getHttpConnection(url);
                LineNumberReader lnr = new LineNumberReader(new InputStreamReader(stream));  
                String line = "";
                int lineNo;
                for (lineNo = 1; lineNo < 90; lineNo++) {
                    if (lineNo == x) {
                            line = lnr.readLine();
                            //output.append(line);
                            Pattern p = Pattern.compile(">(.*?)<");
                            Matcher m = p.matcher(line);
                            if (m.find()) {
                                  output.append(m.group(1)); // => "isp"
                                }
                    } else
                            lnr.readLine();
            }

            } catch (IOException e1) {
                e1.printStackTrace();
            }
            return output.toString();
        }


        // Makes HttpURLConnection and returns InputStream
        public InputStream getHttpConnection(String urlString)
                throws IOException {
            InputStream stream = null;
            URL url = new URL(urlString);
            URLConnection connection = url.openConnection();

            try {
                HttpURLConnection httpConnection = (HttpURLConnection) connection;
                httpConnection.setRequestMethod("GET");
                httpConnection.connect();

                if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    stream = httpConnection.getInputStream();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return stream;
        }


    }



    public String getSimOperator() {
        TelephonyManager telephonyManager = (TelephonyManager)gContext.getSystemService(Context.TELEPHONY_SERVICE);
        String operator = telephonyManager.getSimOperatorName();
        return operator;
    } 

   public GsmCellLocation getCellLocation() {
        //String Context=Context.Telephony_SERVICE;
        TelephonyManager telephonyManager = (TelephonyManager)gContext.getSystemService(Context.TELEPHONY_SERVICE);
        GsmCellLocation CellLocation = (GsmCellLocation)telephonyManager.getCellLocation();

        return CellLocation;
    }


    public Location getLocation(){
        String provider = null;
        LocationManager locationManager;
        Location gps_loc=null;
        String context = Context.LOCATION_SERVICE;
        locationManager = (LocationManager)gContext.getSystemService(context);
        gps_loc=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        try{gps_enabled=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
        try{network_enabled=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}
        if(gps_enabled && gps_loc != null){
        provider = LocationManager.GPS_PROVIDER;
        }
        else{
            provider = LocationManager.NETWORK_PROVIDER;
        }
        Location location = locationManager.getLastKnownLocation(provider);
        locationManager.requestLocationUpdates(provider, 20000, 1, this);

        return location; 
    }

    public String getProvider(Location location){
        String provider = null;
        LocationManager locationManager;
        Location gps_loc=null;
        String context = Context.LOCATION_SERVICE;
        locationManager = (LocationManager)gContext.getSystemService(context);
        gps_loc=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        try{gps_enabled=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
        try{network_enabled=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}
        if(gps_enabled && gps_loc != null ){
            provider = LocationManager.GPS_PROVIDER;
            }
        else{
            provider = LocationManager.NETWORK_PROVIDER;
        }
        return provider;
    }


  public String CellLacLocation(GsmCellLocation CellLocation){
       String cidlacString;

       if (CellLocation != null) {
       int cid = CellLocation.getCid();
       int lac = CellLocation.getLac();
       cidlacString = "CellID:" + cid + "\nLAC:" + lac;}
       else {
           cidlacString="No Cell Location Available";
       }
       return cidlacString;

   }

    public String updateWithNewLocation(Location location) {
        String latLongString;

        if (location != null){
            double lat = location.getLatitude();
            double lng = location.getLongitude();
            latLongString = "Latitude:" + lat + "\nLongitude:" + lng;
        }else{
            latLongString = "No Location available";
        }

        return latLongString;
    }

    @Override
    public void onLocationChanged(Location arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub

    }

}

此应用程序将导出到JAR文件中,然后使用构建路径作为库包含到另一个应用程序中;这就是我在这个新应用程序中写的内容:

public class LocationTheApp extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LocationMobileUser LMU = new LocationMobileUser (this);
    Location location = LMU.getLocation();
    GsmCellLocation CellLocation = LMU.getCellLocation();
    String URL = LocationMobileUser.URL;
    LocationMobileUser.GetXMLTask xmlp = LMU.new GetXMLTask();
    String Provider = LMU.getProvider(location);

    String isp = xmlp.getOutputFromUrl(URL);
    String latLongString = LMU.updateWithNewLocation(location);
    String cidlacString = LMU.CellLacLocation(CellLocation);
    String operator = LMU.getSimOperator();



    TextView myLocationText = (TextView)findViewById(R.id.myLocationText);
    myLocationText.setText("Your current GPS position is:\n" + latLongString);

    TextView myprovider = (TextView)findViewById(R.id.myprovider);
    myprovider.setText("\nYour GPS position is provided by:\n" + Provider);

    TextView myCellLocation = (TextView)findViewById(R.id.mycelllocation);
    myCellLocation.setText("\nYour current Cell position is:\n" + cidlacString);

    TextView myOperator = (TextView)findViewById(R.id.myoperator);
    myOperator.setText("\nYour GSM operator is:\n" + operator);

    TextView myispText = (TextView)findViewById(R.id.myispText);
    myispText.setText("\nYour current ISP is:\n" + isp);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

代码没有错误!! 如果它在这里有任何用处是我的清单:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.locationmobileuser"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.locationtheapp.LocationTheApp"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

尝试在locationmobileuser和locationtheapp之间切换清单中的软件包名称,但仍然应用程序崩溃! 正如我所说的那样,曾经像以前的项目中的魅力一样工作! LOG:

06-18 05:36:54.502: E/Trace(992): error opening trace file: No such file or directory (2)
06-18 05:36:54.852: D/dalvikvm(992): newInstance failed: no <init>()
06-18 05:36:54.862: D/AndroidRuntime(992): Shutting down VM
06-18 05:36:54.862: W/dalvikvm(992): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-18 05:36:55.002: E/AndroidRuntime(992): FATAL EXCEPTION: main
06-18 05:36:55.002: E/AndroidRuntime(992): java.lang.RuntimeException: Unable toinstantiate activity     ComponentInfo{com.example.locationmobileuser/com.example.locationmobileuser.LocationMobileUser}: java.lang.InstantiationException: can't instantiate class com.example.locationmobileuser.LocationMobileUser; no empty constructor
06-18 05:36:55.002: E/AndroidRuntime(992):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
06-18 05:36:55.002: E/AndroidRuntime(992):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-18 05:36:55.002: E/AndroidRuntime(992):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-18 05:36:55.002: E/AndroidRuntime(992):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-18 05:36:55.002: E/AndroidRuntime(992):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 05:36:55.002: E/AndroidRuntime(992):  at android.os.Looper.loop(Looper.java:137)
06-18 05:36:55.002: E/AndroidRuntime(992):  at android.app.ActivityThread.main(ActivityThread.java:5041)
06-18 05:36:55.002: E/AndroidRuntime(992):  at java.lang.reflect.Method.invokeNative(Native Method)
06-18 05:36:55.002: E/AndroidRuntime(992):  at java.lang.reflect.Method.invoke(Method.java:511)
06-18 05:36:55.002: E/AndroidRuntime(992):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-18 05:36:55.002: E/AndroidRuntime(992):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-18 05:36:55.002: E/AndroidRuntime(992):  at dalvik.system.NativeStart.main(Native Method)
06-18 05:36:55.002: E/AndroidRuntime(992): Caused by: java.lang.InstantiationException: can't instantiate class com.example.locationmobileuser.LocationMobileUser; no empty constructor
06-18 05:36:55.002: E/AndroidRuntime(992):  at java.lang.Class.newInstanceImpl(Native Method)
06-18 05:36:55.002: E/AndroidRuntime(992):  at java.lang.Class.newInstance(Class.java:1319)
06-18 05:36:55.002: E/AndroidRuntime(992):  at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
06-18 05:36:55.002: E/AndroidRuntime(992):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
06-18 05:36:55.002: E/AndroidRuntime(992):  ... 11 more

任何帮助都会非常感激!!

3 个答案:

答案 0 :(得分:1)

查看清单的包名称

 package="com.example.locationmobileuser

和活动包名称都应该相同更改此包

<activity
        android:name="com.example.locationtheapp.LocationTheApp"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

答案 1 :(得分:0)

您需要在locationmobileuser类中添加空构造函数。

public LocationMobileUser(){
           }

答案 2 :(得分:0)

不需要做所有这些事情,只需转到旧项目并右键单击项目 - &gt;&gt;重构项重命名项目名称或包名称