0

I have external package class (my custom library), which I want to include in the project. It uses Activity, but shows error as you see.

enter image description here

If I place this command in main project class, it works well... What to do?

Here is the code:

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);
        }
    };



}

And 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>
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
T.Todua
  • 53,146
  • 19
  • 236
  • 237

2 Answers2

1

Update Answer just passing context to class

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();
  }

}
Mina Fawzy
  • 20,852
  • 17
  • 133
  • 156
  • Thanks,but I cant understand.. `Activity` is class of `andoid.app` package. And I my library, I have not any activity class.. (whats more, nothing like `ActivityName` at all). In my main project file, I have `class XYZ extends Activty`. – T.Todua Jan 16 '17 at 12:18
  • p.s. in both files, it imports successfulyy, but in the above given example file, it shows `unused import statement`. – T.Todua Jan 16 '17 at 12:24
  • 1
    so what you trying to do in this line android.app.Activity actv1 = android.app.Activity ?? – Mina Fawzy Jan 16 '17 at 12:43
  • At first, thanks for answer....... in that external file (lets call it `MyFunctions.java`) I have this code: `Toast.makeText(actv1.getApplicationContext(), "Helooo", Toast.LENGTH_LONG).show();` and want to make it work.. so, i wanted to set `actv1` because, without classname, `getApplicationContext()` returns errors.. – T.Todua Jan 16 '17 at 13:06
  • 1
    just replace this code actv1.getApplicationContext() with context it will do the same job – Mina Fawzy Jan 16 '17 at 14:03
  • yes , I've searched how to pass Activity to externally imported library class and it worked! If you update answer, I'll mark it. – T.Todua Jan 16 '17 at 17:40
0

Finally I got it working with the help of topic: How to display a Toast message in from a class that doesn't extend Activity

Problem was in my code. There was no need to call andorid.app at all. I didnt pass Activity to my external file, from where I couldnt call anything..

I had to pass activity like this (from main project file):

public MyExternalClass myEx = new MyExternalClass(Activity);

and needed some modifications...

Community
  • 1
  • 1
T.Todua
  • 53,146
  • 19
  • 236
  • 237