Why does My app Unfortunately Has Stopped - android-studio

Error Shoing For this line
Toolbar toolbar = findViewById(R.id.toolbar);
Error:
Caused by: java.lang.ClassCastException: android.widget.Toolbar cannot be cast to androidx.appcompat.widget.Toolbar
at com.example.mcq.CategoriesActivity.onCreate(CategoriesActivity.java:25)

To use androidx, in your build.gradle file, add the following:
dependencies {
implementation 'androidx.appcompat:appcompat:1.0.0'
}
Now you can use androidx toolbar as follows:
androidx.appcompat.widget.Toolbar
Else if you have not refactored to androidx, then just use:
android.support.v7.widget.Toolbar
Hope you will find the solution.

You are using a wrong library for the toolbar, check your import statement and replace
import androidx.appcompat.widget.Toolbar;
With:
import android.widget.Toolbar;
In your activity

Related

importing Fragment and FragmentTransaction error

when I am trying to import these two lines in my code
"import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;"
showing error like cannot resolve symbol fragment.
Did you declare the dependencies in the Gradle?
It´s the first thing you need to do before an import.
Try this:
dependencies {
// Java language implementation
implementation "androidx.fragment:fragment:$fragment_version"
// Kotlin
implementation "androidx.fragment:fragment-ktx:$fragment_version"
}
And check out this link! Fragments Info

Birt createFactoryObject returning null when the example shouldn't have a null

I am trying to create a simple Birt report following this example using Groovy instead. I'm not certain if it's even possible to design reports with Groovy but I haven't been able to find a solution.
From my understanding I don't believe I need to set BIRT_HOME after version 3.7. Regardless I receive the same error with or without setting BIRT_HOME. For some reason createFactoryObject returns null so I cannot use the createDesignEngine function. I'm not sure if I need to Grab another runtime version or if I'm missing other imports because I'm using Groovy. Any ideas are appreciated.
import java.io.IOException;
#Grab(group='org.eclipse.birt.runtime', module='org.eclipse.birt.runtime',
version='4.5.0a')
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.model.api.CellHandle;
import org.eclipse.birt.report.model.api.DesignConfig;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.DesignEngine;
import org.eclipse.birt.report.model.api.ElementFactory;
import org.eclipse.birt.report.model.api.GridHandle;
import org.eclipse.birt.report.model.api.IDesignEngine;
import org.eclipse.birt.report.model.api.IDesignEngineFactory;
import org.eclipse.birt.report.model.api.ImageHandle;
import org.eclipse.birt.report.model.api.LabelHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.RowHandle;
import org.eclipse.birt.report.model.api.SessionHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import com.ibm.icu.util.ULocale;
/**
* Simple BIRT Design Engine API (DEAPI) demo.
*/
// Create a session handle. This is used to manage all open designs.
// Your app need create the session only once.
//Configure the Engine and start the Platform
DesignConfig config = new DesignConfig( );
//config.setProperty("BIRT_HOME", "C:\\birtruntime\\birt-runtime-4_5_0\\ReportEngine");
IDesignEngine engine = null;
try{
Platform.startup( config );
IDesignEngineFactory factory = (IDesignEngineFactory)
Platform.createFactoryObject( IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY );
engine = factory.createDesignEngine( config );
}catch( Exception ex){
ex.printStackTrace();
}
This is the error I receive
java.lang.NullPointerException: Cannot invoke method createDesignEngine() on null object
at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:91)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:47)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.NullCallSite.call(NullCallSite.java:34)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:128)
at sample.run(sample.groovy:42)
at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:264)
at groovy.lang.GroovyShell.run(GroovyShell.java:377)
at groovy.lang.GroovyShell.run(GroovyShell.java:366)
at groovy.ui.GroovyMain.processOnce(GroovyMain.java:589)
at groovy.ui.GroovyMain.run(GroovyMain.java:332)
at groovy.ui.GroovyMain.access$1400(GroovyMain.java:69)
at groovy.ui.GroovyMain$GroovyCommand.process(GroovyMain.java:291)
at groovy.ui.GroovyMain.processArgs(GroovyMain.java:134)
at groovy.ui.GroovyMain.main(GroovyMain.java:116)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:114)
at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:136)
Caught: java.lang.NullPointerException: Cannot invoke method newSessionHandle() on null object
java.lang.NullPointerException: Cannot invoke method newSessionHandle() on null object
at sample.run(sample.groovy:49)
Evgeny recommended to place the factory creation on a single line. I did not realize it was on 2 lines when I posted it here. Regardless it was on a single line in my source code so that was not the issue.
I did not figure out how to get this piece of code to work with the groovy grab command but I did figure it out using the classpath option. First I downloaded the runtime engine on the eclipse website. It would be the zip file under the Report Engine section. You might be able to use a newer version but I got it to work with 4.5.0.
Unzip this folder somewhere and in the ReportEngine\lib folder copy all the org.eclipse jars along with the flute, com.ibm.icu, and org.mozilla.javascript jars into a separate folder like C:\birtJars. You might be able to use even less jars but I didn't want to spend the time to figure out which ones weren't needed. All I know is if I added ReportEngin\lib to the classpath there would be a linkage error from what I assume were class names that matched some java classes.
Now to get the script to run in the command line you can use
groovy -cp "C:\birtJars\*;." myscript.groovy
What I did not know at first was that if you used -cp after your groovy script then the given classpath will not be used

Error with Nullable annotation when generating Javadoc using doclava

I am trying to generate a Javadoc for an Android Library using Doclava in Android Studio. The source code uses "Nullable" tag at some point and this causes a crash while generating the javadoc:
In doclet class com.google.doclava.Doclava, method start has thrown an exception java.lang.reflect.InvocationTargetException
com.sun.tools.javac.code.Symbol$CompletionFailure: class file for javax.annotation.Nullable not found
Can I somehow overcome this?
I had to include in Javadoc's classpath, the findbugs:jsr305 library.
So, in my Gradle configuration, I added:
dependencies {
// For Doclava JavaDoc
jaxDoclet("com.google.doclava:doclava:1.0.6")
classpaths files('build/intermediates/classes/release')
classpaths 'com.google.code.findbugs:jsr305:3.0.1'
}
task javadoc(type: Javadoc) {
//.... the rest of the configuration
options {
classpath += configurations.classpaths.files.asType(List)
//.... the rest of the configuration
}
}
For this exception, check here: https://code.google.com/p/android/issues/detail?id=1261
Just add -XDignore.symbol.file to the end of the command, and it may remove this exception.

Is JUnit really unable to find my test class in android studio?

I read the solution to How to run a simple JUnit4 test in Android Studio 1.1? and verified my gradle plugin version is greater than 1.1
Right Clicking my junit 4 test class and selecting run gives this error in android studio:
Cannot find class com.me.android.javamodule.MyTestClass
My android proj dir looks like:
In module "app":
src-->main
src-->test-->java-->com.me.android.working contains WorkingTest.java
src-->test-->java-->com.me.android.javamodule contains MyTestClass.java
"javamodule" is a regular java module that the app module depends on, it is a sibling to the "app" module.
I don't think the error is accurate because I copied "MyTestClass.java" into com.me.android.working, which contains WorkingTest.java, a Junit4 test that CAN run. Android Studio still complains with the same error.
Here is MyTestClass.java
package com.me.android.javamodule;
import org.junit.Before;
import org.junit.Test;
public class MyTestClass {
private Solver solver;
#Before
public void init() {
solver = new Solver();
}
#Test
public void testReverse() {
assertTrue(solver.parseStr("woof").equals("foow"));
}
}
This worked for me:
right click src and select New Directory, call it test
do the same for test and call it java
right click java and select New Package to create a new package like com.application. unit test can go here.

GWTP Nested Presenter : Type<RevealContentHandler<?>> Cannot be resolved

I'm trying to make the nested presenter in GWT.
I've been "copy & paste" code from the sample GWTP project #http://code.google.com/p/gwt-platform/wiki/SimpleNestedSample
At one point the code
#ContentSlot
public static final Type<RevealContentHandler<?>> contentSlot = new Type<RevealContentHandler<?>>();
gave me error and i am not able to find which package to import by eclipse suggestion to resolve error by Type.
(Been trying all the possible "Type" packages by eclipse suggestion such as
import com.google.gwt.i18n.server.Type;
import com.google.gwt.dev.asm.Type;
import com.sun.tools.javac.code.Type;
)
Can anyone tell what package to import?
import com.google.gwt.event.shared.GwtEvent.Type;

Resources