onClick button causing crash - java

I've cloned this git depo it's an application that allows you to capture or choose images and save them in a pdf document.
It work fine, but when I tried to integrate in my app module (new project) and clicked on the button to start the activity it crash. I haven't changed anything the Logcat isn't helping either...
Any ideas ?
Note : I already added the activity in my manifest.
Edit : I added the style and layout below
styles
<style name="MyAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
activity_pdf
<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"
tools:context="com.nabeeltech.capturedoc.imgtopdf.PdfActivity">
<Button
android:id="#+id/btnPdf"
android:layout_width="match_parent"
android:layout_margin="16dp"
android:text="Generate Document"
android:layout_height="wrap_content" />
</LinearLayout>
PdfActivity
Button btn_Pdf = findViewById(R.id.btnPdf);
activity = this;
filename=new Date().toString();
btn_Pdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getImages();
}
});
}
private void getImages() {
Config config = new Config();
config.setToolbarTitleRes(R.string.str_bar_tool);
ImagePickerActivity.setConfig(config);
Intent intent = new Intent(this, ImagePickerActivity.class);
startActivityForResult(intent, INTENT_REQUEST_GET_IMAGES);
}
#Override
protected void onActivityResult(int requestCode, int resuleCode, Intent intent) {
super.onActivityResult(requestCode, resuleCode, intent);
if (requestCode == INTENT_REQUEST_GET_IMAGES && resuleCode == Activity.RESULT_OK) {
ArrayList<Uri> image_uris = intent.getParcelableArrayListExtra(ImagePickerActivity.EXTRA_IMAGE_URIS);
if(image_uris.size()!=0){
ArrayList<String> tempUris = new ArrayList<>();
for (Uri uri : image_uris) {
tempUris.add(uri.getPath());
}
CallBackCreatePdf callBackCreatePdf = new CallBackCreatePdf() {
#Override
public void OnCallBackCreatePdf(String tPath) {
if(tPath!=""){
Toast.makeText(getBaseContext(),"Pdf Created", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getBaseContext(),"Error creating PDF", Toast.LENGTH_LONG).show();
}
}
};
MaterialDialog.Builder builder = new MaterialDialog.Builder(activity)
.title("Creating PDF")
.content("just a moment ...")
.cancelable(false)
.progress(true, 0);
MaterialDialog dialog = builder.build();
AsynCreatePdf asynCreatePdf = new AsynCreatePdf(activity,callBackCreatePdf,dialog,tempUris,"1",("pdf"+(new Date()).getSeconds()));
asynCreatePdf.execute();
}
Log.d(LOG_ACTIVITY, "onActivityResult");
}
}
Here is the stack trace
2020-02-05 16:44:36.884 17311-17311/com.nabeeltech.capturedoc E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nabeeltech.capturedoc, PID: 17311
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nabeeltech.capturedoc/com.gun0912.tedpicker.ImagePickerActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
at androidx.appcompat.app.AppCompatDelegateImpl.setSupportActionBar(AppCompatDelegateImpl.java:421)
at androidx.appcompat.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:150)
at com.gun0912.tedpicker.ImagePickerActivity.initView(ImagePickerActivity.java:95)
at com.gun0912.tedpicker.ImagePickerActivity.onCreate(ImagePickerActivity.java:82)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

This Activity already has an action bar supplied by the window decor.
Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set
windowActionBar to false in your theme to use a Toolbar instead.
at androidx.appcompat.app.AppCompatDelegateImpl.setSupportActionBar(AppCompatDelegateImpl.java:421)
Need Another theme. At first goto manifest
<activity android:name=".ImagePickerActivity"
android:theme="#style/CustomTheme"
android:screenOrientation="portrait"
/>
Then go to res/values/styles.xml section. Create Theme. Your Theme will be
<style name="CustomTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

Fixed. Thanks to #intellij-amiya
I missed another style. I was using
<activity android:name="com.nabeeltech.capturedoc.imgtopdf.PdfActivity"
android:label="#string/title_activity_pdf"
android:theme="#style/MyAppTheme"/>
<activity android:name="com.gun0912.tedpicker.ImagePickerActivity"
android:theme="#style/Theme.AppCompat.Light.DarkActionBar"
android:screenOrientation="portrait"
/>
So I replaced ImagePickerActivity with
android:theme="#style/AppTheme_picker"
Now it works.

Related

Error inflating class android.support.design.internal.NavigationMenuItemView

I am running this app on sdk version 28. The problem android.view.InflateException: Binary XML file line #17: Binary XML file line #17: Error inflating class android.support.design.internal.NavigationMenuItemView
suddenly appeared, causing my app to crash. After checking the nav drawer I know that the problem is when there are menu items to inflate, because when I comment them out the app runs with no problems, i also checked if the problem is with the drawables but I changed them with no effect.
here are my files
manifest.xml file
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/SemiGofaaTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
styles.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<!-- SemiGofaa application theme. -->
<style name="SemiGofaaTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:colorBackground">#color/colorPrimaryDark</item>
<item name="fontFamily">#font/cairo</item>
<item name="android:statusBarColor">#color/colorAccent</item>
<item name="alertDialogTheme">#style/AlertDialog.AppCompat.Light</item>
<item name="android:windowContentTransitions">true</item>
<!--<item name="android:background">#drawable/bkg</item>-->
</style>
<style name="SemiGofaaTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="SemiGofaaTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
build.gradle:
android {
compileSdkVersion 28
defaultConfig {
applicationId "semicode.semigofaa"
minSdkVersion 21
targetSdkVersion 28
versionCode 5
versionName "0.5"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false
}
MainActivity.java
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_gofaas) {
if(userLoggedIn)
startActivity(new Intent(this, GofaasActivity.class));
else
startActivity(new Intent(this, LoginActivity.class));
} else if (id == R.id.nav_wishlist) {
if(userLoggedIn)
startActivity(new Intent(this, WishlistActivity.class));
else
startActivity(new Intent(this, LoginActivity.class));
} else if (id == R.id.nav_settings) {
} else if (id == R.id.nav_terms) {
startActivity(new Intent(this,TermsActivity.class));
} else if (id == R.id.nav_contact_us) {
}else if (id == R.id.nav_about) {
startActivity(new Intent(this,AboutActivity.class));
} else if (id == R.id.nav_share) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT,getString(R.string.hey_check_out_semigofaa_app) + link);
sendIntent.setType("text/plain");
startActivity(sendIntent);
}else if(id == R.id.log_out_nav_drawer){
SaveAppData.clearUserData(this);
SaveSharedPreference.clearUserData(this);
startActivity(new Intent(this,MainActivity.class));
finish();
Toast.makeText(getApplicationContext(),"Logged out",Toast.LENGTH_SHORT).show();
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
attaching menu at navigationview
<
android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/header"
app:menu="#menu/menu_items"
/>
and make separate file for menu like
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/mhome" android:title="HOME"
android:icon="#drawable/homeicon"
android:checkable="true"></item>
<item android:id="#+id/mportfolio" android:title="PORLFOLIO"
android:icon="#drawable/portfolioicon"
android:checkable="true"></item>
<item android:id="#+id/mcareer" android:title="CAREER"
android:icon="#drawable/careericon"
android:checkable="true"></item>
<item android:id="#+id/mblog" android:title="BLOG"
android:icon="#drawable/blogicon"
android:checkable="true"></item>
</menu>

Option menu not working with theme ThemeOverlay.AppCompat.Dark.ActionBar

I have created an application which has a RecyclerView and each item has an option menu, which will show the popup to share. Everything is working fine but when I try to click on the option menu it crashes. I am getting a weird error. I google it and it seems like there is something wrong with my theme. I tried many solutions available on the internet but none of them working for me.
Here is my theme:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:textColorPrimary">#ffffff</item>
<item name="android:textColorSecondary">#color/about_us_link_text_color</item>
</style>
This is my Adapter class
holder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PopupMenu popUp = new PopupMenu(context,holder.buttonViewOption);
popUp.inflate(R.menu.option_menu);
popUp.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.share:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,news.get(position).getUrl());
intent.putExtra(Intent.EXTRA_SUBJECT,"check out this site");
context.startActivity(Intent.createChooser(intent,"share"));
break;
case R.id.save:
break;
}
return false;
}
});
popUp.show();
}
});
option_menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/share"
android:title="Share" />
<item
android:id="#+id/save"
android:title="Save" />
</menu>
The error I am getting is :
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 6: TypedValue{t=0x1d/d=0xffff4081 a=3 r=0x7f0c001a}
at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:705)
at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:6890)
at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:7071)
at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:446)
at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:386)
at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:385)
at android.view.LayoutInflater.inflate(LayoutInflater.java:502)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.support.v7.view.menu.MenuAdapter.getView(MenuAdapter.java:93) 
at android.support.v7.view.menu.MenuPopup.measureIndividualMenuWidth(MenuPopup.java:160) 
at android.support.v7.view.menu.StandardMenuPopup.tryShow(StandardMenuPopup.java:153) 
at android.support.v7.view.menu.StandardMenuPopup.show(StandardMenuPopup.java:187) 
at android.support.v7.view.menu.MenuPopupHelper.showPopup(MenuPopupHelper.java:290) 
at android.support.v7.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:175) 
at android.support.v7.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:141) 
at android.support.v7.widget.PopupMenu.show(PopupMenu.java:233) 
at com.kotlin.whatshappening.activity.adapter.NewsAdapter$2.onClick(NewsAdapter.java:142) 
at android.view.View.performClick(View.java:5204) 
at android.view.View$PerformClick.run(View.java:21158) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5461) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Can someone tell me what I am doing wrong?
Instead of:
PopupMenu popUp = new PopupMenu(context, holder.buttonViewOption);
Perform:
PopupMenu popUp = new PopupMenu(view.getContext(), holder.buttonViewOption);
Try this
popUp.getMenuInflater().inflate(R.menu.option_menu, popUp.getMenu());
I hope this helps you.
First create a method in you adapter class like the following.
private void showPopup(View view, String url) {
PopupMenu popup = new PopupMenu(activityContext, view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.option_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.share:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,url);
intent.putExtra(Intent.EXTRA_SUBJECT,"check out this site");
activityContext.startActivity(Intent.createChooser(intent,"share"));
break;
case R.id.save:
break;
}
return true;
}
});
popup.show();
}
And in your onBindViewHolder method call the above function,
holder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showPopup(view, news.get(holder.getAdapterPosition()).getUrl());
}
});

NullpointerException at actionbar in android [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I am newbie to android and working on a demo for swipe tabs demo and refering a link,Going step by step,but i stuck at a point,when i run the app,it throws nullpointerexception,Please see below code and help me to figure out please,
main_listing.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
java
public class MainListingActivity extends FragmentActivity {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listing);
TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager)findViewById(R.id.pager);
Tab.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position); }
});
Tab.setAdapter(TabAdapter);
actionBar = getActionBar();
//Enable Tabs on Action Bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener(){
#Override
public void onTabReselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Tab.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}};
//Add New Tab
actionBar.addTab(actionBar.newTab().setText("Android").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("iOS").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Windows").setTabListener(tabListener));
}
logcat
FATAL EXCEPTION: main
Process: abc.kayraas.com.allaboutcity, PID: 15728
java.lang.RuntimeException: Unable to start activity ComponentInfo{abc.kayraas.com.allaboutcity/abc.kayraas.com.allaboutcity.MainListingActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2404)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5653)
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:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at abc.kayraas.com.allaboutcity.MainListingActivity.onCreate(MainListingActivity.java:37)
at android.app.Activity.performCreate(Activity.java:5541)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2368)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464) 
at android.app.ActivityThread.access$900(ActivityThread.java:172) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:146) 
at android.app.ActivityThread.main(ActivityThread.java:5653) 
at java.lang.reflect.Method.invokeNative(Native Method) 
style.xml
<resources>
<style name="MyRadioButtonStyle" parent="#android:style/Widget.CompoundButton.RadioButton">
<item name="android:button">#drawable/radio_selected</item>
</style>
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
manifest
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity
android:name=".SlashActivity"
android:theme="#android:style/Theme.Holo.Light.DarkActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
try this :
actionBar=getSupportActionBar
try this code. ActionBar actionBar = getSupportActionBar();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Home");
//in styles.xml in your AppTheme ,update as
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
//in your xml write this at the top :
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:visibility="gone"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:visibility="visible"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
this is your current code:
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
instead write this:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
In your style.xml change this line:
<style name="AppBaseTheme" parent="android:Theme.Light">
to
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">

Exception on using custom title bar android

I have been trying using custom title bar in my activity. Following is the code i have used
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
if ( customTitleSupported ) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);
}
final TextView txtTitle = (TextView) findViewById(R.id.txtTitle);
if ( txtTitle != null ) {
txtTitle.setText("PikMyBox - Welcome to PikMyBox");
}
setContentView(R.layout.activity_main);
}
custom_title_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtTitle"
android:layout_alignParentLeft="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtCustomText"
android:layout_alignParentRight="true"/>
</RelativeLayout>
Styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorSplash</item>
<item name="android:windowNoTitle">true</item>
</style>
Manifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
-----
I m getting the following error when activity executed
android.util.AndroidRuntimeException: You cannot combine custom titles with other title features
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:5590)
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.util.AndroidRuntimeException: You cannot combine custom titles with other title features
at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:302)
at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:2975)
at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:3241)
at com.android.internal.policy.impl.PhoneWindow.getDecorView(PhoneWindow.java:1821)
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:363)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:312)
at android.support.v7.app.AppCompatDelegateImplV7.findViewById(AppCompatDelegateImplV7.java:229)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:184)
at com.kommlabs.pikmybox.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5447)
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:5590) 
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) 
How can i solve this problem ?
Change android:windowNoTitle to false in your styles.xml.
Your setContentView(R.layout.activity_main); is below final TextView txtTitle = (TextView) findViewById(R.id.txtTitle); ie why you are getting exception.You are initilizing the view before it is been created.
Change it to
setContentView(R.layout.activity_main);
final TextView txtTitle = (TextView) findViewById(R.id.txtTitle);
Also in case of your exception with custom title bar use this style
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">false</item>
</style>
As suggested here https://stackoverflow.com/a/27410003/3111083

Popup menu crash at show();

So I'm trying to create a popup menu which will popup when user presses on ImageButton.
I'm getting strange force close error on popupMenu.show():
case R.id.action_menu_edit_biljeske_uredi:
linearLayoutDodajBiljeskuBojeIOstalaSranja.setVisibility(View.VISIBLE);
imageButtonBiljeskeDodajBiljeskuBojaPozadine
= (ImageButton) findViewById(R.id.imageButtonBiljeskeDodajBiljeskuBojaPozadine);
imageButtonBiljeskeDodajBiljeskuBojaPozadine.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(getApplicationContext(),
imageButtonBiljeskeDodajBiljeskuBojaPozadine);
popupMenu.getMenuInflater().inflate(R.menu.menu_popup_biljeske_boje,
popupMenu.getMenu());
popupMenu.show();
}
});
}
Does anyone know why?
Logcat
04-12 20:22:27.847 24594-24594/com.daroioradecic.studyandexamplannerbydario E/AndroidRuntime:
FATAL EXCEPTION: main
Process: com.daroioradecic.studyandexamplannerbydario, PID: 24594
java.lang.RuntimeException: Failed to resolve attribute at index 6
at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:603)
at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:6474)
at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:6642)
at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:741)
at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:685)
at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:62)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.support.v7.view.menu.MenuPopupHelper$MenuAdapter.getView(MenuPopupHelper.java:374)
at android.support.v7.view.menu.MenuPopupHelper.measureContentWidth(MenuPopupHelper.java:223)
at android.support.v7.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:157)
at android.support.v7.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:129)
at android.support.v7.widget.PopupMenu.show(PopupMenu.java:216)
at com.daroioradecic.studyandexamplannerbydario.BiljeskeDodajBiljesku$2.onClick(BiljeskeDodajBiljesku.java:106)
at android.view.View.performClick(View.java:4856)
at android.view.View$PerformClick.run(View.java:19956)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5389)
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:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
Menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:icon="#drawable/ic_action_crvena"
android:title=""
android:id="#+id/popupMenuBiljeskeBojeCrvena"/>
<item
android:icon="#drawable/ic_action_ljubicasta"
android:title=""
android:id="#+id/popupMenuBiljeskeBojeLjubicasta"/>
<item
android:icon="#drawable/ic_action_svijetloplava"
android:title=""
android:id="#+id/popupMenuBiljeskeBojeSvijetloPlava"/>
<item
android:icon="#drawable/ic_action_tamnoplava"
android:title=""
android:id="#+id/popupMenuBiljeskeBojeTamnoPlava"/>
<item
android:icon="#drawable/ic_action_zelena"
android:title=""
android:id="#+id/popupMenuBiljeskeBojeZelena"/>
<item
android:icon="#drawable/ic_action_usranozuta"
android:title=""
android:id="#+id/popupMenuBiljeskeBojeUsranoZuta"/>
<item
android:icon="#drawable/ic_action_narancasta"
android:title=""
android:id="#+id/popupMenuBiljeskeBojeNarancasta"/>
<item
android:icon="#drawable/ic_action_siva"
android:title=""
android:id="#+id/popupMenuBiljeskeBojeSiva"/>
</menu>
PopupMenu popupMenu = new PopupMenu(getApplicationContext(), imageButtonBiljeskeDodajBiljeskuBojaPozadine);
Replace getApplicationContext() with YourActivity.this. I think your problem will be solved.
Why getApplicationContext() is not working, visit https://possiblemobile.com/2013/06/context/ for detailed solution.

Categories

Resources