I'm new to the Android SDK as well as Android Studio, but I'm familiar with programming. I have two activities that should be linked together, but for whatever reason my app crashes on launch due to an error in one of my XML files in regards to a TextView object I created. I have no idea what's going on from there. Any help is appreciated!
I'm 90% sure this file is the culprit. I can upload more code on request if you think that it is necessary. Below is the error.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.txfbla.benjamin.fblattire.LoginActivity">
<TextView android:id="#+id/login_usernameHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/username"
android:textSize="18sp"
android:textColor="#string/login_blue"/>
<EditText android:id="#+id/login_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textAutoComplete" />
<TextView android:id="#+id/login_passwordHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/password"
android:textSize="18sp"
android:textColor="#string/login_blue"/>
<EditText android:id="#+id/login_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"/>
<Button android:id="#+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/login"
android:textSize="16sp"
android:onClick="logIn"/>
</LinearLayout>
package com.txfbla.benjamin.fblattire;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class LoginActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
public void logIn(View view){
Intent intent = new Intent(this, HomePageActivity.class);
EditText editText = (EditText) findViewById(R.id.login_username);
String username = editText.getText().toString();
intent.putExtra("username", username);
startActivity(intent);
}
}
Here's the activity I'm trying to load:
package com.txfbla.benjamin.fblattire;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class HomePageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
Here's the error I'm getting:
01-09 02:16:15.144 25368-25368/com.txfbla.benjamin.fblattire E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.txfbla.benjamin.fblattire, PID: 25368
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.txfbla.benjamin.fblattire/com.txfbla.benjamin.fblattire.LoginActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class TextView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class TextView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
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 android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.txfbla.benjamin.fblattire.LoginActivity.onCreate(LoginActivity.java:14)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.content.res.Resources$NotFoundException: File #3677A1 from drawable resource ID #0x7f060018: .xml extension required
at android.content.res.Resources.loadColorStateList(Resources.java:2549)
at android.content.res.TypedArray.getColorStateList(TypedArray.java:427)
at android.widget.TextView.<init>(TextView.java:987)
at android.widget.TextView.<init>(TextView.java:629)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:60)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:56)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:92)
at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:938)
at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:992)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
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 android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.txfbla.benjamin.fblattire.LoginActivity.onCreate(LoginActivity.java:14)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
You can't set textColor from a string resource. You can either hard code the value like #FFFFFF or save the value in colors.xml file
colors.xml
<color name="white">#FFFFFF</color>
Change
<TextView android:id="#+id/login_usernameHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/username"
android:textSize="18sp"
android:textColor="#string/login_blue "/>
to
<TextView android:id="#+id/login_usernameHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/username"
android:textSize="18sp"
android:textColor="#color/white" />
Similarly change the second TextView textColor to color resource.
I was facing similar error but reason was different. If you are trying to add TextView for Activity with name Splash it will give similar error.
Related
This question already has answers here:
How to use VectorDrawables in Android API lower than 21?
(13 answers)
Closed 5 years ago.
So some days ago I was finally done with my first android application. When I was developing the app, I used my Sony Xperia Z (Android 5.1.1, API 22) as a test phone. So far everything worked fine. Untill I built the app on a Samsung galaxy J1 (Android 4.4.4, API 19).
minSdkVersion 19
targetSdkVersion 25
buildToolsVersion "25.0.3"
For some reason I get this error when running the app on the Samsung phone:
01-30 20:00:14.569 11914-11914/com.heli.minvekeplan E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.heli.minvekeplan, PID: 11914
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.heli.minvekeplan/com.heli.minvekeplan.ToolbarActivity}: android.view.InflateException: Binary XML file line #24: Error inflating class Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access$800(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class Button
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:720)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:762)
at android.view.LayoutInflater.inflate(LayoutInflater.java:499)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.heli.minvekeplan.ToolbarActivity.onCreate(ToolbarActivity.java:24)
at android.app.Activity.performCreate(Activity.java:5442)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access$800(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ic_settings_grey_24dp.xml from drawable resource ID #0x7f020074
at android.content.res.Resources.loadDrawable(Resources.java:3451)
at android.content.res.TypedArray.getDrawable(TypedArray.java:614)
at android.view.View.<init>(View.java:3585)
at android.widget.TextView.<init>(TextView.java:696)
at android.widget.Button.<init>(Button.java:107)
at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:66)
at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:62)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:109)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1029)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1087)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:762)
at android.view.LayoutInflater.inflate(LayoutInflater.java:499)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.heli.minvekeplan.ToolbarActivity.onCreate(ToolbarActivity.java:24)
at android.app.Activity.performCreate(Activity.java:5442)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access$800(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #1: invalid drawable tag vector
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:969)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:913)
at android.content.res.Resources.loadDrawable(Resources.java:3447)
at android.content.res.TypedArray.getDrawable(TypedArray.java:614)
at android.view.View.<init>(View.java:3585)
at android.widget.TextView.<init>(TextView.java:696)
at android.widget.Button.<init>(Button.java:107)
at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:66)
at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:62)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:109)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1029)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1087)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:762)
at android.view.LayoutInflater.inflate(LayoutInflater.java:499)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.heli.minvekeplan.ToolbarActivity.onCreate(ToolbarActivity.java:24)
at android.app.Activity.performCreate(Activity.java:5442)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access$800(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
So I searched on the internet and found these answers: answer1, answer2, but none of them seems to work. I think it has something with the drawables and my buttons that uses my drawables to do, but I have no idea how to fix this. Thank you for helping!
My XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.heli.minvekeplan.ToolbarActivity">
<com.heli.minvekeplan.CustomViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/masterviewpager">
</com.heli.minvekeplan.CustomViewPager>
<FrameLayout android:layout_width="match_parent"
android:layout_marginTop="539dp"
android:layout_height="match_parent">
<View android:layout_width="match_parent"
android:layout_height="5dp"
android:background="#drawable/toolbar_dropshadow"/>
</FrameLayout>
<Button
android:id="#+id/settings1"
android:layout_width="38dp"
android:layout_height="38dp"
android:background="#drawable/ic_settings_grey_24dp"
android:layout_marginTop="544dp"
android:layout_marginStart="270dp"/>
<Button
android:id="#+id/timeplan1"
android:layout_width="38dp"
android:layout_height="38dp"
android:background="#drawable/ic_assignment_grey_24dp"
android:layout_marginStart="45dp"
android:layout_marginTop="544dp" />
<Button
android:id="#+id/lekse1"
android:layout_width="38dp"
android:layout_height="38dp"
android:background="#drawable/ic_edit_blue_24dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="540dp"/>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:background="#color/colorToolbar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" >
</android.support.v7.widget.Toolbar>
<TextView
android:id="#+id/settingstext"
android:layout_width="60dp"
android:layout_height="15dp"
android:layout_marginTop="578dp"
android:layout_marginStart="260dp"
android:text="Innstillingar"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="10sp" />
<TextView
android:id="#+id/lekseplantext"
android:layout_width="60dp"
android:layout_height="15dp"
android:layout_marginTop="578dp"
android:layout_centerHorizontal="true"
android:text="Lekseplan"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="10sp" />
<TextView
android:id="#+id/timeplantext"
android:layout_width="60dp"
android:layout_height="15dp"
android:layout_marginTop="578dp"
android:layout_marginStart="35dp"
android:text="Timeplan"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="10sp" />
My Activity:
package com.heli.minvekeplan;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ToolbarActivity extends AppCompatActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
final CustomViewPager viewPager = (CustomViewPager) findViewById(R.id.masterviewpager);
viewPager.setPagingEnabled(false);
viewPager.setAdapter(mSectionsPagerAdapter);
final TextView settingstext = (TextView) findViewById(R.id.settingstext);
final TextView leksetext = (TextView) findViewById(R.id.lekseplantext);
final TextView timetext = (TextView) findViewById(R.id.timeplantext);
leksetext.setTextColor(Color.parseColor("#FF1FBA97"));
settingstext.setTextColor(Color.parseColor("#FF5C5C5C"));
timetext.setTextColor(Color.parseColor("#FF5C5C5C"));
final Button time = (Button) findViewById(R.id.timeplan1);
final Button settings = (Button) findViewById(R.id.settings1);
final Button lekse = (Button) findViewById(R.id.lekse1);
SharedPreferences startt = this.getSharedPreferences("start", Context.MODE_PRIVATE);
final String Start = startt.getString("start", "");
if (Start.equals("settings")) {
viewPager.setCurrentItem(2);
settings.setBackgroundResource(R.drawable.ic_settings_blue_24dp);
settingstext.setTextColor(Color.parseColor("#FF1FBA97"));
lekse.setBackgroundResource(R.drawable.ic_edit_grey_24dp);
leksetext.setTextColor(Color.parseColor("#FF5C5C5C"));
SharedPreferences.Editor editor = startt.edit();
editor.putString("start", "RobloxGamer47");
editor.apply();
}
else {
viewPager.setCurrentItem(1);
}
lekse.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
lekse.setBackgroundResource(R.drawable.ic_edit_blue_24dp);
leksetext.setTextColor(Color.parseColor("#FF1FBA97"));
time.setBackgroundResource(R.drawable.ic_assignment_grey_24dp);
timetext.setTextColor(Color.parseColor("#FF5C5C5C"));
settings.setBackgroundResource(R.drawable.ic_settings_grey_24dp);
settingstext.setTextColor(Color.parseColor("#FF5C5C5C"));
viewPager.setCurrentItem(1);
}
});
settings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
settings.setBackgroundResource(R.drawable.ic_settings_blue_24dp);
settingstext.setTextColor(Color.parseColor("#FF1FBA97"));
lekse.setBackgroundResource(R.drawable.ic_edit_grey_24dp);
leksetext.setTextColor(Color.parseColor("#FF5C5C5C"));
time.setBackgroundResource(R.drawable.ic_assignment_grey_24dp);
timetext.setTextColor(Color.parseColor("#FF5C5C5C"));
viewPager.setCurrentItem(2);
}
});
time.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
time.setBackgroundResource(R.drawable.ic_assignment_blue_24dp);
timetext.setTextColor(Color.parseColor("#FF1FBA97"));
lekse.setBackgroundResource(R.drawable.ic_edit_grey_24dp);
leksetext.setTextColor(Color.parseColor("#FF5C5C5C"));
settings.setBackgroundResource(R.drawable.ic_settings_grey_24dp);
settingstext.setTextColor(Color.parseColor("#FF5C5C5C"));
viewPager.setCurrentItem(0);
}
});
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TimeActivity timeActivity = new TimeActivity();
return timeActivity;
case 1:
LekseActivity lekseActivity = new LekseActivity();
return lekseActivity;
case 2:
SettingsActivity settingsActivity = new SettingsActivity();
return settingsActivity;
default:
return null;
}
}
#Override
public int getCount() {
return 3;
}
}
}
Drawable example:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="38dp"
android:height="38dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#1fba97"
android:pathData="M19.43,12.98c0.04,-0.32 0.07,-0.64 0.07,-0.98s-0.03,-0.66 -0.07,-0.98l2.11,-1.65c0.19,-0.15 0.24,-0.42 0.12,-0.64l-2,-3.46c-0.12,-0.22 -0.39,-0.3 -0.61,-0.22l-2.49,1c-0.52,-0.4 -1.08,-0.73 -1.69,-0.98l-0.38,-2.65C14.46,2.18 14.25,2 14,2h-4c-0.25,0 -0.46,0.18 -0.49,0.42l-0.38,2.65c-0.61,0.25 -1.17,0.59 -1.69,0.98l-2.49,-1c-0.23,-0.09 -0.49,0 -0.61,0.22l-2,3.46c-0.13,0.22 -0.07,0.49 0.12,0.64l2.11,1.65c-0.04,0.32 -0.07,0.65 -0.07,0.98s0.03,0.66 0.07,0.98l-2.11,1.65c-0.19,0.15 -0.24,0.42 -0.12,0.64l2,3.46c0.12,0.22 0.39,0.3 0.61,0.22l2.49,-1c0.52,0.4 1.08,0.73 1.69,0.98l0.38,2.65c0.03,0.24 0.24,0.42 0.49,0.42h4c0.25,0 0.46,-0.18 0.49,-0.42l0.38,-2.65c0.61,-0.25 1.17,-0.59 1.69,-0.98l2.49,1c0.23,0.09 0.49,0 0.61,-0.22l2,-3.46c0.12,-0.22 0.07,-0.49 -0.12,-0.64l-2.11,-1.65zM12,15.5c-1.93,0 -3.5,-1.57 -3.5,-3.5s1.57,-3.5 3.5,-3.5 3.5,1.57 3.5,3.5 -1.57,3.5 -3.5,3.5z"/>
Thank you
You're using a Vector Drawable as the background for your button.
Android API 21+ has 100% full support for this. So your app works fine when running on a device that is using API 22.
To use vector drawables below API 21, you need to add a few things to your java/gradle code, and then it'll work perfectly fine.
See this guide:
https://developer.android.com/guide/topics/graphics/vector-drawable-resources.html#vector-drawables-backward-solution
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I tried to add data to sqlite, but when I clicked Add Button error appear and my app closed. I don't understand, so many wasted hours for looking the solution by myself..
here is my java code, AddSampleActivity.java
package com.example.mangan;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class AddSampleActivity extends AppCompatActivity {
DatabaseHelper myDb;
EditText editNama, editJarak, editRating , editHarga;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_sample);
editNama = (EditText) findViewById(R.id.nama);
editJarak = (EditText) findViewById(R.id.jarak);
editRating = (EditText) findViewById(R.id.rating);
editHarga = (EditText) findViewById(R.id.harga);
myDb = new DatabaseHelper(this);
}
public void addData(View view) {
boolean isInserted = myDb.insertData(editNama.getText().toString(),
editJarak.getText().toString(),
editRating.getText().toString(),
editHarga.getText().toString() );
if(isInserted)
Toast.makeText(AddSampleActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
else
Toast.makeText(AddSampleActivity.this,"Data not Inserted",Toast.LENGTH_LONG).show();
}
}
my layout, activity_add_sample.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:padding="10dp"
tools:context="com.example.mangan.AddSampleActivity">
<EditText
android:id="#+id/jarak_editText"
android:hint="Jarak"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/rating_editText"
android:hint="Rating"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/harga_editText"
android:hint="Harga"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addData"
android:text="Add"/>
</LinearLayout>
This is fullstack trace
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:5675)
at android.view.View$PerformClick.run(View.java:22651)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:208)
at android.app.ActivityThread.main(ActivityThread.java:6267)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
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:5675)
at android.view.View$PerformClick.run(View.java:22651)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:208)
at android.app.ActivityThread.main(ActivityThread.java:6267)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.mangan.AddSampleActivity.addData(AddSampleActivity.java:30)
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:5675)
at android.view.View$PerformClick.run(View.java:22651)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:208)
at android.app.ActivityThread.main(ActivityThread.java:6267)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
It seem that some content on addData method is the problem. I read another question, Getting IllegalStateException on button click, but the problem is little bit different..
Your edittext ids aren't correct. change them like below
editJarak = (EditText) findViewById(R.id.jarak_editText);
editRating = (EditText) findViewById(R.id.rating_editText);
editHarga = (EditText) findViewById(R.id.harga_editText);
Your edit text ids are wrong. Try this,
editNama = (EditText) findViewById(R.id.nama_editText);
editJarak = (EditText) findViewById(R.id.jarak_editText);
editRating = (EditText) findViewById(R.id.rating_editText);
editHarga = (EditText) findViewById(R.id.harga_editText);
I am getting error Error inflating class after running the code.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.umairgulfad.bismillah, PID: 14270
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.umairgulfad.bismillah/com.example.umairgulfad.bismillah.MainActivity}: android.view.InflateException: Binary XML file line #26: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2697)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2771)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4380)
at android.app.ActivityThread.access$1000(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1438)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5912)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
Caused by: android.view.InflateException: Binary XML file line #26: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:436)
at android.app.Activity.setContentView(Activity.java:2245)
at com.example.umairgulfad.bismillah.MainActivity.onCreate(MainActivity.java:73)
at android.app.Activity.performCreate(Activity.java:6185)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2650)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2771)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4380)
at android.app.ActivityThread.access$1000(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1438)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5912)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.example.umairgulfad.bismillah.MainActivity$MyDetailFragment that is not a Fragment
at android.app.Fragment.instantiate(Fragment.java:618)
at android.app.Fragment.instantiate(Fragment.java:594)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2124)
at android.app.Activity.onCreateView(Activity.java:5523)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:740)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:436)
at android.app.Activity.setContentView(Activity.java:2245)
at com.example.umairgulfad.bismillah.MainActivity.onCreate(MainActivity.java:73)
at android.app.Activity.performCreate(Activity.java:6185)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2650)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2771)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4380)
at android.app.ActivityThread.access$1000(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1438)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5912)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
Caused by: java.lang.ClassCastException
at android.app.Fragment.instantiate(Fragment.java:618)
at android.app.Fragment.instantiate(Fragment.java:594)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2124)
at android.app.Activity.onCreateView(Activity.java:5523)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:740)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:436)
at android.app.Activity.setContentView(Activity.java:2245)
at com.example.umairgulfad.bismillah.MainActivity.onCreate(MainActivity.java:73)
at android.app.Activity.performCreate(Activity.java:6185)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2650)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2771)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4380)
at android.app.ActivityThread.access$1000(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1438)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5912)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
The code runs in portrait mode well but in going to landscape mode, app stopped working. What is wrong here. Thanks in advance.
Layout-land/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sw600dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<fragment
android:name="com.example.umairgulfad.bismillah.MainActivity$MyListFragment"
android:id="#+id/list_fragment"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1" />
<fragment
android:name="com.example.umairgulfad.bismillah.MainActivity$MyDetailFragment"
android:id="#+id/detail_fragment"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="2" />
</LinearLayout>
</LinearLayout>
Layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_weight="2"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/mapwhere" />
</LinearLayout>
Layout-land/detailfragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/title_detailfragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Detail Fragment"/>
</LinearLayout>
MainActivity
import android.app.Activity;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
public class MainActivity extends Activity implements OnMapReadyCallback {
// if run on phone, isSinglePane = true
// if run on tablet, isSinglePane = false
boolean isSinglePane;
static String[] month ={
"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"};
#Override
public void onMapReady(GoogleMap googleMap) {
}
public static class MyListFragment extends ListFragment {
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
ListAdapter myArrayAdapter =
new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_1, month);
setListAdapter(myArrayAdapter);
}
}
public class MyDetailFragment extends SupportMapFragment {
#Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
View rootView = layoutInflater.inflate(R.layout.detailfragment, viewGroup, false);
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapwhere);
mapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
}
});
return rootView;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View v = findViewById(R.id.mapwhere);
if(v == null){
//it's run on tablet
isSinglePane = false;
/*
* MyListFragment and MyDetailFragment have been loaded in XML,
* no need load.
*/
}else{
//it's run on phone
//Load MyListFragment programmatically
isSinglePane = true;
if(savedInstanceState == null){
//if's the first time created
MyListFragment myListFragment = new MyListFragment();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.mapwhere, myListFragment);
fragmentTransaction.commit();
}
}
}
}
I extend the ListFragment class with android.support.v4.app.ListFragment but now the error is in fragmentTransaction.add(R.id.mapwhere, myListFragment); line error is
Cannot resolve method:add(int,com.example.umairgulfad.bismillad.MainActivity.MyListFragments)
Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.example.umairgulfad.bismillah.MainActivity$MyDetailFragment that is not a Fragment
There are two types of fragments: native ones and support/backport ones. You need to pick one type and stick with that.
As it stands, you are trying to use both:
You are using getFragmentManager() to add MyListFragment, indicating that MyListFragment extends android.app.ListFragment, not android.support.v4.app.ListFragment
You are using SupportMapFragment, which is a support/backport fragment
I'm very new to Android Application Development. I know my problem might be simple one. but, I'm sure it is related to layout.
Here's my issue: There's is a listView in the Fragment. When I click the button the new Fragment is added to the ScrollView in the main Layout. When I start the activity one fragment is already added to the activity. So, when I change the rotation the application does not crash.
But, new Fragment is added in the ScrollView after clicking button. But this time when I change the Rotation of the device the Application crashes giving following the error in the end. Can you help me solve this problem. Error occurs only when I change rotaion otherwise app runs fine.
Layout(XML) File activity_another_custom_list_view.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_another_custom_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.practices.app.practices.AnotherCustomListViewActivity"
android:background="#drawable/border">
<Button
android:text="#string/add_fragments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:onClick="addfrag"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/button">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.practices.app.practices.AnotherCustomListViewFragment"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="#+id/fragment"
/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Fragment Layout another_custom_listview_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fraglistview"></ListView>
</LinearLayout>
Java File AnotherCustomListViewActivity.java
package com.practices.app.practices;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.View;
public class AnotherCustomListViewActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another_custom_list_view);
}
public void addfrag(View v){
AnotherCustomListViewFragment f = new AnotherCustomListViewFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment,f);
ft.commit();
}
}
Fragment Java Class AnotherCustomListViewFragment
package com.practices.app.practices;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Adapter;
import android.widget.ListAdapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class AnotherCustomListViewFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.another_custom_listview_fragment, container, false);
String[] str = {"a","b","c","d"};
ListView fraglist = (ListView) view.findViewById(R.id.fraglistview);
ListAdapter listadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, str);
fraglist.setAdapter(listadapter);
return view;
}
}
Here's the full Error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.practices.app.practices/com.practices.app.practices.AnotherCustomListViewActivity}: android.view.InflateException: Binary XML file line #36: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3994)
at android.app.ActivityThread.access$1000(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1337)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5318)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:922)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:717)
Caused by: android.view.InflateException: Binary XML file line #36: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
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 android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143)
at com.practices.app.practices.AnotherCustomListViewActivity.onCreate(AnotherCustomListViewActivity.java:15)
at android.app.Activity.performCreate(Activity.java:6024)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3994)
at android.app.ActivityThread.access$1000(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1337)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5318)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:922)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:717)
Caused by: java.lang.IllegalStateException: Fragment com.practices.app.practices.AnotherCustomListViewFragment did not create a view.
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2145)
at android.app.Activity.onCreateView(Activity.java:5373)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:36)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:77)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
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 android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143)
at com.practices.app.practices.AnotherCustomListViewActivity.onCreate(AnotherCustomListViewActivity.java:15)
at android.app.Activity.performCreate(Activity.java:6024)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3994)
at android.app.ActivityThread.access$1000(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1337)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5318)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:922)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:717)
I added reply to the answers in comment section but no one is answering.
Well i had this kind of problem few days back but after changing the drawable in my layout it solved my problem because my drawable was too large that's why it's causing the problem.
You can check in your layout for drawable if it is large than change it with other small one.
I think this will end your problem.
In my case, updating the libraries worked for me.
I have two radio buttons and if one is selected I want to change the value of Textview. I am getting an error. The app stops working when I click one of my radio buttons. It is giving this error on log.
logged error
> /com.example.myapp.mttally E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.myapp.mttally, PID: 15125
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4025)
at android.view.View.performClick(View.java:4785)
at android.widget.CompoundButton.performClick(CompoundButton.java:120)
at android.view.View$PerformClick.run(View.java:19884)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4020)
at android.view.View.performClick(View.java:4785)
at android.widget.CompoundButton.performClick(CompoundButton.java:120)
at android.view.View$PerformClick.run(View.java:19884)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at
`com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
..`
My java code on main file is this
package com.example.myapp.mttally;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
RadioButton lend;
RadioButton borrow;
TextView toFrom;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView toFrom = (TextView) findViewById(R.id.toFrom);
RadioButton lend = (RadioButton) findViewById(R.id.lend);
RadioButton borrow = (RadioButton) findViewById(R.id.borrow);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
Boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
case R.id.lend:
if (checked)
toFrom.setText(" To");
break;
case R.id.borrow:
if (checked)
toFrom.setText(" From");
break;
}
}
}
Here my xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="8dp"
android:id="#+id/linearLayout">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:clickable="false"
>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_button1"
android:id="#+id/lend"
android:textSize="16dp"
android:onClick="onRadioButtonClicked"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_button2"
android:id="#+id/borrow"
android:textSize="16dp"
android:onClick="onRadioButtonClicked"
/>
</RadioGroup>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="140dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/toFrom"
android:layout_below="#+id/linearLayout"
android:layout_centerHorizontal="true"
android:layout_marginTop="78dp" />
</RelativeLayout>
Every time you write something like TextView toFrom, RadioButton lend, or, more generally <type of variable> <variable name>, you're declaring a new variable. You declare variables called twoFrom, lend, and borrow in two places: once in the MainActivity class and once in the onCreate method. So, your code has two different variables that are both called toFrom, two variables called lend, and two variables called borrow.
Consider the lend variable. Each copy of the lend variable has its own "scope", or the area of code in which it exists. Since you declared the first lend variable in the class itself (not inside any of the methods), its scope is the class. You can (typically) use it in code anywhere in the class.
Your second lend variable, however, is what causes the problem. You've declared a second lend variable inside the onCreate method. That second lend is the one gets the TextView object...and then it ceases to exist once the onCreate method finishes executing.
So, to fix this, your onCreate should look like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toFrom = (TextView) findViewById(R.id.toFrom);
lend = (RadioButton) findViewById(R.id.lend);
borrow = (RadioButton) findViewById(R.id.borrow);
}
That way, when you get the reference to the TextView, you're holding it in the variable that will still exist when onRadioButtonClicked is called, not in a second identically-named variable that will cease to exist almost immediately.