繁体   English   中英

Eclipse-Android应用在启动时崩溃?

[英]Eclipse - Android App Crashes on Startup?

我目前正在使用eclipse为android进行开发,最近遇到了一个问题,即我的应用程序不再启动,打开时崩溃了。

我认为问题可能出在布局文件或activity_main文件中。


MainActivity类:

package com.dd.splash;


import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;
import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;


public class myMainScreen extends Activity 
implements OnClickListener {

 private LocationManager locationMangaer=null;
 private LocationListener locationListener=null; 

 private Button btnGetLocation = null;
 private EditText editLocation = null; 
 private ProgressBar pb =null;

 private static final String TAG = "Debug";
 private Boolean flag = false;

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


  //if you want to lock screen for always Portrait mode  
  setRequestedOrientation(ActivityInfo
  .SCREEN_ORIENTATION_PORTRAIT);

  pb = (ProgressBar) findViewById(R.id.progressBar1);
  pb.setVisibility(View.INVISIBLE);

  //This part
  editLocation = (EditText) findViewById(R.id.editTextLocation); 

  btnGetLocation = (Button) findViewById(R.id.btnLocation);
  btnGetLocation.setOnClickListener(this);

  locationMangaer = (LocationManager) 
  getSystemService(Context.LOCATION_SERVICE);

 }

 @Override
 public void onClick(View v) {
  flag = displayGpsStatus();
  if (flag) {

   Log.v(TAG, "onClick");  
   //setContentView(R.layout.map);
   editLocation.setText("Please wait while we locate you...");

   pb.setVisibility(View.VISIBLE);
   locationListener = new MyLocationListener();

   locationMangaer.requestLocationUpdates(LocationManager
   .GPS_PROVIDER, 5000, 10,locationListener);

   } else {
   alertbox("Gps Status", "Your GPS is: OFF");
  }

 }

 /*----Method to Check GPS is enable or disable ----- */
 private Boolean displayGpsStatus() {
  ContentResolver contentResolver = getBaseContext()
  .getContentResolver();
  boolean gpsStatus = Settings.Secure
  .isLocationProviderEnabled(contentResolver, 
  LocationManager.GPS_PROVIDER);
  if (gpsStatus) {
   return true;

  } else {
   return false;
  }
 }

 /*----------Method to create an AlertBox ------------- */
 protected void alertbox(String title, String mymessage) {
  AlertDialog.Builder builder = new AlertDialog.Builder(this);
  builder.setMessage("Your Device's GPS is Disabled!")
  .setCancelable(false)
  .setTitle("** Gps Status **")
  .setPositiveButton("Gps On",
   new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int id) {
   // finish the current activity
   // AlertBoxAdvance.this.finish();
   Intent myIntent = new Intent(
   Settings.ACTION_SECURITY_SETTINGS);
   startActivity(myIntent);
      dialog.cancel();
   }
   })
   .setNegativeButton("Cancel",
   new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int id) {
    // cancel the dialog box
    dialog.cancel();
    }
   });
  AlertDialog alert = builder.create();
  alert.show();
 }

 /*----------Listener class to get coordinates ------------- */
 private class MyLocationListener implements LocationListener {
        @Override
        public void onLocationChanged(Location loc) {

            editLocation.setText("");
            pb.setVisibility(View.INVISIBLE);
            Toast.makeText(getBaseContext(),"Location changed : Lat: " +
   loc.getLatitude()+ " Lng: " + loc.getLongitude(),
   Toast.LENGTH_SHORT).show();
            String longitude = "Longitude: " +loc.getLongitude();  
      Log.v(TAG, longitude);
      String latitude = "Latitude: " +loc.getLatitude();
      Log.v(TAG, latitude);

    /*----------to get City-Name from coordinates ------------- */
      String cityName=null;   
      String countryName=null;
      String stateName=null;
      //Bundle extraInfo=null;
      Geocoder gcd = new Geocoder(getBaseContext(), 
   Locale.getDefault());             
      List<Address>  addresses;  
      try {  
      addresses = gcd.getFromLocation(loc.getLatitude(), loc
   .getLongitude(), 1);  
      if (addresses.size() > 0)  
         System.out.println(addresses.get(0).getLocality());  
         cityName=addresses.get(0).getLocality();
         countryName=addresses.get(0).getCountryName();
         stateName=addresses.get(0).getAdminArea();
         //extraInfo=addresses.get(0).getExtras();



        } catch (IOException e) {            
        e.printStackTrace();  
      } 

      //String extraInfoHolder = "(Coming in next update! Check our facebook page for updates)";
      String s = longitude+"\n"+latitude +
   "\n\nYou are currently near "+cityName+", "+stateName+" ("+countryName+")";


           editLocation.setText(s);

        // Get a handle to the Map Fragment
           GoogleMap map = ((MapFragment) getFragmentManager()
                   .findFragmentById(R.id.map)).getMap();

           LatLng mylocation = new LatLng(-33.867, 151.206);

           map.setMyLocationEnabled(true);
           map.moveCamera(CameraUpdateFactory.newLatLngZoom(mylocation, 13));

           map.addMarker(new MarkerOptions()
                   .title("You")
                   .snippet("You are here")
                   .position(mylocation)
                   .alpha(0.7f)
                   .flat(true)
                   //.icon(BitmapDescriptorFactory.fromAsset(marker.jpeg))

                   );

        }



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

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

        @Override
        public void onStatusChanged(String provider, 
  int status, Bundle extras) {
            // TODO Auto-generated method stub         
        }
    }
    }

MainActivity.XML布局文件:

<fragment
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:name="com.google.android.gms.maps.MapFragment"/>


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

<LinearLayout
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:weightSum="1">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.20"
        android:gravity="center"
        android:text="@string/locator_title"
        android:textSize="20sp" />


 <EditText
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_weight="0.33"
  android:id="@+id/editTextLocation"
  android:editable="false">
     <requestFocus></requestFocus>
 </EditText>





</LinearLayout>
<LinearLayout 
android:id="@+id/layButtonH" 
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_weight="0.15">

<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/location_button"
android:id="@+id/btnLocation"></Button>
</LinearLayout>
<LinearLayout android:id="@+id/layloadingH"
 android:layout_height="wrap_content"
 android:layout_weight="0.20"
 android:layout_width="fill_parent"
 android:gravity="center">
 <br/>
 <br/>
 <ProgressBar 
 android:layout_width="wrap_content"
 android:id="@+id/progressBar1"
 android:layout_height="wrap_content" >
 </ProgressBar>

</LinearLayout>

错误日志:

12-07 22:39:20.991: D/AndroidRuntime(29272): Shutting down VM
12-07 22:39:20.991: W/dalvikvm(29272): threadid=1: thread exiting with uncaught exception (group=0x4157ab90)
12-07 22:39:21.001: E/AndroidRuntime(29272): FATAL EXCEPTION: main
12-07 22:39:21.001: E/AndroidRuntime(29272): Process: com.dd.splash, PID: 29272
12-07 22:39:21.001: E/AndroidRuntime(29272): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dd.splash/com.dd.splash.myMainScreen}: android.view.InflateException: Binary XML file line #72: Error inflating class br
12-07 22:39:21.001: E/AndroidRuntime(29272):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at android.app.ActivityThread.access$700(ActivityThread.java:135)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at android.os.Handler.dispatchMessage(Handler.java:102)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at android.os.Looper.loop(Looper.java:137)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at android.app.ActivityThread.main(ActivityThread.java:4998)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at java.lang.reflect.Method.invokeNative(Native Method)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at java.lang.reflect.Method.invoke(Method.java:515)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at dalvik.system.NativeStart.main(Native Method)
12-07 22:39:21.001: E/AndroidRuntime(29272): Caused by: android.view.InflateException: Binary XML file line #72: Error inflating class br
12-07 22:39:21.001: E/AndroidRuntime(29272):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at android.app.Activity.setContentView(Activity.java:1928)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at com.dd.splash.myMainScreen.onCreate(myMainScreen.java:49)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at android.app.Activity.performCreate(Activity.java:5243)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
12-07 22:39:21.001: E/AndroidRuntime(29272):    ... 11 more
12-07 22:39:21.001: E/AndroidRuntime(29272): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.br" on path: DexPathList[[zip file "/data/app/com.dd.splash-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.dd.splash-1, /vendor/lib, /system/lib]]
12-07 22:39:21.001: E/AndroidRuntime(29272):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at android.view.LayoutInflater.createView(LayoutInflater.java:559)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:652)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
12-07 22:39:21.001: E/AndroidRuntime(29272):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
12-07 22:39:21.001: E/AndroidRuntime(29272):    ... 22 more


非常感谢您的帮助,我已经搜索了几天,无法找到问题所在。

我的猜测是,LinearLayout中的<br/>语句似乎不起作用。 尝试删除它们。

要获得我想要的效果,请尝试在ProgressBar设置android:paddingTop="10dp"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM