Android Studio:无法解析getApplication()

时间:2017-11-30 07:30:10

标签: java android sqlite

我正在尝试访问UARTlogfragment.java中AndroidManifest.xml中声明的全局变量,但使用getApplication()时出现错误,即“无法解析”。 我在这里发布我的Androidmanifest.xml和UARTlogfragment.java。

AndroidManifest.xml是:

<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>

UARTlogfragment.java(我试图访问全局变量的地方)是:

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

错误是什么?为什么它不能解决getApplication() ??

2 个答案:

答案 0 :(得分:1)

更改

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

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

答案 1 :(得分:1)

当您尝试使用片段中的应用程序上下文时,您无法使用getApplication(),因为Fragment类的isn&t方法
因此,您首先必须使用getActivity(),它将返回片段当前与之关联的片段活动。

总结你的代码,

而不是this.getApplication()您必须使用getActivity.getApplication()