我有外部包类(我的自定义库),我想包含在项目中。它使用Activity
,但会显示错误。
如果我将此命令放在主项目类中,它运行良好......该怎么办?
以下是代码:
package com.__MyDefaultLibrary;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.ScaleAnimation;
import android.widget.*;
import java.io.File;
import java.util.Calendar;
//
import android.app.Activity;
import com.oceanesa.samplevideorecorder.R;
public class __MyDefaultFunctions{
public android.app.Activity actv1 = android.app.Activity;
public void Initt(){
PreferenceManager.setDefaultValues(actv1.getBaseContext(), R.xml.mypreferences, false);
}
// ========== MY CUSTOM LIBRARY =============//
//button find
public Button fvb(int id) {
return (Button) actv1.findViewById(id);
}
//message show
public void msg(String text) {
Toast.makeText(actv1.getApplicationContext(), text, Toast.LENGTH_LONG).show();
}
public View.OnClickListener optionsListener2 = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(actv1.getBaseContext(), com.__MyDefaultLibrary.__MyDefaultPreferencesInit1.class);
actv1.startActivity(i);
}
};
}
和AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.oceanesa.samplevideorecorder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature
android:name="android.hardware.camera.front"
android:required="false" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:theme="@style/AppTheme"
>
<activity
android:name="com.oceanesa.samplevideorecorder.VideoCaptureExample"
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="com.__MyDefaultLibrary.__MyDefaultPreferencesInit1"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:1)
更新答案只是将上下文传递给类
public class MyClass {
private static Context mContext;
private static Activity mActivity;
public MyClass(Context c) {
mContext= c;
}
public MyClass(Activity act) {
mActivity= act;
}
public static void showToastMethod() {
// then passing context or activity to
Toast.makeText(mContext, "mymessage ", Toast.LENGTH_SHORT).show();
// or Toast.makeText(mActivity, "mymessage ", Toast.LENGTH_SHORT).show();
}
}
答案 1 :(得分:0)
最后,我在主题的帮助下开始工作: How to display a Toast message in from a class that doesn't extend Activity
问题出现在我的代码中。根本不需要致电andorid.app
。我没有将 Activity
传递给我的外部文件,我无法调用任何内容..
我必须传递这样的活动(来自主项目文件):
public MyExternalClass myEx = new MyExternalClass(Activity);
需要一些修改...