Exception on using custom title bar android - java

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

Related

onClick button causing crash

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.

Android -Simple random app

I'm trying to build an app and have a little bit of a problem.
Android Monitor :
08-31 07:41:59.705 10118-10118/com.example.user.rotateit E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.rotateit/com.example.user.rotateit.HasilActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.user.rotateit.HasilActivity.onCreate(HasilActivity.java:24)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
at android.app.ActivityThread.access$600(ActivityThread.java:130) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4745) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 
HasilActivity.Java :
public class HasilActivity extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hasil);
int choice = 0;
final Random random = new Random();
choice = random.nextInt(2)+0;
EditText et_black = (EditText) findViewById(R.id.et_black);
EditText et_white = (EditText) findViewById(R.id.et_white);
TextView tv_result = (TextView) findViewById(R.id.tv_result);
String et_blackText = et_black.getText().toString();
String et_whiteText = et_white.getText().toString();
if (choice == 0) {
tv_result.setText(et_blackText);
}
if (choice == 1) {
tv_result.setText(et_whiteText);
}
}
}
XML (activity_hasil):
<?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"
tools:context="com.example.user.rotateit.HasilActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/tv_result"/>
</LinearLayout>
MainActivity.Java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView btn_start = (ImageView) findViewById(R.id.btn_start);
btn_start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, HasilActivity.class);
startActivity(i);
}
});
}
}
XML (activity_main) :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:background="#42079D"
tools:context="com.example.user.rotateit.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="#+id/et_black"
style="#style/EditText1"
android:background="#0B0503"
android:textColor="#FEFCFB"
/>
<EditText
android:id="#+id/et_white"
style="#style/EditText1"
android:background="#FEFCFB"
android:textColor="#0B0503"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_start"
android:id="#+id/btn_start"
android:layout_gravity="center"/>
</LinearLayout>
</ScrollView>
MANIFEST.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.rotateit">
<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/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HasilActivity"></activity>
</application>
</manifest>
The app always stops working when I run the program.
This is the whole program. I'm pretty sure that the problem is in the editText.
Basically, i want the program itself be able to show what the user typed in, in random. So, there are two EditTexts, et_black and et_white. the user will typed in any text in both of the editText, than the program will show either et_black or et_white.
Just started learning android studio.
You have to create a String with the text inside the et_black TextEdit object. After that you can assign this String to the tv_result TextView
String et_blackText = et_black.getText().toString();
if (choice == 0 || choice == 1) {
tv_result.setText(et_blackText);
}
In addition, you have to create the following objects in your XML
EditText et_black = (EditText) findViewById(R.id.et_black);
EditText et_white = (EditText) findViewById(R.id.et_white);
TextView tv_result = (TextView) findViewById(R.id.tv_result);
R.id.et_black,R.id.et_white,R.id.tv_result are not created in your HasilXML file
You are trying to put the EditText et_black as the text of the TextView.
I won't say anything else, just..carefully check your code!!
PS post you manifest.xml

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">

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.

Android I/O 2014 Demo App Problems

I am attempting to write get a handle on the new Shared Elements Animations API that google L preview is giving us.
I have been trying to use the code base as a point of reference along with: https://developer.android.com/preview/material/animations.html
For the life of me I cannot seem to get my app to work. However when I run the Demo App from google the transition works just fine.
MyActivity.java:
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
final ImageView imageView = (ImageView)this.findViewById(R.id.img_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void showPhoto(View view) {
Intent intent = new Intent();
intent.setClass(this, MyActivity2.class);
ImageView hero = (ImageView) ((View) view.getParent()).findViewById(R.id.img_main);
((ViewGroup) hero.getParent()).setTransitionGroup(false);
ActivityOptions options =
ActivityOptions.makeSceneTransitionAnimation(this, hero, "robot");
startActivity(intent, options.toBundle());
}
}
activity_my.xml:
<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=".MyActivity">
<ImageView
android:id="#+id/img_main"
android:viewName="photo1"
android:layout_width="64dp"
android:layout_height="64dp"
android:src="#drawable/test"
android:onClick="showPhoto"/>
</RelativeLayout>
styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowExitTransitionOverlap">true</item>
<item name="android:windowActionBarOverlay">false</item>
<item name="android:windowContentOverlay">#null</item>
</style>
</resources>
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jms_m_000.transitionshit" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MyActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MyActivity2"
android:label="#string/title_activity_my_activity2" >
</activity>
</application>
</manifest>
The Error I am receiving:
07-06 18:54:42.153 24145-24145/com.example.jms_m_000.transitionshit
E/AndroidRuntime﹕FATAL EXCEPTION: main
Process: com.example.jms_m_000.transitionshit, PID: 24145
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3970)
at android.view.View.performClick(View.java:4598)
at android.view.View$PerformClick.run(View.java:19268)
at android.os.Handler.handleCallback(Handler.java:738)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5070)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at rnal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
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:3965)
at android.view.View.performClick(View.java:4598)
at android.view.View$PerformClick.run(View.java:19268)
at android.os.Handler.handleCallback(Handler.java:738)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5070)
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:836)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.app.ActivityOptions.toBundle()' on a null object reference
at com.example.jms_m_000.transitionshit.MyActivity.showPhoto(MyActivity.java:56)
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:3965)
at android.view.View.performClick(View.java:4598)
at android.view.View$PerformClick.run(View.java:19268)
at android.os.Handler.handleCallback(Handler.java:738)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5070)
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:836)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
Im not sure why but options is null after calling .makeSceneTransitionAnimation()...
Any Thoughts?
Try adding
requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
in the onCreate() method of your activity.
That surprisingly worked for me in making options non null.
Define a view name by calling setViewName() to 'hero' in your first Activity, and define a view name (same as defined in makeSceneTransitionAnimation) to your ImageView in your second Activity . That's worked for me

Categories

Resources