2011-12-18 60 views
0

我正在尝试将开源应用程序集成到我的android应用程序中。我已经将开源应用程序作为库应用程序,并将xml集成到我的Android清单文件中。没有编译错误。android集成库应用程序

第一个屏幕是为图书馆应用的登录画面,当它被称为是在投掷的Java类郎异常错误:

m_app = (TodoApplication) getApplication(); 

loginscreen.java的源代码:

public class LoginScreen extends Activity { 

final static String TAG = LoginScreen.class.getSimpleName(); 

private TodoApplication m_app; 
private Button m_LoginButton; 
private BroadcastReceiver m_broadcastReceiver; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.login); 

    m_app = (TodoApplication) getApplication(); 

    // supposed to help with the banding on the green background 



    findViewById(R.id.loginbackground).getBackground().setDither(true); 

    IntentFilter intentFilter = new IntentFilter(); 
    intentFilter.addAction("com.todotxt.todotxttouch.ACTION_LOGIN"); 
    m_broadcastReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      Intent i = new Intent(context, TodoTxtTouch.class); 
      startActivity(i); 
      finish(); 
     } 
    }; 
    registerReceiver(m_broadcastReceiver, intentFilter); 

    m_LoginButton = (Button) findViewById(R.id.login); 
    m_LoginButton.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      login(); 
     } 
    }); 

    //final RemoteClient remoteClient = m_app.getRemoteClientManager() 
     // .getRemoteClient(); 
    //if (remoteClient.isAuthenticated()) { 



     switchToTodolist(); 
    //} 
} 

private void switchToTodolist() { 
    Intent intent = new Intent(this, TodoTxtTouch.class); 
    startActivity(intent); 
    finish(); 
} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    unregisterReceiver(m_broadcastReceiver); 
} 


void login() { 
    final RemoteClient client = m_app.getRemoteClientManager() 
      .getRemoteClient(); 

    if (!client.isAvailable()) { 
     Log.d(TAG, "Remote service " + client.getClass().getSimpleName() 
       + " is not available; aborting login"); 
     Util.showToastLong(m_app, R.string.toast_login_notconnected); 
    } else { 
     RemoteLoginTask loginTask = client.getLoginTask(); 
     loginTask.showLoginDialog(this); 
    } 
} 

} 

android manifest.xml中的集成库代码:

<activity android:name="com.todotxt.todotxttouch.LoginScreen" android:label="@string/app_label" 
     android:theme="@android:style/Theme.NoTitleBar" 
     android:configChanges="keyboardHidden|orientation">  
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
      <category android:name="com.todotxt.todotxttouch.category.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" />    
     </intent-filter> 


    </activity> 

    <activity android:name="com.todotxt.todotxttouch.TodoApplication" /> 

    <activity android:name="com.todotxt.todotxttouch.Filter" android:label="Filter" 
     android:theme="@android:style/Theme.NoTitleBar" /> 
    <activity android:name="com.todotxt.todotxttouch.Preferences" android:label="@string/set_preferences" /> 
    <activity android:name="com.todotxt.todotxttouch.AddTask" android:label="@string/addtask" 
     android:theme="@android:style/Theme.NoTitleBar" 
     android:configChanges="orientation|keyboardHidden" 
     android:windowSoftInputMode="adjustResize"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
     </intent-filter> 
    </activity> 

    <activity-alias android:name="com.todotxt.todotxttouch.AddTaskShortcut" 
     android:targetActivity="com.todotxt.todotxttouch.AddTask" android:label="@string/shortcut_addtask_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.CREATE_SHORTCUT" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity-alias> 
    <activity-alias android:name="com.todotxt.todotxttouch.AddTaskShare" 
     android:targetActivity="com.todotxt.todotxttouch.AddTask" android:label="@string/share_addtask_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.SEND" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <data android:mimeType="text/plain" /> 
     </intent-filter> 
    </activity-alias> 
    <activity android:name="com.todotxt.todotxttouch.HelpActivity" 
     android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" /> 
    <activity android:name="com.todotxt.todotxttouch.TodoTxtTouch" android:theme="@android:style/Theme.NoTitleBar" 
     android:configChanges="keyboardHidden|orientation"> 
     <intent-filter> 
      <action android:name="android.intent.action.SEARCH" /> 
     </intent-filter> 
     <meta-data android:name="android.app.searchable" 
      android:resource="@xml/searchable" /> 
    </activity> 

任何人都可以请帮我在理解问题。

让我进一步解释:我有一个名为Todoapplication.java文件....因此类存在......它LoginScreen.java被称为

m_app = (TodoApplication) getApplication(); 

,这是我在哪里获得java lang类异常?

回答

0

Activity.getApplication()返回在<application>元素的清单中声明的​​应用程序类的实例。我没有看到你的粘贴清单中。

这是不够的简单应用程序类在您的应用程序。它必须明确指定为清单中的一个。

0

我也许弄错了棍子的末端,所以我请求编程的神提前饶恕。

假设你在Eclipse中开发,这不是一个简单的例子,在Eclipse中使用开源的源代码在项目属性中有一个选项isLibrary打勾。

在您的项目属性中,您可以添加一个库,Eclipse将列出开源源码(以及其他任何已经检查过“isLibrary”的源码)。你会不会简单地选择开源项目并添加它?你的项目会添加库并重新编译?

要访问开源项目,现在是一个库,可以使用“导入”语句来访问任何公开的公开方法。

这个使用开源库项目的设置过程的一个很好的例子是Actionbar Sherlock,我写了一个教程youtube,它可视化地演示了我刚写的东西。它可以在http://www.youtube.com/watch?v=avcp6eD_X2k