【问题标题】:How to properly call mainactivity from library module on app module如何从应用程序模块上的库模块正确调用 mainactivity
【发布时间】:2019-01-06 05:05:12
【问题描述】:

每次我从应用程序模块调用库模块上的活动时,应用程序都会崩溃

应用模块上有一个按钮,当按下它时,它会从库模块中调用一个活动,但每次我这样做时,应用都会崩溃

以下是应用程序模块上的代码,其中意图将调用库模块上的主要活动

 //Audio Activity
    audioButton = (ImageView) findViewById(R.id.IV_main_audio);
    audioButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            Intent intentLoadNewActivity = new 
    Intent("com.danielkim.soundrecorder.MainActivity");
            startActivity(intentLoadNewActivity);
        }

    });

这是应用模块上的 android 清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.kiminonawa.mydiary">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<uses-feature android:name="android.hardware.location.network" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-feature
    android:name="android.hardware.telephony"
    android:required="false" />

<application
    android:name=".shared.MyDiaryApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="false"
    android:theme="@style/AppTheme"
    tools:replace="android:icon">

    <!-- Add this key for search the place name -->
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="AIzaSyDVL96wY3-Xqe8Rjt065Low6hMCOjW-Qd4" />



    <activity
        android:name=".init.InitActivity"
        android:screenOrientation="portrait"
        android:theme="@style/Theme_NoActionBar_FullScreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".main.MainActivity"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan" />
    <activity
        android:name=".contacts.ContactsActivity"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan" />
    <activity
        android:name=".entries.DiaryActivity"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan" />
    <activity
        android:name=".entries.photo.PhotoOverviewActivity"
        android:screenOrientation="portrait"
        android:theme="@style/Theme_NoActionBar_FullScreen" />
    <activity
        android:name=".entries.photo.PhotoDetailViewerActivity"
        android:screenOrientation="portrait" />
    <activity
        android:name=".memo.MemoActivity"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan" />
    <activity
        android:name=".setting.SettingActivity"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan" />
    <activity
        android:name=".main.AboutActivity"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan" />
    <activity
        android:name=".security.PasswordActivity"
        android:screenOrientation="portrait" />
    <activity
        android:name=".backup.BackupActivity"
        android:screenOrientation="portrait" />

    <!-- Ucrop -->
    <activity
        android:name="com.yalantis.ucrop.UCropActivity"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar" />

    <!-- Start For NoNonsense-FilePicker -->
    <activity
        android:name=".backup.DirectoryPickerActivity"
        android:theme="@style/FilePickerTheme">
        <intent-filter>
            <action android:name="android.intent.action.GET_CONTENT" />

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

    <!-- This is for Android N -->
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true"
        tools:replace="android:authorities">
        <meta-data
        tools:replace="android:resource"
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/nnf_provider_paths" />
    </provider>
    <!-- End For NoNonsense-FilePicker -->


    <activity android:name=".main.CollageFragment"></activity>

</application>

【问题讨论】:

  • 与其在标题中写下已解决并在问题正文中解释您所做的事情,不如做出自己的答案,并将您的答案标记为最佳答案。

标签: android android-studio


【解决方案1】:
Intent i=new Intent(PresentActivity.this, NextActivity.class);
startActivity(i);

【讨论】:

  • 如何区分我尝试调用的活动。因为应用模块和库模块都有 MainActivity
  • 我这样做了,但它仍然崩溃 Intent i=new Intent(MainActivity.this, com.danielkim.soundrecorder.activities.MainActivity.class); startActivity(i);
  • 你使用的是哪个库?
  • 这是 logcat 崩溃报告 imgur.com/a/FECU0xL 我将 com.danielkim.soundrecorder 从应用程序模块转换为库模块,这就是我将其用作库的原因。
【解决方案2】:
audioButton = (ImageView) findViewById(R.id.IV_main_audio);
audioButton.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
        try{
            // try another constructor
            Intent intentLoadNewActivity = new Intent(v.getContext(), "com.danielkim.soundrecorder.MainActivity");
            // add optional flag FLAG_ACTIVITY_NEW_TASK if necessary.
            // intentLoadNewActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intentLoadNewActivity);
        } catch (Exception e) {
             Log.e("TAG",e);// if still not work, please show us the log.
        }
    }
});

【讨论】:

  • 在哪里可以看到日志??抱歉,我是使用 android studio 的新手
  • @DatsiK77 按底部工具栏上的alt + 6Logcat。见developer.android.com/studio/debug/am-logcat
  • 我看不到出了什么问题,请将过滤器设置为 logcat 平面右上角的only selected application。并输入您登录中顶部输入平面时使用的TAG
【解决方案3】:

我的错,反射可以用来从库模块启动你的活动:

Class.forName("com.mypackage.MainActivity")

try {
    Intent myIntent = new 
    Intent(this,Class.forName("com.danielkim.soundrecorder.MainActivity"));
    startActivity(myIntent);

    } catch (ClassNotFoundException e) {
     e.printStackTrace();
  }

【讨论】:

  • 我将在应用模块或库模块上添加这个??
【解决方案4】:

(代表问题作者发布解决方案)

经过一番搜索,我已经解决了。我只是在应用模块和库模块上有一个重复的文件名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 2018-08-01
    • 1970-01-01
    • 2017-12-10
    • 1970-01-01
    • 2018-09-06
    • 1970-01-01
    相关资源
    最近更新 更多