Unable to call the fragment with imageView in android - java

Today is my first time to be using the fragments. And I was able to make a simple app that utilizes the fragment, however, there is one problem that I cannot understand or hadn't noticed yet. The problem is the app crashes when I add the fragment with the imageView. Everything works fine when I use simple texts or other widgets. I've set the sample code below.
I'm calling the fragment from Activity1 like this.
android.support.v4.app.FragmentManager fragmentManager2 = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction2 = fragmentManager2.beginTransaction();
TestFragment fragment = new TestFragment();
fragmentTransaction2.add(R.id.fragment_container, fragment,"HELLO");
fragmentTransaction2.commit();
This is the layout that fills the fragment
<LinearLayout
android:id="#+id/fragment_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
This is the simple Activity2 class that extends the fragment
public class Activity2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/** Inflating the layout for this fragment **/
View v = inflater.inflate(R.layout.fragment_class2, null);
return v;
}
}
this is the layout for the class that extends the fragment .
Ps: The imageview in here causes the error, however, the settings in the fragment class may be the problem. I need to display the imageView.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/imaew2"
android:src="#drawable/playimage2"
/>
</LinearLayout>
These are the error I get and I'm not sure how these errors mean, however the error is pointing to the Fragment
         
 
 at com.trymeagain.Activity2.onCreateView(Activity2.java:13)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
Added new Error
05-07 17:56:20.701 3173-3173/com.trymeagain E/art﹕ Throwing OutOfMemoryError "Failed to allocate a 74649612 byte allocation with 1048576 free bytes and 63MB until OOM"
05-07 17:56:20.703 3173-3173/com.trymeagain D/skia﹕ --- allocation failed for scaled bitmap
05-07 17:56:20.704 3173-3173/com.trymeagain D/AndroidRuntime﹕ Shutting down VM
05-07 17:56:20.704 3173-3173/com.trymeagain E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.trymeagain, PID: 3173
android.view.InflateException: Binary XML file line #13: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.trymeagain.PlusOneFragment.onCreateView(Activity2.java:13)

you getting the exception in your imageview where your image playimage2 is too large and the heap memory is not enough to allocate ...
solution-
case1 : if size need to increase on higher resolution device
if playimage2 is possible to patch-then try to reduce the size of the image..you can use tool like photoshop to do so.and then patch the image
case2 : if size need to be constant as written in xml 50dp*50dp
Just try to reduce the size of the image

Related

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>

Error: inflating class com.google.android.material.navigation.NavigationView in androidX

My app working above API level 26 or higher but stoped working bellow API level 26. Please help me..
Why does XML returns error in inflating class, and stoped working in lower API level?
I tried these methods but it did not work:
Check your code for drawable and color resources used inside the com.google.android.material.navigation.NavigationView you've used, probably in activity_main.xml, if you're using the default Navigation Drawer template code from Android Studio.
Check that the drawable files are in res/drawable folder, not in res/drawable-v21.
Check if you've used android:backgroundTint() or android:src or similar inside your NavigationView. Since they don't work below android API Level 21, use app:backgroundTint or app:srcCompat instead.
...
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bozorgan/com.example.bozorgan.MainActivity}: android.view.InflateException: Binary XML file line #14: Binary XML file line #14: Error inflating class com.google.android.material.navigation.NavigationView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:223)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7223)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.view.InflateException: Binary XML file line #14: Binary XML file line #14: Error inflating class com.google.android.material.navigation.NavigationView
at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
at android.view.LayoutInflater.inflate(LayoutInflater.java:380)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
at com.example.bozorgan.MainActivity.onCreate(MainActivity.java:63)
at android.app.Activity.performCreate(Activity.java:6877)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
at android.app.ActivityThread.access$1100(ActivityThread.java:223) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:7223) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class com.google.android.material.navigation.NavigationView
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
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:layout_gravity="start"
android:fitsSystemWindows="true"
android:id="#+id/navigation_drawer">
<include layout="#layout/activity_main"/>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/navigation_view"
android:layoutDirection="rtl"
app:headerLayout="#layout/navigation_header"
android:layout_gravity="end"
app:menu="#menu/menu" />
</androidx.drawerlayout.widget.DrawerLayout>
Be sure to add the Drawerlayout library to the build.gradle(:app) file so you can use Drawerlayout.
If not, open the build.gradle(:app) file and add:
implementation "androidx.drawerlayout: drawerlayout: 1.1.1"
in AndroidX NavigationView requires these dependencies and also check out Documentation for more Information here
// Java language implementation
implementation "androidx.navigation:navigation-fragment:2.3.2"
implementation "androidx.navigation:navigation-ui:2.3.2"
// Kotlin
implementation "androidx.navigation:navigation-fragment-ktx:2.3.2"
implementation "androidx.navigation:navigation-ui-ktx:2.3.2"

AndroidStudio - two identical (same sourcecode) apps - one works, second doesnt - app closes after starting

after setting up a new development environment and just wanted to get started, I have two simple Android Apps with just an easy, normal ImageView. They have both the same sourcecode (title is different) and amazingly the first one works, the second doesnt.
The second app gets closed instantly after beeing started. The first one works and shows the image.
As the problem I could identify the ImageView, if I delete the element, the app is starting fine.
Maybe someone can give me a tip or can help. Thank you very much!
AndroidStudio 3.6 -
Device for testing: Huawei Android 6.0
MainActivity.java:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
public void fade (View view){
ImageView imageView = findViewById(R.id.imageView);
imageView.animate().alpha(0).setDuration(2000);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml:
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:onClick="fade"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/bart" />
</androidx.constraintlayout.widget.ConstraintLayout>
Logcat:
05-12 23:29:44.274 14433-14433/? I/art: Late-enabling -Xcheck:jni
05-12 23:29:44.356 14433-14450/com.example.testdrei E/HAL: load: id=gralloc != hmi->id=gralloc
05-12 23:29:44.373 14433-14433/com.example.testdrei W/System: ClassLoader referenced unknown path: /data/app/com.example.testdrei-2/lib/arm64
05-12 23:29:44.401 14433-14433/com.example.testdrei I/HwCust: Constructor found for class android.app.HwCustHwWallpaperManagerImpl
05-12 23:29:44.412 14433-14433/com.example.testdrei W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
05-12 23:29:44.477 14433-14433/com.example.testdrei I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
05-12 23:29:44.477 14433-14433/com.example.testdrei I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
05-12 23:29:44.559 14433-14433/com.example.testdrei W/ResourceType: Failure getting entry for 0x7f060061 (t=5 e=97) (error -75)
05-12 23:29:44.559 14433-14433/com.example.testdrei W/ResourceType: Failure getting entry for 0x7f060061 (t=5 e=97) (error -75)
05-12 23:29:44.592 14433-14433/com.example.testdrei I/Process: Sending signal. PID: 14433 SIG: 9
The ImageView with ID R.id.imageView3 is not a part of your layout. So the below line will cause an exception
ImageView imageView = findViewById(R.id.imageView3);
Change it with the correct ID
ImageView imageView = findViewById(R.id.imageView);
Try this first and lets see if the error is coming from the xml file
<ImageView
android:id="#+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
app:srcCompat="#drawable/bart" />

Cannot show TabLayout in InputView

I'm creating an Android input method, and want to use TabLayout from the support library in the InputView (the UI where the user inputs text in the form of keyclicks).
The layout for the InputView is very simple:
<?xml version="1.0" encoding="utf-8"?>
<org.szm.enigma2.ime.InputView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabLayout android:id="#+id/tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_alignParentTop="true"
style="#style/Theme.AppCompat">
<android.support.design.widget.TabItem android:text="Hello"/>
<android.support.design.widget.TabItem android:text="World"/>
<android.support.design.widget.TabItem android:text="Enigma"/>
</android.support.design.widget.TabLayout>
</org.szm.enigma2.ime.InputView>
And I use the same layout for both InputView and the settings activity, which is a normal activity.
Create InputView from subclass of InputMethodService:
return getLayoutInflator().inflat(R.layout.input_view, null);
Inside the constructor of settings activity class:
setContentView(R.layout.input_view);
When I run the program, the settings activity is normal. Then tab layout is working perfectly. But when I activate the input method's input view, the program crashes with the following info:
09-08 18:56:49.064 28437-28437/org.szm.enigma2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: org.szm.enigma2, PID: 28437
android.view.InflateException: Binary XML file line #6: Binary XML file line #6: Error inflating class android.support.design.widget.TabLayout
Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class android.support.design.widget.TabLayout
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.view.LayoutInflater.createView(LayoutInflater.java:652)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:812)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:752)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:883)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:846)
at android.view.LayoutInflater.inflate(LayoutInflater.java:522)
at android.view.LayoutInflater.inflate(LayoutInflater.java:430)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at org.szm.enigma2.ime.InputService.onCreateInputView(InputService.java:42)
at android.inputmethodservice.InputMethodService.updateInputViewShown(InputMethodService.java:1228)
at android.inputmethodservice.InputMethodService.showWindowInner(InputMethodService.java:1622)
at android.inputmethodservice.InputMethodService.showWindow(InputMethodService.java:1590)
at android.inputmethodservice.InputMethodService$InputMethodImpl.showSoftInput(InputMethodService.java:442)
at android.inputmethodservice.IInputMethodWrapper.executeMessage(IInputMethodWrapper.java:206)
at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:37)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6175)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
Caused by: java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:33)
at android.support.design.widget.TabLayout.<init>(TabLayout.java:297)
at android.support.design.widget.TabLayout.<init>(TabLayout.java:291)
at java.lang.reflect.Constructor.newInstance0(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:430) 
at android.view.LayoutInflater.createView(LayoutInflater.java:652) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:812) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:752) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:883) 
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:846) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:522) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:430) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:377) 
at org.szm.enigma2.ime.InputService.onCreateInputView(InputService.java:42) 
at android.inputmethodservice.InputMethodService.updateInputViewShown(InputMethodService.java:1228) 
at android.inputmethodservice.InputMethodService.showWindowInner(InputMethodService.java:1622) 
at android.inputmethodservice.InputMethodService.showWindow(InputMethodService.java:1590) 
at android.inputmethodservice.InputMethodService$InputMethodImpl.showSoftInput(InputMethodService.java:442) 
at android.inputmethodservice.IInputMethodWrapper.executeMessage(IInputMethodWrapper.java:206) 
at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:37) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6175) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764) 
09-08 18:56:49.081 28437-28437/? I/Process: Sending signal. PID: 28437 SIG: 9
I don't know why it didn't work even if I set the style attribute. And how can I use tab layout in an input view? (since input view is not inside any activity)
Thanks
EDIT
I have already checked this question and googled other resources. Most of them is dealing with error of inflating TabLayout in Activity. My problem is that TabLayout works fine inside Activity but not in input view.
you can do it like this:
Context context = new ContextThemeWrapper(this, android.support.v7.appcompat.R.style.Theme_AppCompat_Light_NoActionBar);
LayoutInflater inflater = LayoutInflater.from(context);
view = (View) inflater.inflate(R.layout.XXXXX, null);
the Tablayout is in the R.layout.XXXXX

android app crashes when redirecting to new activity

I am totally new with android, i created an app with basic activity and then added a button to direct to another activity, it worked but whenever i click the button to go to the new activity page the app get crashed
am getting the following error:
10/02 15:22:04: Launching app
$ adb push C:\Users\Ahmed\AndroidStudioProjects\Shiftind\app\build\outputs\apk\app-debug.apk /data/local/tmp/com.shiftind.www.shiftind
$ adb shell pm install -r "/data/local/tmp/com.shiftind.www.shiftind"
Success
$ adb shell am start -n "com.shiftind.www.shiftind/com.shiftind.www.shiftind.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Connected to process 2284 on device emulator-5554
W/System: ClassLoader referenced unknown path: /data/app/com.shiftind.www.shiftind-2/lib/x86
I/InstantRun: Instant Run Runtime started. Android package is com.shiftind.www.shiftind, real application class is null.
W/System: ClassLoader referenced unknown path: /data/app/com.shiftind.www.shiftind-2/lib/x86
W/art: Verification of android.support.v4.media.session.MediaControllerCompat android.support.v4.app.FragmentActivity.getSupportMediaController() took 420.734ms
W/art: Verification of void android.support.v4.app.FragmentActivity.requestPermissionsFromFragment(android.support.v4.app.Fragment, java.lang.String[], int) took 107.527ms
W/art: Verification of void android.support.v4.app.FragmentActivity.setEnterSharedElementCallback(android.support.v4.app.SharedElementCallback) took 102.953ms
W/art: Verification of void android.support.v7.app.AppCompatActivity.onSupportActionModeFinished(android.support.v7.view.ActionMode) took 192.620ms
W/art: Verification of void android.support.v4.app.FragmentManagerImpl.setHWLayerAnimListenerIfAlpha(android.view.View, android.view.animation.Animation) took 130.794ms
W/art: Verification of android.support.v7.app.AppCompatDelegate android.support.v7.app.AppCompatDelegate.create(android.content.Context, android.view.Window, android.support.v7.app.AppCompatCallback) took 147.296ms
W/art: Verification of void android.support.v7.app.AppCompatDelegateImplBase.<init>(android.content.Context, android.view.Window, android.support.v7.app.AppCompatCallback) took 138.730ms
W/art: Verification of void android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor() took 166.867ms
W/art: Verification of void android.support.v7.app.AppCompatDelegateImplV9.onConfigurationChanged(android.content.res.Configuration) took 215.247ms
W/art: Verification of android.support.v7.view.ActionMode android.support.v7.app.AppCompatDelegateImplV9.startSupportActionModeFromWindow(android.support.v7.view.ActionMode$Callback) took 113.775ms
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
W/gralloc_ranchu: Gralloc pipe failed
[ 10-02 15:22:33.523 2284: 2284 D/ ]
HostConnection::get() New Host Connection established 0xa72c2540, tid 2284
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
I/Choreographer: Skipped 40 frames! The application may be doing too much work on its main thread.
D/AndroidRuntime: Shutting down VM
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.shiftind.www.shiftind, PID: 2284
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22260)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5610) 
at android.view.View$PerformClick.run(View.java:22260) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.shiftind.www.shiftind/com.shiftind.www.shiftind.registration}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1805)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1523)
at android.app.Activity.startActivityForResult(Activity.java:4224)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
at android.app.Activity.startActivityForResult(Activity.java:4183)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:856)
at android.app.Activity.startActivity(Activity.java:4507)
at android.app.Activity.startActivity(Activity.java:4475)
at com.shiftind.www.shiftind.MainActivity.onButtonClick(MainActivity.java:19)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:5610) 
at android.view.View$PerformClick.run(View.java:22260) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
Application terminated.
my mainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.shiftind.www.shiftind.MainActivity">
<Button
android:text="Let's Get Started"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/startbutton"
style="#style/Widget.AppCompat.Button.Colored"
android:onClick="onButtonClick"
android:layout_alignParentStart="true"
android:layout_marginStart="85dp" />
</RelativeLayout>
my registartion.xml (second activity)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="registration activity"
android:layout_width="wrap_content"
android:layout_height="111dp"
android:id="#+id/textView3"
android:layout_weight="1"
android:textStyle="normal|bold" />
</LinearLayout>
my registration.java
package com.shiftind.www.shiftind;
import android.app.Activity;
import android.os.Bundle;
/**
* Created by Ahmed on 10/2/2016.
*/
public class registration extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registration);
}
}
my main activity.java
package com.shiftind.www.shiftind;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtonClick(View view) {
if (view.getId() == R.id.startbutton) {
Intent i = new Intent(MainActivity.this, registration.class);
startActivity(i);
}
}
}
Declare the second Activity in your manifest file and you should be sorted out
Also your button is not declared
Add this line inside <application> tag
<activity android:name=".registration" ></activity>
hope it helps

Categories