Android Studio: java.lang.RuntimeException: Unable to start activity - java

I am a beginner and I am doing an Android game which I made the model in Java (on Eclipse).
I'm actually trying to draw a grid, but when I run, I have a java.lang.RuntimeException: Unable to start activity
I don't really understand where the problem is, it says the problem is from the setContentView (R.layout.activity_main).
This is my AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="e.khoig.test2">
<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"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
This is where it contains the game and its creation:
public class Game extends Application {
TenGame tenGame;
public void onCreate(){
super.onCreate();
tenGame=new TenGame();
}
TenGame getTenGame(){
return tenGame;
}
}
Then this is my GameView which I draw and do the actions:
public class GameView extends SurfaceView implements SurfaceHolder.Callback{
Game gam;
Paint paint=new Paint();
int canvasWidth;
int cellSize;
public GameView(Context context, AttributeSet attributeSet){
super(context,attributeSet);
getHolder().addCallback(this);
getGam(context);
}
public GameView(Context context){
super(context);
getHolder().addCallback(this);
this.getGam(context);
}
public final void getGam(Context context){
gam=(Game)(context.getApplicationContext());
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
canvasWidth=width;
cellSize=width/5;
reDraw();
}
public void reDraw(){
Canvas c=getHolder().lockCanvas();
if(c!=null){
this.onDraw(c);
getHolder().unlockCanvasAndPost(c);
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
int x=(int)event.getX();
int y=(int)event.getY();
int action=event.getAction();
TenGame theGame=gam.getTenGame();
switch(action){
case MotionEvent.ACTION_DOWN:{
theGame.getSelectedGroup();
}
default:
return false;
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
#Override
public void onDraw(Canvas canvas){
paint.reset();
TenGame theGame=gam.getTenGame();
canvas.drawColor(Color.GRAY);
paint.setColor(Color.BLACK);
for(int x=0;x<canvasWidth;x+=cellSize){
canvas.drawLine(x,0,x,canvasWidth,paint);
}
for(int y=0;y<canvasWidth;y+=cellSize){
canvas.drawLine(0,y,canvasWidth,y,paint);
}
paint.setTextSize(50);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
for(int x=0;x<5;x++){
for(int y=0;y<5;y++){
canvas.drawText("1",(x*cellSize)+11,(cellSize+y*cellSize)-6,paint);
}
}
}
}
And finally my MainActivity:
public class MainActivity extends Activity {
Game gam;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gam=(Game)this.getApplication();
}
}
And my activity_main.xml:
<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=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<e.khoig.test2.GameView
android:id="#+id/gameView"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
/>
</LinearLayout>
This is my error message:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: e.khoig.test2, PID: 15920
java.lang.RuntimeException: Unable to start activity ComponentInfo{e.khoig.test2/e.khoig.test2.MainActivity}: android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class e.khoig.test2.GameView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class e.khoig.test2.GameView
Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class e.khoig.test2.GameView
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:461)
at android.app.Activity.setContentView(Activity.java:2737)
at e.khoig.test2.MainActivity.onCreate(MainActivity.java:12)
at android.app.Activity.performCreate(Activity.java:7183)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to e.khoig.test2.Game
at e.khoig.test2.GameView.getGam(GameView.java:34)
at e.khoig.test2.GameView.<init>(GameView.java:24)
UPDATE :
I resolved my problem, i just had to declare the application in the manifest, thank you for your helps !

The 3rd last line in your stacktrace states that you can't cast application to an instance of your game class.
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to e.khoig.test2.Game
In your main activity after setContentView could you use getActivity without the this i.e.
gam = (Game) getActivity();
I believe the problem right now could be that you are trying to cast an instance of application to an instance of activity.
Just my two cents.

The stack trace says -
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to e.khoig.test2.Game
at e.khoig.test2.GameView.getGam(GameView.java:34)
You are doing this -
public final void getGam(Context context){
gam=(Game)(context.getApplicationContext());
}
You should not cast the instance of superclass to subclass. This is leading to your ClassCastException.
This answer gives a better picture.
Hope it helps.

This stack trace shows this
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to e.khoig.test2.Game
Because this
A extends B
A a = new B() // this is wrong
(B)a//this is wrong
B b = new A() // this is right
(A)b // this is right
You cannot cast the instance of superclass to subclass.

Related

Android: Error inflating PreferenceScreen

I've seen a few other similar references to this issue, however have not yet found a solution to my issue.
I used to use android.preference.Preference however have recently moved over to androidx in order to use a SeekBarPreference.
Now, when I try to inflate the Preference screen I get the following error:
2021-11-17 20:57:20.623 18270-18270/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.project.test, PID: 18270
android.view.InflateException: Binary XML file line #1: Error inflating class (not found)android.support.v7.preference.PreferenceScreen
at androidx.preference.PreferenceInflater.createItemFromTag(PreferenceInflater.java:287)
at androidx.preference.PreferenceInflater.inflate(PreferenceInflater.java:151)
at androidx.preference.PreferenceInflater.inflate(PreferenceInflater.java:109)
at androidx.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:216)
at androidx.preference.PreferenceFragmentCompat.setPreferencesFromResource(PreferenceFragmentCompat.java:377)
at com.project.test.SettingsFragment.onCreatePreferences(SettingsFragment.java:33)
at androidx.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:160)
at com.project.test.SettingsFragment.onCreate(SettingsFragment.java:25)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2684)
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:280)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1175)
at androidx.fragment.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1255)
at androidx.fragment.app.FragmentTransition.calculateFragments(FragmentTransition.java:1138)
at androidx.fragment.app.FragmentTransition.startTransitions(FragmentTransition.java:136)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1989)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1947)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: java.lang.ClassNotFoundException: android.support.v7.preference.PreferenceScreen
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:454)
at androidx.preference.PreferenceInflater.createItem(PreferenceInflater.java:214)
at androidx.preference.PreferenceInflater.createItemFromTag(PreferenceInflater.java:277)
at androidx.preference.PreferenceInflater.inflate(PreferenceInflater.java:151) 
at androidx.preference.PreferenceInflater.inflate(PreferenceInflater.java:109) 
at androidx.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:216) 
at androidx.preference.PreferenceFragmentCompat.setPreferencesFromResource(PreferenceFragmentCompat.java:377) 
at com.project.test.SettingsFragment.onCreatePreferences(SettingsFragment.java:33) 
at androidx.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:160) 
at com.project.test.SettingsFragment.onCreate(SettingsFragment.java:25) 
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2684) 
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:280) 
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1175) 
at androidx.fragment.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1255) 
at androidx.fragment.app.FragmentTransition.calculateFragments(FragmentTransition.java:1138) 
at androidx.fragment.app.FragmentTransition.startTransitions(FragmentTransition.java:136) 
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1989) 
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1947) 
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849) 
at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413) 
at android.os.Handler.handleCallback(Handler.java:938) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loopOnce(Looper.java:201) 
at android.os.Looper.loop(Looper.java:288) 
at android.app.ActivityThread.main(ActivityThread.java:7839) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 
Caused by: java.lang.ClassNotFoundException: android.support.v7.preference.PreferenceScreen
at java.lang.Class.classForName(Native Method) 
at java.lang.Class.forName(Class.java:454) 
at androidx.preference.PreferenceInflater.createItem(PreferenceInflater.java:214) 
at androidx.preference.PreferenceInflater.createItemFromTag(PreferenceInflater.java:277) 
at androidx.preference.PreferenceInflater.inflate(PreferenceInflater.java:151) 
at androidx.preference.PreferenceInflater.inflate(PreferenceInflater.java:109) 
at androidx.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:216) 
at androidx.preference.PreferenceFragmentCompat.setPreferencesFromResource(PreferenceFragmentCompat.java:377) 
at com.project.test.SettingsFragment.onCreatePreferences(SettingsFragment.java:33) 
at androidx.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:160) 
at com.project.test.SettingsFragment.onCreate(SettingsFragment.java:25) 
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2684) 
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:280) 
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1175) 
at androidx.fragment.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1255) 
at androidx.fragment.app.FragmentTransition.calculateFragments(FragmentTransition.java:1138) 
at androidx.fragment.app.FragmentTransition.startTransitions(FragmentTransition.java:136) 
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1989) 
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1947) 
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849) 
at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413) 
at android.os.Handler.handleCallback(Handler.java:938) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loopOnce(Looper.java:201) 
at android.os.Looper.loop(Looper.java:288) 
at android.app.ActivityThread.main(ActivityThread.java:7839) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 
An excerpt of my SettingsFragment.java file (error is reported on the setPreferencesFromResource line):
public class SettingsFragment extends PreferenceFragmentCompat {
Utilities utils = new Utilities();
Context context = ProjectGlobal.getAppContext();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor settingsEditor = settings.edit()
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.pref_general, rootKey);
Preference pfPermissionsReset = findPreference("permissionsPrompt");
Preference pfBatterySaving = findPreference("batteryPrompt");
final SeekBarPreference pfVolumeValue = (SeekBarPreference) findPreference("sbVolume");
final SwitchPreference pfGlobalVolume = (SwitchPreference) findPreference("chkUseGlobalVolume");
//init global volume control
pfVolumeValue.setMax(100);
pfVolumeValue.setMin(0);
From my pref_general.xml preference layout:
<android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.preference.PreferenceCategory
android:summary="App Settings"
android:title="GENERAL" />
<android.support.v7.preference.SwitchPreference
android:key="chkNotificationListener"
android:summary="This option allows you to disable the app without uninstalling the application. [Default: On]"
android:title="Notification Listener"
android:defaultValue="true"
/>
<android.support.v7.preference.SwitchPreference
android:key="chkHideBanner"
android:summary="This option hides the banner notification at the top of the screen. Please note, this may impact the reliability of the application if disabled! [Default: Off]"
android:title="Hide Banner Notification"
android:defaultValue="false"
/>
<android.support.v7.preference.Preference android:title="Check Battery Saving"
android:key="batteryPrompt"
android:summary="Tap here to check that the app is allowed to run in the background/during startup on your device" />
<android.support.v7.preference.PreferenceCategory
android:summary="Audio"
android:title="AUDIO"/>
<android.support.v7.preference.SwitchPreference
android:key="chkOnlyUseSpeaker"
android:summary="This option will attempt to play audio alerts from the device loudspeaker only [Default: Off]"
android:title="Only Use Speaker"
android:defaultValue="false"
/>
<android.support.v7.preference.SwitchPreference
android:key="chkUseGlobalVolume"
android:summary="This option will override any volume settings set in each rule [Default: Off]"
android:title="Use Global Volume"
android:defaultValue="false"
/>
<android.support.v7.preference.SeekBarPreference
android:key="sbVolume"
android:title="Global Volume"
android:dependency="chkUseGlobalVolume"
android:max="100"
android:defaultValue="50"
/>
/>
<android.support.v7.preference.PreferenceCategory
android:summary="Security"
android:title="SECURITY"/>
<android.support.v7.preference.Preference android:title="Check Permissions"
android:key="#string/permissionsPrompt"
android:summary="Tap here to re-check permissions" />
</android.support.v7.preference.PreferenceScreen>
From my Launcher.java:
public class Launcher extends AppCompatActivity implements SettingsFragment.OnFragmentInteractionListener,RuleFragment.OnFragmentInteractionListener,HelpFragment.OnFragmentInteractionListener {
RuleFragment ruleFragment = new RuleFragment();
SettingsFragment prefsFragment = new SettingsFragment();
HelpFragment helpFragment = new HelpFragment();
Utilities utils = new Utilities();
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
getSupportFragmentManager().beginTransaction().remove(helpFragment).commit();
getSupportFragmentManager().beginTransaction().remove(prefsFragment).commit();
getSupportFragmentManager().beginTransaction().replace(R.id.contentFrame,ruleFragment).commit();
return true;
case R.id.navigation_settings:
getSupportFragmentManager().beginTransaction().remove(ruleFragment).commit();
getSupportFragmentManager().beginTransaction().remove(helpFragment).commit();
getSupportFragmentManager().beginTransaction().replace(R.id.contentFrame,prefsFragment).commit();
return true;
case R.id.navigation_help:
getSupportFragmentManager().beginTransaction().remove(prefsFragment).commit();
getSupportFragmentManager().beginTransaction().remove(ruleFragment).commit();
getSupportFragmentManager().beginTransaction().replace(R.id.contentFrame,helpFragment).commit();
return true;
}
return false;
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Launcher.context = getApplicationContext();
Utilities utils = new Utilities();
//initiate home screen
setContentView(R.layout.main);
getSupportFragmentManager().beginTransaction().replace(R.id.contentFrame,ruleFragment).commit();
}
My main.xml layout for Launcher:
<?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">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="#+id/main_coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layout_above="#+id/navigation"
app:titleTextColor="#color/Black"
>
<FrameLayout
android:id="#+id/contentFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="?android:attr/windowBackground"
app:menu="#menu/navigation" />
</RelativeLayout>
Managed to solve this. It was because I hadn't updated android.support.v7.preference within pref_general.xml to androidx.preference.
After doing this, it worked fine!

Null pointer exception on 'android.content.Context.getApplicationInfo()' [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
I'm very new to android, just took it as a weekend development for fun, trying to make a very simple app and I'm having an error.
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gwallet">
<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>
</application>
</manifest>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
Context context;
final TextView txtNewDailyDetailDescription = findViewById(R.id.txt_new_daily_detail_description);
final TextView txtNewDailyDetailSpend = findViewById(R.id.txt_new_daily_detail_spend);
final Button btnNewDailyDetail = findViewById(R.id.btn_new_daily_detail);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
txtNewDailyDetailSpend.setInputType(InputType.TYPE_CLASS_NUMBER);
btnNewDailyDetail.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
btnNewDailyDetailClick();
}
});
}
protected void btnNewDailyDetailClick() {
//Some irrelevant code.
}
}
Stack trace of the error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.gwallet, PID: 32507
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.gwallet/com.gwallet.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3027)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3264)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1955)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7091)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:164)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
at android.content.Context.obtainStyledAttributes(Context.java:677)
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:692)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:659)
at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:479)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:214)
at com.gwallet.MainActivity.<init>(MainActivity.java:18)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:41)
at android.app.Instrumentation.newActivity(Instrumentation.java:1219)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3015)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3264) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1955) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7091) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975) 
I do get that something that is not initialized is being tried to get in use, but I don't know what it is. I keep reading Activities documentation. Any help?
Let me know if this work!
public class MainActivity extends AppCompatActivity {
Context context;
TextView txtNewDailyDetailDescription;
TextView txtNewDailyDetailSpen;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtNewDailyDetailDescription=findViewById(R.id.txt_new_daily_detail_description);
txtNewDailyDetailSpend = findViewById(R.id.txt_new_daily_detail_spend);
btnNewDailyDetail = findViewById(R.id.btn_new_daily_detail);
context = this;
txtNewDailyDetailSpend.setInputType(InputType.TYPE_CLASS_NUMBER);
btnNewDailyDetail.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
btnNewDailyDetailClick();
}
});
}
protected void btnNewDailyDetailClick() {
//Some irrelevant code.
}
}

I am getting error Unfortunately app has stopped.Where is the mistake?

These are the sections of code mentioned.As I run these errors pop up in logcat file and the app crashes when opened.There are so many of them that I could not figure out where the actual mistake is.Please Help me out where there is need of modification and replacement as I am trying to build a splash screen.
logcat:
06-08 11:36:18.203 17265-17265/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.elvero.telecom.voipapp, PID: 17265
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.elvero.telecom.voipapp/com.elvero.telecom.voipapp.splash}: android.view.InflateException: Binary XML file line #10: Error inflating class ImageView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2567)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2641)
at android.app.ActivityThread.access$800(ActivityThread.java:182)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1515)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5717)
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:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class ImageView
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.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.elvero.telecom.voipapp.splash.onCreate(splash.java:13)
at android.app.Activity.performCreate(Activity.java:6092)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2514)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2641) 
at android.app.ActivityThread.access$800(ActivityThread.java:182) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1515) 
at android.os.Handler.dispatchMessage(Handler.java:111) 
at android.os.Looper.loop(Looper.java:194) 
at android.app.ActivityThread.main(ActivityThread.java:5717) 
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:959) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) 
Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x6
at android.content.res.TypedArray.getDimensionPixelOffset(TypedArray.java:533)
at android.view.View.<init>(View.java:3927)
at android.widget.ImageView.<init>(ImageView.java:139)
at android.widget.ImageView.<init>(ImageView.java:135)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:60)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:56)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
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: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.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292) 
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
at com.elvero.telecom.voipapp.splash.onCreate(splash.java:13) 
at android.app.Activity.performCreate(Activity.java:6092) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2514) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2641) 
at android.app.ActivityThread.access$800(ActivityThread.java:182) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1515) 
at android.os.Handler.dispatchMessage(Handler.java:111) 
at android.os.Looper.loop(Looper.java:194) 
at android.app.ActivityThread.main(ActivityThread.java:5717) 
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:959) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) 
06-08 11:36:22.207 17265-17265/? I/Process: Sending signal. PID: 17265 SIG: 9
MainActivity.java:
package com.elvero.telecom.voipapp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar= (Toolbar)findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.elvero.telecom.voipapp">
<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">
</activity>
<activity android:name=".splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
splashactivity:
<?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"
android:visibility="visible"
tools:context="com.elvero.telecom.voipapp.splash">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#android:color/holo_blue_light"
android:elevation="#android:dimen/dialog_min_width_minor"
android:scaleType="fitCenter"
android:src="#drawable/logo_elvero"/>
</RelativeLayout>
Splash.java:
package com.elvero.telecom.voipapp;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class splash extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Handler handler=new Handler();
handler.postDelayed(new Runnable(){
#Override
public void run(){
startActivity(new Intent(splash.this,MainActivity.class));
finish();
}
},1000);
}
}
error caused because code
android:elevation="#android:dimen/dialog_min_width_minor"
its better to give direct value to it like
android:elevation="5dp"
You can make separate Activity as a splash screen and use below code in onCreate() to show it for required seconds and then can move to your main landing activity.
new CountDownTimer(2000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
HarmUtils.openActivity(SplashScreenActivity.this, LoginActivity.class, true);
}
}.start();
here 2000 is waiting time (2000 = 2 seconds) and second parameter is time interval which I set to 1 second.
You can specify your time as per your requirements.
Hope this will help you.

Move from Activity class to AppCompatActivity class

I'm trying to develop an Android Application that will play through a video and then transition to another activity.
The issue I'm having is that the video needs to be played FullScreen and in order to do so I had to switch the extends from "AppCompatActivity" to "Activity" otherwise the program would crash when trying to run full screen.
So when I transition to the next activity I had to call that class with extends Activity as well.
Now when I try to implement a drawer sidebar I'm running into issues because the class doesn't extend AppCompat, and is having explicit issues with "getSupportActionBar"
(The video plays, and transitions to the second screen without issue it's just proceeding from there that I'm caught on)
Is there any way to have my second activity extend AppCompat, without it crashing? Or if anyone is aware of a more effective way of implementing this?
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.act_main);
VideoView splash = (VideoView) findViewById(R.id.SplashView);
Uri splashPath = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.phsplash);
splash.setVideoURI(splashPath);
splash.start();
final int interval = 7000;
Handler handler = new Handler();
Runnable runnable = new Runnable(){
public void run(){
Intent goHome = new Intent(Main.this, HomeView.class);
startActivity(goHome);
}
};
handler.postAtTime(runnable, System.currentTimeMillis()+interval);
handler.postDelayed(runnable, interval);
}
}
xml
<VideoView
android:id="#+id/SplashView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
There is then the Home view (Not finished writing due to AppCompat issue)
public class HomeView extends Activity {
private ListView theDrawer;
private ArrayAdapter<String> theAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
theDrawer = (ListView)findViewById(R.id.drawlayout);
theOptions();
setupDrawer();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
private void theOptions(){
String[] panelarray = {"About PHam", "Upcoming Projects", "Idea Submit", "Locate PHam"};
theAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, panelarray);
theDrawer.setAdapter(theAdapter);
theDrawer.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
Toast.makeText(HomeView.this, "WTF is Toast!", Toast.LENGTH_SHORT).show();
}
});
}
private void setupDrawer(){
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="This is the Home Screen!"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView" />
<ListView
android:id="#+id/navlist"
android:layout_width="200dp"
android:layout_gravity="left|start"
android:layout_height="match_parent"
android:background="#ffeeeeee" />
</LinearLayout>
And then Finally the manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.phsplash.captain.pcketham">
<application
android:allowBackup="true"
android:icon="#mipmap/phico"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<activity android:name=".Main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".HomeView"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black" >
</activity>
</application>
</manifest>
Log Cat Error Files when implementing Public class HomeView extends AppCompatActivity:
11-15 21:05:54.857 13627-13627/? E/Typeface: SANS_LOC file not found.
11-15 21:05:54.867 13627-13627/? E/Typeface: SANS_LOC file not found.
11-15 21:06:02.107 13627-13627/com.phsplash.captain.pcketham E/VdcInflateDelegate: Exception while inflating <vector>
java.lang.RuntimeException: Failed to resolve attribute at index 1
at android.content.res.TypedArray.getColorStateList(TypedArray.java:425)
at android.support.graphics.drawable.VectorDrawableCompat.updateStateFromTypedArray(VectorDrawableCompat.java:518)
at android.support.graphics.drawable.VectorDrawableCompat.inflate(VectorDrawableCompat.java:472)
at android.support.graphics.drawable.VectorDrawableCompat.createFromXmlInner(VectorDrawableCompat.java:436)
at android.support.v7.widget.AppCompatDrawableManager$VdcInflateDelegate.createFromXmlInner(AppCompatDrawableManager.java:708)
at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:348)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:188)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:181)
at android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawableManager.java:689)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:186)
at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:77)
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:83)
at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:146)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:41)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:193)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:173)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:511)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:71)
at com.phsplash.captain.pcketham.HomeView.onCreate(HomeView.java:22)
at android.app.Activity.performCreate(Activity.java:5958)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
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:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
11-15 21:06:02.117 13627-13627/com.phsplash.captain.pcketham E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.phsplash.captain.pcketham, PID: 13627
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.phsplash.captain.pcketham/com.phsplash.captain.pcketham.HomeView}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
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:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:343)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:312)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:277)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.phsplash.captain.pcketham.HomeView.onCreate(HomeView.java:23)
at android.app.Activity.performCreate(Activity.java:5958)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474) 
at android.app.ActivityThread.access$800(ActivityThread.java:144) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:155) 
at android.app.ActivityThread.main(ActivityThread.java:5696) 
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:1028) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 
Updated LogCat with <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar":
11-15 21:37:15.627 27184-27184/? E/Typeface: SANS_LOC file not found.
11-15 21:37:15.627 27184-27184/? E/Typeface: SANS_LOC file not found.
11-15 21:37:22.867 27184-27184/com.phsplash.captain.pcketham E/VdcInflateDelegate: Exception while inflating <vector>
java.lang.RuntimeException: Failed to resolve attribute at index 1
at android.content.res.TypedArray.getColorStateList(TypedArray.java:425)
at android.support.graphics.drawable.VectorDrawableCompat.updateStateFromTypedArray(VectorDrawableCompat.java:518)
at android.support.graphics.drawable.VectorDrawableCompat.inflate(VectorDrawableCompat.java:472)
at android.support.graphics.drawable.VectorDrawableCompat.createFromXmlInner(VectorDrawableCompat.java:436)
at android.support.v7.widget.AppCompatDrawableManager$VdcInflateDelegate.createFromXmlInner(AppCompatDrawableManager.java:708)
at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:348)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:188)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:181)
at android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawableManager.java:689)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:186)
at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:77)
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:83)
at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:146)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:41)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:193)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:173)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:511)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:71)
at com.phsplash.captain.pcketham.HomeView.onCreate(HomeView.java:22)
at android.app.Activity.performCreate(Activity.java:5958)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
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:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
11-15 21:37:22.877 27184-27184/com.phsplash.captain.pcketham E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.phsplash.captain.pcketham, PID: 27184
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.phsplash.captain.pcketham/com.phsplash.captain.pcketham.HomeView}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
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:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:343)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:312)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:277)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.phsplash.captain.pcketham.HomeView.onCreate(HomeView.java:23)
at android.app.Activity.performCreate(Activity.java:5958)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474) 
at android.app.ActivityThread.access$800(ActivityThread.java:144) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:155) 
at android.app.ActivityThread.main(ActivityThread.java:5696) 
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:1028) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 
Extend your second class from AppCompatActivity instead of Activity.
public class HomeView extends AppCompatActivity
Happy coding. Cheers!
EDIT
For that greatness who just downvoted my answer, here is a snippet of the code without any error from Android Studio:
public class test extends AppCompatActivity{
private ListView theDrawer;
private ArrayAdapter<String> theAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
}
and here is the link to necessary documentation about support action bar.
To solve appCompat error, you need to add support library to your app build.gradle dependencies:
compile 'com.android.support:appcompat-v7:24.2.0'
Remember to always adjust your androidBuildToolsVersion, androidCompileSdkVersion, and androidTargetSdkVersion with the same API version. In this case, you need to use API Level 24.
Then you can extend your Activity with AppCompatActivity, something like:
public class HomeView extends AppCompatActivity {
...
}
Read more for Support Library from:
Support Library Features
Support Library Setup
UPDATE
Based on your error log, it means that you need to use AppCompat as the theme for you activity. Change styles.xml resource with AppCompat, something like this:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item> <!-- Your primary color -->
<item name="colorPrimaryDark">#color/colorPrimaryDark</item> <!-- Your dark color -->
<item name="colorAccent">#color/colorAccent</item> <!-- your accent color -->
</style>
Then add the theme to your Activity or Application in AndroidManifest.xml with android:theme="#style/AppTheme" something like this:
<application
android:allowBackup="true"
android:icon="#mipmap/phico"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">

Unable to start activity error with Scene2D

We are trying to draw something with the scene2D from libGDX. We have our main activity then use the extends Androidapplication to initialize our testgame class. Here we define our screen and then make our stage and actors in the screen class.
Problem is that we are getting the same error over and over:
>
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gamepadTest.app/com.gamepadTest.app.MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.gamepadTest.app/com.gamepadTest.app.TestGame}; have you declared this activity in your AndroidManifest.xml?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.gamepadTest.app.MainActivity" on path: DexPathList[[zip file "/data/app/com.gamepadTest.app-1.apk"],nativeLibraryDirectories=[/data/app- lib/com.gamepadTest.app-1, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
            
> at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
Have a look:
import android.os.Bundle;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
public class MainActivity extends AndroidApplication {
private TestGame testGame = new TestGame();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
initialize(testGame, cfg);
}
}
import com.badlogic.gdx.Game;
public class TestGame extends Game
{
TestScreen testScreen;
#Override
public void create() {
testScreen = new TestScreen();
setScreen(testScreen);
}
#Override
public void dispose() {
testScreen.dispose();
}
}
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.scenes.scene2d.Stage;
public class TestScreen implements Screen {
private Stage stage;
private TestActor testActor;
public TestScreen() {
stage = new Stage();
Gdx.input.setInputProcessor(stage);
testActor = new TestActor();
stage.addActor(testActor);
}
public void resize(int width, int height) {
stage.setViewport(800, 600, false);
stage.getCamera().translate(-stage.getGutterWidth(), -stage.getGutterHeight(), 0);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
}
#Override public void dispose() {
stage.dispose();
}
#Override public void show() {}
#Override public void hide() {}
#Override public void pause() {}
#Override public void resume() {}
}
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.scenes.scene2d.Actor;
public class TestActor extends Actor{
private ShapeRenderer renderer;
#Override
public void act(float delta) {
super.act(delta);
renderer = new ShapeRenderer();
}
#Override
public void draw (SpriteBatch batch, float parentAlpha) {
batch.end();
renderer.setProjectionMatrix(batch.getProjectionMatrix());
renderer.setTransformMatrix(batch.getTransformMatrix());
renderer.translate(getX(), getY(), 0);
renderer.begin(ShapeRenderer.ShapeType.Point);
renderer.rect(0, 0, 100, 100);
renderer.end();
batch.begin();
}
}
UPDATE:
Here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gamepadTest.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.gamepadTest.app.MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You have to declare your activity TestGame inside your AndroidManifest.xml as <activity>
For more info about the manifest: http://developer.android.com/guide/topics/manifest/manifest-intro.html
Should look something like this, and needs to be inside the <application>-tag:
<activity
android:name="com.example.TestGame"
android:label="#string/app_name" >
</activity>
TestGame isn't an Activity:
public class TestGame extends Game{
The Activity you must declare is MainActivity. So its wierd, probably you made another Activity with that same name. But anyway, try setting it with dot notation instead of the whole package:
<activity
android:name=".TestGame"
android:label="#string/app_name" >
</activity>
See this:
Unable to find explicit activity class

Categories