-1

I am trying to access the global variable declared in AndroidManifest.xml in UARTlogfragment.java but there is an error in the use of getApplication() i.e. "cannot resolve". I am posting my Androidmanifest.xml and the UARTlogfragment.java here.

The AndroidManifest.xml is:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.NFC" />
    <uses-permission android:name="no.nordicsemi.android.LOG" />

    <uses-feature
        android:name="android.hardware.bluetooth_le"
        android:required="true" />

    <!-- To auto-complete the email text field in the login form with the user's emails -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />

    <application
        android:name=".ToolboxApplication"
        android:allowBackup="true"
        android:fullBackupContent="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name=".SplashscreenActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.Fullscreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.app.shortcuts"
                android:resource="@xml/shortcuts" />

            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />

                <category android:name="android.intent.category.DEFAULT" />

                <data android:mimeType="application/vnd.no.nordicsemi.type.app" />
                <data android:mimeType="application/vnd.no.nordicsemi.type.address" />
            </intent-filter>
        </activity>

        <!-- Template plugin activities -->
        <!-- Remember to add your plug-in Activities to the Android Manifest file. -->


        <!-- Plug-in activities -->
        <activity
            android:name=".FeaturesActivity"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:theme="@style/AppTheme.TranslucentStatusBar" />
        <activity
            android:name=".uart.UARTActivity"
            android:icon="@drawable/ic_uart_feature"
            android:label="@string/uart_feature_title"
            android:launchMode="singleTask"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="no.nordicsemi.android.nrftoolbox.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".proximity.ProximityService"
            android:label="@string/proximity_feature_title" />
        <service
            android:name=".dfu.DfuService"
            android:exported="true"
            android:label="@string/dfu_service_title">
            <intent-filter>
                <action android:name="no.nordicsemi.android.action.DFU_UPLOAD" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </service>

        <activity
            android:name=".dfu.DfuInitiatorActivity"
            android:label="@string/dfu_service_title"
            android:noHistory="true"
            android:theme="@style/AppTheme.Translucent">
            <intent-filter>
                <action android:name="no.nordicsemi.android.action.DFU_UPLOAD" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <service
            android:name=".cgms.CGMService"
            android:label="@string/cgms_feature_title" />
        <service
            android:name=".rsc.RSCService"
            android:label="@string/rsc_feature_title" />
        <service
            android:name=".csc.CSCService"
            android:label="@string/csc_feature_title" />
        <service
            android:name=".hts.HTSService"
            android:label="@string/hts_feature_title" />
        <service
            android:name=".uart.UARTService"
            android:label="@string/uart_feature_title" />
        <service android:name=".wearable.MainWearableListenerService">
            <intent-filter>
                <action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
                <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />

                <data
                    android:host="*"
                    android:pathPrefix="/nrftoolbox"
                    android:scheme="wear" />
            </intent-filter>
        </service>

        <provider
            android:name=".uart.UARTLocalLogContentProvider"
            android:authorities="no.nordicsemi.android.nrftoolbox.uart.log"
            android:exported="true" />

        <activity
            android:name=".Register_Page"
            android:label="@string/title_activity_register__page" />
        <activity android:name=".View_Profile" />
        <activity android:name=".Profile" />
        <activity android:name=".Wallet_Page" />
        <activity android:name=".confirm_booking" />
        <activity android:name=".LoginActivity" />
        <activity android:name=".Add_Amount" />
        <activity android:name=".last_step_book"></activity>
    </application>

The UARTlogfragment.java(where I am trying to access the global variable) is :

public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_feature_uart_log, container, false);
        mydb = new no.nordicsemi.android.nrftoolbox.myDbAdapter(getContext());
        String s = ((ToolboxApplication) this.getApplication()).getSomeVariable();
        Cursor rs = mydb.getData(1);
        rs.moveToFirst();

        String nam = rs.getString(rs.getColumnIndex(no.nordicsemi.android.nrftoolbox.myDbAdapter.CONTACTS_COLUMN_NAME));
        String phon = rs.getString(rs.getColumnIndex(no.nordicsemi.android.nrftoolbox.myDbAdapter.CONTACTS_COLUMN_PHONE));
        String emai = rs.getString(rs.getColumnIndex(no.nordicsemi.android.nrftoolbox.myDbAdapter.CONTACTS_COLUMN_EMAIL));
        String cit = rs.getString(rs.getColumnIndex(no.nordicsemi.android.nrftoolbox.myDbAdapter.CONTACTS_COLUMN_CITY));
        String gend = rs.getString(rs.getColumnIndex(no.nordicsemi.android.nrftoolbox.myDbAdapter.CONTACTS_COLUMN_GENDER));
        String pas = rs.getString(rs.getColumnIndex(no.nordicsemi.android.nrftoolbox.myDbAdapter.CONTACTS_COLUMN_PASS));
        String dobb = rs.getString(rs.getColumnIndex(no.nordicsemi.android.nrftoolbox.myDbAdapter.CONTACTS_COLUMN_DOB));

        if (!rs.isClosed())  {
            rs.close();
        }

        final EditText field = mField = view.findViewById(R.id.field);
        mField.setText(nam+" "+emai+" "+phon+" "+gend+" "+cit);
        field.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEND) {
                onSendClicked();
                return true;
            }
            return false;
        });

        final Button sendButton = mSendButton = view.findViewById(R.id.action_send);
        sendButton.setOnClickListener(v -> onSendClicked());
        return view;
    }

What is the error ?? And why cannot it resolve getApplication()??

2 Answers2

2

Change

String s = ((ToolboxApplication) this.getApplication()).getSomeVariable();

to

String s = ((ToolboxApplication) getActivity().getApplication()).getSomeVariable();
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
2

As you are trying yo use application context from fragment you can not use getApplication() because isn't method of Fragment class
So you first have to use the getActivity() which will return a Fragment Activity to which the fragment is currently associated with.

to sumup in your code,

instead of this.getApplication() you have to use getActivity.getApplication()

Shankar
  • 2,890
  • 3
  • 25
  • 40