Balaraju M
Balaraju M

Reputation: 493

Intent issue in android app from java to kotlin

09-22 11:14:21.804 13211-13211/com.example.balarajum.kotlin E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.balarajum.kotlin, PID: 13211
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.balarajum.kotlin/com.example.balarajum.kotlin.Main2Activity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2348)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2410)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5345)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.                                                                               
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742) 
09-22 11:14:21.804 507-910/? W/ActivityManager:   Force finishing activity 1 com.example.balarajum.kotlin/.Main2Activity

Here is my simple Java Activity. I am passing my Java intent to Kotlin activity.Build is ok no errors and app is also launching when I click button too Kotlin intent the app is crashing

public class MainActivity extends Activity {
    private Button btn1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button) findViewById(R.id.java_btn);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,Main2Activity.class));
            }
        });
    }
}

Here is my Kotlin intent activity

class Main2Activity : AppCompatActivity() {
    override
    protected fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)

        var btn = findViewById(R.id.kotlin_btn);
        btn.setOnClickListener {
            startActivity(Intent(this@Main2Activity, MainActivity::class.java))
        }
    }
}

Upvotes: 1

Views: 931

Answers (1)

user8612828
user8612828

Reputation:

You have to use Appcompat Theme in your style. Make sure you have the theme like this.

   <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

Also, check your manifest, you have the correct theme.

Try AppcompatActiviy instead of Activity.Change the code like this.

public class MainActivity extends AppcompatActiviy

Hope it helps:)

Upvotes: 1

Related Questions