简体   繁体   中英

Problem in opening an Activity from Fragment Activity

I have created a Fragment view in my Navigation Drawer and I want to call an Activity from that Fragment view using an ImageView for that I have written this java code in My Fragment View class But I don't know why whenever I click on my image the app gets crashed please help out

package com.example.gamesense.ui.Maps;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.example.gamesense.R;

public class MapsFragment extends Fragment {
    private MapsViewModel mapsViewModel;
    private ImageView erangle_image;
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        mapsViewModel =
                new ViewModelProvider(this).get(MapsViewModel.class);
            View root = inflater.inflate(R.layout.fragment_maps, container, false);

            //on click listener for image

            erangle_image = root.findViewById(R.id.erangle_image);
            erangle_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              startActivity(new Intent(getActivity(),ErangleMap.class));
            }
        });

        return root;
    }

}

And here is the XML for Fragment View and from ImageView I want to open new Activity

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="20dp"
    android:paddingTop="20dp"
    android:paddingEnd="20dp"
    android:background="@drawable/background"
    android:paddingBottom="20dp"
    android:paddingStart="20dp"
    android:paddingRight="20dp"
    android:orientation="vertical"
    tools:context=".ui.Maps.MapsFragment">

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="vertical"
       android:id="@+id/erangle"
       android:background="@drawable/shape">

      <ImageView
          android:layout_width="fill_parent"
          android:layout_height="200dp"
          android:scaleType="fitXY"
          android:id="@+id/erangle_image"
          android:src="@drawable/erangel"/>

      <TextView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="ERANGLE:"
          android:textAlignment="viewStart"
          android:background="#C59507"
          android:textSize="24sp"
          android:textColor="#FFFFFF"
          android:paddingTop="7dp"
          android:paddingLeft="15dp"
          android:textStyle="bold" />

      <TextView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="#C59507"
          android:paddingBottom="10dp"
          android:text="Erangel is the first 8x8 km playable map created for PUBG."
          android:textAlignment="textStart"
          android:paddingLeft="15dp"
          android:textColor="#FFFFFF"
          android:textSize="18sp" />
   </LinearLayout>
</androidx.appcompat.widget.LinearLayoutCompat>

Logcat

2021-03-23 09:48:47.556 10047-10047/com.example.gamesense E/Parcel: Reading a NULL string not supported here.
2021-03-23 09:48:47.586 10047-10086/com.example.gamesense E/libEGL: Invalid file path for libcolorx-loader.so
2021-03-23 09:48:47.596 10047-10047/com.example.gamesense D/AndroidRuntime: Shutting down VM
2021-03-23 09:48:47.597 10047-10047/com.example.gamesense E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.gamesense, PID: 10047
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gamesense/com.example.gamesense.ui.Maps.ErangleMap}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3699)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3866)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2289)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:254)
        at android.app.ActivityThread.main(ActivityThread.java:8202)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1006)
     Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:843)
        at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:806)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:693)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
        at com.example.gamesense.ui.Maps.ErangleMap.onCreate(ErangleMap.java:14)
        at android.app.Activity.performCreate(Activity.java:8146)
        at android.app.Activity.performCreate(Activity.java:8130)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1310)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3668)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3866) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2289) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:254) 
        at android.app.ActivityThread.main(ActivityThread.java:8202) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1006) 
2021-03-23 09:48:47.623 10047-10047/com.example.gamesense I/Process: Sending signal. PID: 10047 SIG: 92021-03-23 09:48:47.556 10047-10047/com.example.gamesense E/Parcel: Reading a NULL string not supported here.
2021-03-23 09:48:47.586 10047-10086/com.example.gamesense E/libEGL: Invalid file path for libcolorx-loader.so
2021-03-23 09:48:47.596 10047-10047/com.example.gamesense D/AndroidRuntime: Shutting down VM
2021-03-23 09:48:47.597 10047-10047/com.example.gamesense E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.gamesense, PID: 10047
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gamesense/com.example.gamesense.ui.Maps.ErangleMap}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3699)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3866)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2289)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:254)
        at android.app.ActivityThread.main(ActivityThread.java:8202)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1006)
     Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:843)
        at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:806)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:693)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
        at com.example.gamesense.ui.Maps.ErangleMap.onCreate(ErangleMap.java:14)
        at android.app.Activity.performCreate(Activity.java:8146)
        at android.app.Activity.performCreate(Activity.java:8130)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1310)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3668)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3866) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2289) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:254) 
        at android.app.ActivityThread.main(ActivityThread.java:8202) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1006) 
2021-03-23 09:48:47.623 10047-10047/com.example.gamesense I/Process: Sending signal. PID: 10047 SIG: 9```

it says "You need to use a Theme.AppCompat theme (or descendant) with this activity." maybe you edited/removed the theme by mistake. check your manifest

have you declared this activity in your AndroidManifest.xml? check this one Check your manifest first and declare your activity

Add or modify existing theme with AppCompat theme in your styles.xml . like the following:

<style name="yourThemeName" parent="@style/Theme.AppCompat....">
    //.... items will go here
</style>

And you can use this theme as global theme

<application
    android:theme="@style/yourThemeName">

OR

For specific activity

<activity
    android:name=".ErangleMap"
    android:theme="@style/yourThemeName">
    //.... 
</activity>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM