Error after downgrade Android API from 27 to 21 (KOTLIN) - java

I tried to downgrade my app from api 27 to 21 because my target api is from 21 to 27. But it can only run on API 26 and 27. After downgrade i try to install on API 21 then the app cannot open (it stopped working after open)
Here is my main_activity
package com.ddm.xposed.digi_com
import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.design.widget.NavigationView
import android.support.v4.view.GravityCompat
import android.support.v7.app.ActionBarDrawerToggle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import com.felhr.usbserial.UsbSerialDevice
import com.felhr.usbserial.UsbSerialInterface
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.app_bar_main.*
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
var unlock : Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
val toggle = ActionBarDrawerToggle(
this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
drawer_layout.setDrawerListener(toggle)
toggle.syncState()
nav_view.setItemIconTintList(null);
nav_view.setNavigationItemSelectedListener(this)
displayScreen(-1)
}
override fun onBackPressed() {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// 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.
when (item.itemId) {
R.id.action_settings -> return true
else -> return super.onOptionsItemSelected(item)
}
}
fun displayScreen(id: Int){
val fragment = when(id){
R.id.nav_connect -> {
ConnectFragment()
}
else -> {
ConnectFragment()
}
}
supportFragmentManager.beginTransaction().replace(R.id.relativeLayout,fragment).commit()
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
// Handle navigation view item clicks here.
displayScreen(item.itemId)
drawer_layout.closeDrawer(GravityCompat.START)
return true
}
}
Then this is the error on debug
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ddm.xposed.digi_com, PID: 2279
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ddm.xposed.digi_com/com.ddm.xposed.digi_com.MainActivity}:
android.view.InflateException: Binary XML file line #16: Error
inflating class android.support.design.widget.NavigationView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class
android.support.design.widget.NavigationView
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
at com.ddm.xposed.digi_com.MainActivity.onCreate(MainActivity.kt:32)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
at android.app.ActivityThread.access$800(ActivityThread.java:144) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5221) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249) 
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106) 
at com.ddm.xposed.digi_com.MainActivity.onCreate(MainActivity.kt:32) 
at android.app.Activity.performCreate(Activity.java:5937) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
at android.app.ActivityThread.access$800(ActivityThread.java:144) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5221) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
Caused by: android.content.res.Resources$NotFoundException: Resource ID
0x7f060057
at android.content.res.Resources.getValue(Resources.java:1233)
at android.content.res.Resources.getDrawable(Resources.java:756)
at android.content.Context.getDrawable(Context.java:402)
at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:26)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:319)
at android.support.v7.internal.widget.TintManager.getDrawable(TintManager.java:145)
at android.support.v7.internal.view.menu.MenuItemImpl.getIcon(MenuItemImpl.java:422)
at android.support.design.internal.NavigationMenuPresenter$NavigationMenuAdapter.prepareMenuItems(NavigationMenuPresenter.java:393)
at android.support.design.internal.NavigationMenuPresenter$NavigationMenuAdapter.<init>(NavigationMenuPresenter.java:255)
at android.support.design.internal.NavigationMenuPresenter.getMenuView(NavigationMenuPresenter.java:97)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:149)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:88)
at java.lang.reflect.Constructor.newInstance(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
at android.view.LayoutInflater.createView(LayoutInflater.java:607) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249) 
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106) 
at com.ddm.xposed.digi_com.MainActivity.onCreate(MainActivity.kt:32) 
at android.app.Activity.performCreate(Activity.java:5937) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
at android.app.ActivityThread.access$800(ActivityThread.java:144) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5221)

Based on my answer on this SO
You are experiencing this issue because you have resources in
drawable-v26 only and you are trying to reference them in v21. Either move them to drawable folder or drawable-v21 (if these
resources are not required in lower version)
Explanation:
When you request a resource for which you provide alternatives, Android selects which alternative resource to use at runtime, depending on current device configuration.
Lets say your current OS version is 23:
OS tries selects drawables from drawable-v23 by comparing the device configuration to available alternative resources.
Based on the documentation:
The system arrives at its decision for which resources to use with the following logic:
The flowchart of how Android finds the best-matching resource is as follows
The precedence is based on the table defined here
It actually works on selection by elimination. It will first eliminate drawable-v24 and above and try to find the drawable in drawable-v23 and below. Since it cannot find the drawables in drawable-v23 and below it throws the exception

Related

Falt form(at com.going.books.MainActivity.onCreate(MainActivity.java:19))

//main activity.java
package com.going.books;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
//start button
private Button startBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
//button start
startBtn=findViewById( R.id.startBtn1 );
startBtn.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
// Start Activity
startActivity( new Intent( MainActivity.this,Main2Activity.class ) );
}
} );
}
}
//complete
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/custom_uid"
android:orientation="horizontal"
android:gravity="bottom"
tools:context=".MainActivity">
<Button
android:id="#+id/startBtn1"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:background="#color/white"
android:layerType="software"
android:layout_weight="1"
android:text="Start"
android:textColor="#color/Black"
android:textSize="20dp" />
<!--complete-->
</LinearLayout>'''
//logcat error
'''04-05 17:34:45.750 32139-32139/com.going.books E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.going.books, PID: 32139
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.going.books/com.going.books.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.going.books.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:6033)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
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:902) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697) 
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55) 
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:482) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555) 
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161) 
at com.going.books.MainActivity.onCreate(MainActivity.java:19) 
at android.app.Activity.performCreate(Activity.java:6033) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
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:902) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697) 
Caused by: android.content.res.Resources$NotFoundException: Resource "com.going.books:drawable/custom_uid" (7f06005f) is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f06005f a=-1 r=0x7f06005f}
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2441)
at android.content.res.Resources.loadDrawable(Resources.java:2381)
at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
at android.view.View.(View.java:3738)
at android.view.ViewGroup.(ViewGroup.java:498)
at android.widget.LinearLayout.(LinearLayout.java:200)
at android.widget.LinearLayout.(LinearLayout.java:196)
at android.widget.LinearLayout.(LinearLayout.java:192)
at java.lang.reflect.Constructor.newInstance(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
at android.view.LayoutInflater.createView(LayoutInflater.java:607) 
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55) 
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:482) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555) 
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161) 
at com.going.books.MainActivity.onCreate(MainActivity.java:19) 
at android.app.Activity.performCreate(Activity.java:6033) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
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:902) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697) 
'''
Firstly I am not used background image so no error will appear.
then I am using a background image then the onClick() method error will be produced.
Next than not using background image so no error will be found...
but I am not sure why did hear...

App crashing in the part of Grid layout declaration

Class & XML are not the same
public void r(View view)
{
LinearLayout linearLayout=findViewById(R.id.playAgain);
linearLayout.setVisibility(View.INVISIBLE);
activePLayer=0 ;
for(int i=0;i<9;i++)
{
gameState[i]=2;
}
Log.i("Success","0");
//Till this part it's working
GridLayout gridL = findViewById(R.id.gl);
Log.i("Success","0");
//This log don't show up
for(int i=0;i<gridL.getChildCount();i++)
{
((ImageView)gridL.getChildAt(i)).setImageResource(0);
}
}
I want to reset my Connect-3 game made in a constraint layout to play it again but my app crashes at the point of declaration of GridLayout.
Any suggestions ?
My build error log :-
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.vivanksharma.gameconnect, PID: 32090
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:389)
at android.view.View.performClick(View.java:5612)
at android.view.View$PerformClick.run(View.java:22285)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384)
at android.view.View.performClick(View.java:5612) 
at android.view.View$PerformClick.run(View.java:22285) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6123) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 
Caused by: java.lang.ClassCastException: android.support.v7.widget.GridLayout cannot be cast to android.widget.GridLayout
at com.vivanksharma.gameconnect.MainActivity.r(MainActivity.java:91)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384) 
at android.view.View.performClick(View.java:5612) 
at android.view.View$PerformClick.run(View.java:22285) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6123) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Change from
GridLayout gridLayout = findViewById(R.id.gridLayout);
to
androidx.gridlayout.widget.GridLayout gridLayout = findViewById(R.id.gridLayout);
it is written in your stack trace,
java.lang.ClassCastException: android.support.v7.widget.GridLayout cannot be cast to android.widget.GridLayout
your Class & XML are not the same. You need to make sure that the import is correct
to fix this, just switch the current import with:
import android.support.v7.widget.GridLayout;

java.lang.IllegalStateException: Could not execute method for android:onClick when trying to click a button

When I click the buttons on my page, I get an "IllegalStateException" in my logcat. I'm new at Java development, so I don't really know if it's something simple that i'm doing wrong. All I'm trying to do is get the button to open the activity. It worked fine until i started playing with the fragments etc for the layout.
log cat
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.as1.app172, PID: 5621
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5637) 
at android.view.View$PerformClick.run(View.java:22429) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.as1.app172/com.as1.app172.LoginActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1805)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1523)
at android.app.Activity.startActivityForResult(Activity.java:4225)
at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
at android.app.Activity.startActivityForResult(Activity.java:4183)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:720)
at android.app.Activity.startActivity(Activity.java:4522)
at android.app.Activity.startActivity(Activity.java:4490)
at com.as1.app172.Import1.BtnLogin_Click(Import1.java:25)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:5637) 
at android.view.View$PerformClick.run(View.java:22429) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
java
package com.as1.app172;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class Import1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_import1);
}
public void BtnRegister_Click (View v) {
Intent i = new Intent (Import1.this, RegistrationActivity.class);
startActivity(i);
}
public void BtnLogin_Click(View v) {
Intent i = new Intent (Import1.this, LoginActivity.class);
startActivity(i);
}
}
Check your AndroidManifest.xml and make sure you are already declared the activity LoginActivity. <activity android:name=".view.acitvity.LoginActivity" />

Crash after Button Click: Could not execute method for android:onClick

I've been searching everywhere for a solution, but ultimately didn't succeed. The Program is supposed to read what's in the TextView (id=textField), then it should display the text with Log.i
Here is MainActivity.java
package quotebook.theoneandonly.com.buttons;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public void clickfunction(View view) {
EditText myTextField = ((EditText) findViewById(R.id.textField));
Log.i("textField Value", myTextField.getText().toString());
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
This is the error message I get after I click on the Button:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: quotebook.theoneandonly.com.buttons, PID: 11117
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6294) 
at android.view.View$PerformClick.run(View.java:24770) 
at android.os.Handler.handleCallback(Handler.java:790) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
at quotebook.theoneandonly.com.buttons.MainActivity.clickfunction(MainActivity.java:11)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:6294) 
at android.view.View$PerformClick.run(View.java:24770) 
at android.os.Handler.handleCallback(Handler.java:790) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
'textField' is TextView's ID. You are using it on EditText.
Check you xml again and change the ID of EditText in MainActivity.
Error log says, TextView cannot be cast to EditText.
Sorry , not enough credits to comment.
Perhaps you are trying to cast to the wrong object here. It should be a TextView and not an EditView:
EditText myTextField = ((EditText) findViewById(R.id.textField));
Should be:
TextView myTextField = ((TextView) findViewById(R.id.textField));
The relevant error message in this case is the last one in the stack trace:
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
it should be like >
EditText myTextField = (EditText) findViewById(R.id.textField);
remove "()" before casting edit text and end of the line also...i think that's all..problem solved!

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo, all activities are registered in AndroidManifest.xml, dont know whats wrong

I'm new to Android and i'm trying to run an app (simple test app), but get this error(. I have registered all my activities in AndroidManifest.xml and i'm using Android Studio 1.0.2
Here is the logcat of an app:
01-03 20:34:24.128 17300-17300/com.example.temur.sherlocked D/AndroidRuntime﹕ Shutting down VM
01-03 20:34:24.134 17300-17300/com.example.temur.sherlocked E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.temur.sherlocked, PID: 17300
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.temur.sherlocked/com.example.temur.sherlocked.MainActivity}: java.lang.InstantiationException: com.example.temur.sherlocked.MainActivity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2124)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
Caused by: java.lang.InstantiationException: com.example.temur.sherlocked.MainActivity
at java.lang.Class.newInstance(Class.java:1561)
at android.app.Instrumentation.newActivity(Instrumentation.java:1084)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2115)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1559)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1084)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2115)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:1892)
at com.example.temur.sherlocked.MainActivity.<init>(MainActivity.java:43)
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.Class.newInstance(Class.java:1559)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1084)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2115)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
Code of MainActivity
package com.example.temur.sherlocked;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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.menu_main, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
TextView infoTextView = (TextView)findViewById(R.id.textViewAnswer);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode,resultCode,data);
if (requestCode==CHOOSE_THIEF){
if (resultCode==RESULT_OK){
String thiefName = data.getStringExtra(SecondActivity.THIEF);
infoTextView.setText(thiefName);
}else {
infoTextView.setText("");
}
}
}
static final private int CHOOSE_THIEF = 0;
public void onClick(View view) {
Intent questionIntent = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(questionIntent,CHOOSE_THIEF);
}
}
Help me please :)
If you read other Stack Overflow questions, you will notice that well-written questions include the source code that is crashing (in your case, MainActivity) in addition to the stack trace.
In this case, I can tell just from the stack trace that you are calling findViewById() on your MainActivity too soon. This cannot be done until after your call to setContentView().
In particular, you appear to be calling findViewById() in the initializer of data member. Do not call inherited methods on Activity until after super.onCreate(), unless specifically directed otherwise.

Categories