2011-09-21 117 views
0

我的Android應用程序不斷崩潰,但沒有任何錯誤。 繼承人的代碼。爲什麼我的Android應用崩潰?

LocationActivity.java 修訂

package com.marakana.tutomaps; 

import android.app.TabActivity; 
import android.content.Intent; 
import android.content.res.Resources; 
import android.os.Bundle; 
import android.widget.TabHost; 

public class LocationActivity extends TabActivity { 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Resources res = getResources(); // Resource object to get Drawables 
    TabHost tabHost = getTabHost(); // The activity TabHost 
    TabHost.TabSpec spec1, spec2, spec3; // Resusable TabSpec for each tab 
    Intent intent1, intent2, intent3; // Reusable Intent for each tab 

    // Create an Intent to launch an Activity for the tab (to be reused) 
    intent1 = new Intent().setClass(this, HaffActivity.class); 

    // Initialize a TabSpec for each tab and add it to the TabHost 
    spec1 = tabHost.newTabSpec("Haff").setIndicator("Haff", 
         res.getDrawable(R.drawable.ic_tab_haff)) 
        .setContent(intent1); 
    tabHost.addTab(spec1); 

    // Do the same for the other tabs 
    intent2 = new Intent().setClass(this, MapsActivity.class); 
    spec2 = tabHost.newTabSpec("Maps").setIndicator("Maps", 
         res.getDrawable(R.drawable.ic_tab_maps)) 
        .setContent(intent2); 
    tabHost.addTab(spec2); 

    intent3 = new Intent().setClass(this, ProfileActivity.class); 
    spec3 = tabHost.newTabSpec("Profile").setIndicator("Profile", 
         res.getDrawable(R.drawable.ic_tab_profile)) 
        .setContent(intent3); 
    tabHost.addTab(spec3); 

    tabHost.setCurrentTab(2); 
} 
} 

這裏是主要的佈局文件。 修訂 * main.xml中 *

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout android:orientation="vertical" 
     android:layout_width="fill_parent"  android:layout_height="fill_parent"> 
     <View android:layout_width="fill_parent" android:layout_height="0.5dip" 
      android:background="#000" /> 
     <TabWidget android:id="@android:id/tabs" 
      android:layout_width="fill_parent" android:layout_height="wrap_content" 
      android:layout_marginLeft="0dip" android:layout_marginRight="0dip" /> 
     <View android:layout_width="fill_parent" android:layout_height="2dip" 
      android:background="#696969" /> 
     <View android:layout_width="fill_parent" android:layout_height="2dip" 
      android:background="#000" /> 
     <FrameLayout android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" android:layout_height="fill_parent" /> 
    </LinearLayout> 
</TabHost> 

繼承人修訂

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.marakana.tutomaps" 
    android:versionCode="1" 
    android:versionName="1.0"> 
<uses-sdk android:minSdkVersion="4" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".LocationActivity" 
       android:label="@string/app_name"> 
     <intent-filter> 
        <uses-library android:name="com.google.android.maps" /> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

<activity android:name=".HaffActivity"> 
</activity> 

<activity android:name=".MapsActivity"> 
</activity> 

<activity android:name=".ProfileActivity"> 
</activity> 

</application> 

MapsActivity.java清單文件

package com.marakana.tutomaps; 

    import java.io.IOException; 
    import java.util.List; 

    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.util.Log; 
    import android.widget.TextView; 

    import com.google.android.maps.GeoPoint; 
    import com.google.android.maps.MapActivity; 
    import com.google.android.maps.MapController; 
    import com.google.android.maps.MapView; 

    public class MapsActivity extends MapActivity implements LocationListener { 

private static final String TAG = "MapsActivity"; 

LocationManager locationManager; 
Geocoder geocoder; 
TextView locationText; 
MapView map;  
MapController mapController; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    locationText = (TextView)this.findViewById(R.id.lblLocationInfo); 
    map = (MapView)this.findViewById(R.id.mapview); 
    map.setBuiltInZoomControls(true); 

    mapController = map.getController(); 
    mapController.setZoom(16); 

    locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE); 

    geocoder = new Geocoder(this); 

    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    if (location != null) { 
     Log.d(TAG, location.toString()); 
     this.onLocationChanged(location); 
    } 
} 

@Override 
protected void onPause() { 
    // TODO Auto-generated method stub 
    super.onPause(); 
    locationManager.removeUpdates(this); 
} 

@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this); 
} 

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

@Override 
public void onLocationChanged(Location location) { 
    // TODO Auto-generated method stub 
    Log.d(TAG, "onLocationChanged with location " + location.toString()); 
    // Displays lat, long, altitude and bearing 
    String text = String.format("Lat:\t %f\nLong:\t %f\nAlt:\t %f\nBearing:\t %f", location.getLatitude(), location.getLongitude(), location.getAltitude(), location.getBearing()); 
    this.locationText.setText(text); 

    try { 
     // This gets a list of addresses 
     List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10); 
     for (Address address : addresses) { 
      this.locationText.append("\n" + address.getAddressLine(0)); 
     } 

     // Convert latitude and longitude into int that the GeoPoint constructor can understand 
     int latitude = (int)(location.getLatitude() * 1000000); 
     int longitude = (int)(location.getLongitude() * 1000000); 

     GeoPoint point = new GeoPoint(latitude,longitude); 
     mapController.animateTo(point); 

    } catch (IOException e) { 
     Log.e("LocateMe", "Could not get Geocoder data", e); 
    } 
} 

@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 

} 
    } 

請幫助我。

+0

把你的DDMS logcat放在這裏 –

+0

當你的應用程序崩潰時發佈logcat ........ –

+0

嘗試使用Debug配置(Eclipse)運行並在Logcat窗口(Debug透視圖)上看到。 – adatapost

回答

0

我認爲你做了一個錯誤...... 你逝去的佈局值,而不是意圖..

只是用我檢查你的代碼..你逝去的R.layout.hafflayout,而不是意圖 in setContent(intent);

更新

TabHost.TabSpec spec1,spec2,spec3; // Resusable TabSpec for each tab 
Intent intent1,intent2,intent3; 
intent1 = new Intent().setClass(this, HaffActivity.class); 

// Initialize a TabSpec for each tab and add it to the TabHost 
spec1 = tabHost.newTabSpec("Haff").setIndicator("Haff", 
        res.getDrawable(R.drawable.ic_tab_haff)) 
       .setContent(intent1); 
tabHost.addTab(spec); 

// Do the same for the other tabs 
intent2 = new Intent().setClass(this, MapsActivity.class); 
spec2 = tabHost.newTabSpec("Maps").setIndicator("Maps", 
        res.getDrawable(R.drawable.ic_tab_maps)) 
       .setContent(intent2); 
tabHost.addTab(spec); 

intent3 = new Intent().setClass(this, ProfileActivity.class); 
spec3 = tabHost.newTabSpec("Profile").setIndicator("Profile", 
        res.getDrawable(R.drawable.ic_tab_profile)) 
       .setContent(intent3); 
tabHost.addTab(spec); 

更新:

你沒有加入之前活動的名點()在menifest.xml

<activity android:name=".HaffActivity"> 
</activity> 

<activity android:name=".MapsActivity"> 
</activity> 

<activity android:name=".ProfileActivity"> 
</activity> 

更新...最新

在您的MapsActivity.class中,您setContentView(R.layout.main);所以請更改它併爲MapActivity添加您的xml

+0

更改爲意圖,仍然不起作用 –

+0

您是否更改過所有規格? –

+0

yeap他們都看起來像這個名字的execept。 –

0

你有沒有添加到您的AndroidManifest.xml

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".LoadingActivity" 
       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="HaffActivity" 
    </activity> 

    <activity android:name="MapsActivity" 
    </activity> 

    <activity android:name="ProfileActivity" 
    </activity> 

</application> 

因爲如果你在你的項目有不同的活動,則必須在清單中添加他們。

+0

加入了你現在告訴我的清單: –

+0

添加了清單,看看它的okey。 –

+0

我編輯我的答案您的清單現在看起來像這樣嗎?而當你運行你的應用程序現在它的工作? @downvoter:爲什麼downvote? – mthpvg

0

嘗試清理項目。另外,您是否在使用maps的谷歌庫?您需要添加一個鏈接到Google API。

+0

我在清單中得到了這個,這是足夠的嗎?:

+0

你在你的Android項目中使用了哪些庫?你可以嘗試單獨執行該Activity,發佈logcat? – Dayerman

+0

即時通訊使用Android 1.6和谷歌地圖API我不知道你的意思,當我運行應用程序時,我得到的唯一錯誤是:09-21 13:09:57.200:ERROR/dalvikvm(2618 ):找不到類com.marakana.tutomaps.MapsActivity,從方法com.marakana.tutomaps.LocationActivity.onCreate引用 –

0

您是否在您的代碼中實現了MapsActivity?我只能看到LocationActivity。

+0

你是指執行? –

相關問題