2016-01-18 28 views
0

我有當我測試它在我的手機會立即打破了,我得到這個錯誤的Android應用程序的一個問題:android.app.Application無法投射Yamba應用程序?

"java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.massine.final_pgm/com.example.massine.final_pgm.BaseActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to com.example.massine.final_pgm.YambaApplication" 

我不知道哪裏出了問題來了,我讀了類似的帖子,但我不能修復它:這是我的Java類和我的清單:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.massine.final_pgm"> 


    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 

     <activity android:name="com.example.massine.final_pgm.BaseActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN"/> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

<uses-permission android:name="android.permission.INTERNET" /> 
</manifest> 

和我的java類:

package com.example.massine.final_pgm; 

import android.app.Application; 
import android.content.ContentValues; 
import android.content.SharedPreferences; 
import android.content.SharedPreferences.OnSharedPreferenceChangeListener; 
import android.preference.PreferenceManager; 
import android.text.TextUtils; 
import android.util.Log; 
import java.util.List; 

import winterwell.jtwitter.Twitter; 
import winterwell.jtwitter.Twitter.Status; 

public class YambaApplication extends Application implements OnSharedPreferenceChangeListener { 
    private static final String TAG = YambaApplication.class.getSimpleName(); 
    public Twitter twitter; 
    private SharedPreferences prefs; 
    private boolean serviceRunning; 
    private StatusData statusData; 


    @Override 
    public void onCreate() { 
    super.onCreate(); 
    this.prefs = PreferenceManager.getDefaultSharedPreferences(this); 
    this.prefs.registerOnSharedPreferenceChangeListener(this); 
    this.statusData = new StatusData(this); 
    Log.i(TAG, "onCreated"); 
    } 

    @Override 
    public void onTerminate() { 
    super.onTerminate(); 
    Log.i(TAG, "onTerminated"); 
    } 

    // Returns the Twitter object 
    public synchronized Twitter getTwitter() { 
    if (this.twitter == null) { 
     String username = this.prefs.getString("username", null); 
     String password = this.prefs.getString("password", null); 
     String apiRoot = prefs.getString("apiRoot", "http://yamba.newcircle.com/api"); 
     if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password) 
      && !TextUtils.isEmpty(apiRoot)) { 
     this.twitter = new Twitter(username, password); 
     this.twitter.setAPIRootUrl(apiRoot); 
     } 
    } 
    return this.twitter; 
    } 

    // Part of being OnSharedPreferenceChangeListener 
    public synchronized void onSharedPreferenceChanged( 
     SharedPreferences sharedPreferences, String key) { 
    this.twitter = null; 
    } 

    public boolean isServiceRunning() { 
    return serviceRunning; 
    } 

    public void setServiceRunning(boolean serviceRunning) { 
    this.serviceRunning = serviceRunning; 
    } 

    public StatusData getStatusData() { 
    return statusData; 
    } 

    public SharedPreferences getPrefs() { 
    return prefs; 
    } 

    // Connects to the online service and puts the latest statuses into DB. 
    // Returns the count of new statuses 
    public synchronized int fetchStatusUpdates() { 
    Log.d(TAG, "Fetching status updates"); 
    Twitter twitter = this.getTwitter(); 
    if (twitter == null) { 
     Log.d(TAG, "Twitter connection info not initialized"); 
     return 0; 
    } 
    try { 
     List<Status> statusUpdates = twitter.getFriendsTimeline(); 
     long latestStatusCreatedAtTime = this.getStatusData() 
      .getLatestStatusCreatedAtTime(); 
     int count = 0; 
     ContentValues values = new ContentValues(); 
     for (Status status : statusUpdates) { 
     values.put(StatusData.C_ID, status.getId()); 
     long createdAt = status.getCreatedAt().getTime(); 
     values.put(StatusData.C_CREATED_AT, createdAt); 
     values.put(StatusData.C_TEXT, status.getText()); 
     values.put(StatusData.C_USER, status.getUser().getName()); 
     Log.d(TAG, "Got update with id " + status.getId() + ". Saving"); 
     this.getStatusData().insertOrIgnore(values); 
     if (latestStatusCreatedAtTime < createdAt) { 
      count++; 
     } 
     } 
     Log.d(TAG, count > 0 ? "Got " + count + " status updates" 
      : "No new status updates"); 
     return count; 
    } catch (RuntimeException e) { 
     Log.e(TAG, "Failed to fetch status updates", e); 
     return 0; 
    } 
    } 
} 

問題的路線是:

yamba = ((YambaApplication) getApplication()); 

謝謝大家。

回答

1

您需要在清單文件的應用程序元素上指定android:name

android:name="com.example.massine.final_pgm.YambaApplication"

沒有它的代碼運行,但不使用YambaApplication類,但是這是造成ClassCastException異常的默認應用程序。

編輯澄清基於評論:

<application 
    android:name="com.example.massine.final_pgm.YambaApplication" 
    ...> 
+0

嗨,但是當我做,我得到這個錯誤com.example.massine.final_pgm.YambaApplication是不能分配給android.app.activity。 驗證Android XML文件中的資源引用? – Masshat

+0

您確定您已將其添加到應用程序中,而不是您的清單文件中的活動,如我的編輯中所示? –

+0

<活動機器人:名稱= 「com.example.massine.final_pgm.YambaApplication」 機器人:allowBackup = 「真」 機器人:圖標= 「@ mip映射/ ic_launcher」 機器人:標籤= 「@串/ APP_NAME」 機器人:supportsRtl =「true」 android:theme =「@ style/AppTheme」> – Masshat

相關問題