Surface View Inflate error - java

When I am trying to run the code in a real device it gives an error but it works on an emulator.
I think the error is related to the packages.
Here is the error;
11-11 18:27:57.481 30000-30000/com.lag.duckdodger.duckdodger E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.lag.duckdodger.duckdodger, PID: 30000
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lag.duckdodger.duckdodger/com.lag.duckdodger.duckdodger.MainActivity}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class com.lag.duckdodger.duckdodger.View.AnimationView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class com.lag.duckdodger.duckdodger.View.AnimationView
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class com.lag.duckdodger.duckdodger.View.AnimationView
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.lag.duckdodger.duckdodger.MainActivity.onCreate(MainActivity.java:12)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 103219212 byte allocation with 12866376 free bytes and 12MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:879)
at android.graphics.Bitmap.createBitmap(Bitmap.java:856)
at android.graphics.Bitmap.createBitmap(Bitmap.java:787)
at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:663)
at com.lag.duckdodger.duckdodger.AnimationModel.Background.<init>(Background.java:44)
at com.lag.duckdodger.duckdodger.View.AnimationView.<init>(AnimationView.java:32)
at java.lang.reflect.Constructor.newInstance0(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:430) 
at android.view.LayoutInflater.createView(LayoutInflater.java:645) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858) 
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:518) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:426) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:377) 
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) 
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
at com.lag.duckdodger.duckdodger.MainActivity.onCreate(MainActivity.java:12) 
at android.app.Activity.performCreate(Activity.java:6664) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
Here is a screenshot of the packages:
Package Screenshot
Here is the surface view;
package com.lag.duckdodger.duckdodger.View;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import com.lag.duckdodger.duckdodger.AnimationModel.Background;
import com.lag.duckdodger.duckdodger.AnimationModel.LogModel;
import com.lag.duckdodger.duckdodger.AnimationModel.LogModel2;
import com.lag.duckdodger.duckdodger.AnimationModel.PlayerModel;
import com.lag.duckdodger.duckdodger.AnimationModel.RockModel;
/**
* Created by FrancisJan on 20/10/2016.
*/
public class AnimationView extends SurfaceView implements SurfaceHolder.Callback{
private final AnimationThread DuckThread;
private final Background bg;
private final PlayerModel pm;
private final LogModel lm;
private final RockModel rm;
private final LogModel2 lm2;
public AnimationView(Context context, AttributeSet attrs) {
super(context, attrs);
getHolder().addCallback(this);
DuckThread = new AnimationThread(this);
bg = new Background(context);
pm = new PlayerModel(context);
lm = new LogModel(context);
rm = new RockModel(context);
lm2 = new LogModel2(context);
}
And here is the layout xml file;
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
tools:context="com.lag.duckdodger.duckdodger.MainActivity">
<view
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.lag.duckdodger.duckdodger.View.AnimationView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="#+id/view" />
</RelativeLayout>

Caused by: java.lang.OutOfMemoryError: Failed to allocate a 103219212 byte allocation with 12866376 free bytes and 12MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:879)
at android.graphics.Bitmap.createBitmap(Bitmap.java:856)
at android.graphics.Bitmap.createBitmap(Bitmap.java:787)
at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:663)
at com.lag.duckdodger.duckdodger.AnimationModel.Background.<init>(Background.java:44)
at com.lag.duckdodger.duckdodger.View.AnimationView.<init>(AnimationView.java:32)
Basically, the constructor to AnimationView is creating a Background object. The constructor of Background is trying to create a bitmap that is 103MB large. That's too big. (Something close in size to a 5000x5000 pixel image) You didn't show the code to Background, but it's trying to create a Bitmap or load an image that is too large.
Try scaling your original Bitmap resource to a more manageable size.

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;

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!

How to programatically add LinearLayout object to my context?

I have my app, it has the following appareance:
The table at the bottom side is a TableLayout.
In my MainActivity.java I am using the following code to populate the whole layout of my app:
setContentView(R.layout.activity_main);
movieCompletionView = (TokenCompletionView)findViewById(R.id.searchMovie);
cineCompletionView = (TokenCompletionView)findViewById(R.id.searchCine);
...
TableLayout tl_Movies = (TableLayout)findViewById(R.id.tableLayoutMovies);
tl_Movies.removeAllViews();
for (...) tl_Movies.addView(someRow);
Then, I googled for some samples to improve the appareance of my table, and I found this:
But this sample builds only the table. In the MainActivity.java the code used for building it :
Table table = new Table(this);
setContentView(table);
And Table class:
public class Table extends LinearLayout {
..
}
Now, what I want is to add the smarter table to the original layout of my app.
I added an empty LinearLayout component in my activity_main.xml and I tried to populate it with this code:
setContentView(R.layout.activity_main);
movieCompletionView = (TokenCompletionView)findViewById(R.id.searchMovie);
cineCompletionView = (TokenCompletionView)findViewById(R.id.searchCine);
...
linearLayout = (LinearLayout) findViewById(R.id.smarterTable);
linearLayout = new Table(this);
But it does not work.
Sorry for the very basic question, but, can you tell me which are the right statements for adding the smarter table to the original layout of my app?
Edit 27-Nov-2017
Following the suggestion on Mike M. and F43nd1r.
I configured my activity_main.xml in this way:
<?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="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="com.myapps.pavel.myseapp.MainActivity">
<com.myapps.pavel.myseapp.Table
android:id="#+id/smarterTable"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"/>
</LinearLayout>
And I used the following code in my MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = (Table) findViewById(R.id.smarterTable);
And I got this error:
FATAL EXCEPTION: main
Process: com.myapps.pavel.myseapp, PID: 5875
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapps.pavel.myseapp/com.myapps.pavel.myseapp.MainActivity}: android.view.InflateException: Binary XML file line #12: Binary XML file line #12: Error inflating class com.myapps.pavel.myseapp.Table
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: android.view.InflateException: Binary XML file line #12: Binary XML file line #12: Error inflating class com.myapps.pavel.myseapp.Table
Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class com.myapps.pavel.myseapp.Table
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
at java.lang.Class.getConstructor0(Class.java:2204)
at java.lang.Class.getConstructor(Class.java:1683)
at android.view.LayoutInflater.createView(LayoutInflater.java:618)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.myapps.pavel.myseapp.MainActivity.onCreate(MainActivity.java:83) /* This lane has this statement: setContentView(R.layout.activity_main);*/
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Any suggestions?
Thanks
Solved!:
as Mike M. says, I had to change the parameters of the Table constructor
public Table(Context context,AttributeSet attrs) {
super(context, attrs);
Instead of <LinearLayout> use <com.your.package.Table> in the xml layout.
Then use the following:
linearLayout = (Table) findViewById(R.id.smarterTable);
for (...) linearLayout.addView(someRow);
Edit regarding your edit
Add this constructor to Table:
public Table(Context context, AttributeSet attrs) {
super(context, attrs);
}

Android: Inflate Custom View inside Fragment

I have an application that uses a fragment view as part of its layout. Within this fragment, I want to instantiate a Paint Canvas where i can do custom drawing.
The problem is, my app crashes when I go to inflate the fragment with the error android.view.InflateException: Binary XML file line #15: Binary XML file line #15: Error inflating class com.example.xxxxx.NeckCanvasOverlay
I'm not sure what's causing my Paint class not to inflate properly. I know there are special rules about how you use findViewById inside a fragment, but I thought I was adhering to them since i'm calling findViewByid on the fragment's view itself (called "v" in my code).
How can I fix this issue?
FragmentNeckDisplayMenu.java (Fragment Class)
public class FragmentNeckDisplayMenu extends Fragment {
private static View v;
private NeckCanvasOverlay neckHUD;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.menu_fragment_neck_display,container,false); //ERROR HERE
NeckCanvasOverlay neckHUD = (NeckCanvasOverlay) v.findViewById(R.id.neckHUD); //paint test
...
return v;
}
}
NeckCanvasOverlay.java (Paint Class)
class NeckCanvasOverlay extends View {
private Paint mPainter;
public NeckCanvasOverlay(Context context) {
super(context);
initView();
}
private void initView(){ //Initializes canvas & paint objects here to save performance
mPainter = new Paint(Paint.ANTI_ALIAS_FLAG);
mPainter.setColor(Color.BLUE);
mPainter.setAlpha(128);
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(180, 900, 200, mPainter);
}
}
menu_fragment_neck_display.xml (Fragment's Layout)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/canvas"
android:background="#drawable/image">
<LinearLayout
android:layout_width="148dp"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="4">
!--Custom view for Canvas here -->
<com.example.xxxxx.NeckCanvasOverlay
android:id="#+id/neckHUD"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/neck_for_menu"/>
</LinearLayout>
<Button
android:id="#+id/menuIcon"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="64dp"
android:text="Menu"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="bottom"
android:layout_weight="0.5"/>
</LinearLayout>
Full Stack Trace (Edit: Included full trace)
08-06 21:47:24.894 26334-26334/com.example.xxxxx E/ACRA: ACRA caught a InflateException for com.example.xxxxx
android.view.InflateException: Binary XML file line #15: Binary XML file line #15: Error inflating class com.example.gu.NeckCanvasOverlay
at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
at com.example.xxxxx.FragmentNeckDisplayMenu.onCreateView(FragmentNeckDisplayMenu.java:32)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
at android.support.v4.view.ViewPager.setOffscreenPageLimit(ViewPager.java:733)
at com.example.xxxxx.PlayFrets.configMenu(PlayFrets.java:150)
at com.example.xxxxx.AsyncTask_NoteFileLoader.onPostExecute(AsyncTask_NoteFileLoader.java:125)
at com.example.xxxxx.AsyncTask_NoteFileLoader.onPostExecute(AsyncTask_NoteFileLoader.java:9)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.access$500(AsyncTask.java:180)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class com.example.gu.NeckCanvasOverlay
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:855)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
at android.view.LayoutInflater.inflate(LayoutInflater.java:527)
at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
at com.example.xxxxx.FragmentNeckDisplayMenu.onCreateView(FragmentNeckDisplayMenu.java:32) 
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248) 
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) 
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613) 
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570) 
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) 
at android.support.v4.view.ViewPager.populate(ViewPager.java:1106) 
at android.support.v4.view.ViewPager.populate(ViewPager.java:952) 
at android.support.v4.view.ViewPager.setOffscreenPageLimit(ViewPager.java:733) 
at com.example.xxxxx.PlayFrets.configMenu(PlayFrets.java:150) 
at com.example.xxxxx.AsyncTask_NoteFileLoader.onPostExecute(AsyncTask_NoteFileLoader.java:125) 
at com.example.xxxxx.AsyncTask_NoteFileLoader.onPostExecute(AsyncTask_NoteFileLoader.java:9) 
at android.os.AsyncTask.finish(AsyncTask.java:651) 
at android.os.AsyncTask.access$500(AsyncTask.java:180) 
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7229) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.gu.NeckCanvasOverlay" on path: DexPathList[[zip file "/data/app/com.example.xxxxx-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.xxxxx-2/lib/arm64, /vendor/lib64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.view.LayoutInflater.createView(LayoutInflater.java:595)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:847) 
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:855) 
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:527) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
at com.example.xxxxx.FragmentNeckDisplayMenu.onCreateView(FragmentNeckDisplayMenu.java:32) 
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248) 
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) 
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613) 
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570) 
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) 
at android.support.v4.view.ViewPager.populate(ViewPager.java:1106) 
at android.support.v4.view.ViewPager.populate(ViewPager.java:952) 
at android.support.v4.view.ViewPager.setOffscreenPageLimit(ViewPager.java:733) 
at com.example.xxxxx.PlayFrets.configMenu(PlayFrets.java:150) 
at com.example.xxxxx.AsyncTask_NoteFileLoader.onPostExecute(AsyncTask_NoteFileLoader.java:125) 
at com.example.xxxxx.AsyncTask_NoteFileLoader.onPostExecute(AsyncTask_NoteFileLoader.java:9) 
at android.os.AsyncTask.finish(AsyncTask.java:651) 
at android.os.AsyncTask.access$500(AsyncTask.java:180) 
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7229) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
Suppressed: java.lang.ClassNotFoundException: com.example.gu.NeckCanvasOverlay
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 33 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
08-06 21:47:24.954 26334-27123/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.xxxxx, PID: 26334
android.view.InflateException: Binary XML file line #15: Binary XML file line #15: Error inflating class com.example.gu.NeckCanvasOverlay
at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
at com.example.xxxxx.FragmentNeckDisplayMenu.onCreateView(FragmentNeckDisplayMenu.java:32)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
at android.support.v4.view.ViewPager.setOffscreenPageLimit(ViewPager.java:733)
at com.example.xxxxx.PlayFrets.configMenu(PlayFrets.java:150)
at com.example.xxxxx.AsyncTask_NoteFileLoader.onPostExecute(AsyncTask_NoteFileLoader.java:125)
at com.example.xxxxx.AsyncTask_NoteFileLoader.onPostExecute(AsyncTask_NoteFileLoader.java:9)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.access$500(AsyncTask.java:180)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class com.example.gu.NeckCanvasOverlay
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:855)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
at android.view.LayoutInflater.inflate(LayoutInflater.java:527)
at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
at com.example.xxxxx.FragmentNeckDisplayMenu.onCreateView(FragmentNeckDisplayMenu.java:32) 
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248) 
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) 
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613) 
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570) 
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) 
at android.support.v4.view.ViewPager.populate(ViewPager.java:1106) 
at android.support.v4.view.ViewPager.populate(ViewPager.java:952) 
at android.support.v4.view.ViewPager.setOffscreenPageLimit(ViewPager.java:733) 
at com.example.xxxxx.PlayFrets.configMenu(PlayFrets.java:150) 
at com.example.xxxxx.AsyncTask_NoteFileLoader.onPostExecute(AsyncTask_NoteFileLoader.java:125) 
at com.example.xxxxx.AsyncTask_NoteFileLoader.onPostExecute(AsyncTask_NoteFileLoader.java:9) 
at android.os.AsyncTask.finish(AsyncTask.java:651) 
at android.os.AsyncTask.access$500(AsyncTask.java:180) 
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7229) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.gu.NeckCanvasOverlay" on path: DexPathList[[zip file "/data/app/com.example.xxxxx-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.xxxxx-2/lib/arm64, /vendor/lib64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.view.LayoutInflater.createView(LayoutInflater.java:595)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:847) 
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:855) 
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:527) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
at com.example.xxxxx.FragmentNeckDisplayMenu.onCreateView(FragmentNeckDisplayMenu.java:32) 
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248) 
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) 
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613) 
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570) 
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) 
at android.support.v4.view.ViewPager.populate(ViewPager.java:1106) 
at android.support.v4.view.ViewPager.populate(ViewPager.java:952) 
at android.support.v4.view.ViewPager.setOffscreenPageLimit(ViewPager.java:733) 
at com.example.xxxxx.PlayFrets.configMenu(PlayFrets.java:150) 
at com.example.xxxxx.AsyncTask_NoteFileLoader.onPostExecute(AsyncTask_NoteFileLoader.java:125) 
The immediate problem is a ClassNotFoundException. When dealing with custom Views in a layout, a common cause for this is an incorrect class name in the layout XML, which seems to have been the issue here. Ensure that the XML tag has the correct, fully-qualified class name for the custom View class, which will be the class's package from the top of the file, prepended to the class name.
Additionally, Views inflated from your layout will be instantiated with a two-argument constructor that takes a Context and an AttributeSet. Your class definition needs to have at least that constructor to allow inflation, or you'll get a NoSuchMethodException. Also, it would make things a little simpler if you'd chain your constructors, if you're just extending View. For example:
public NeckCanvasOverlay(Context context) {
this(context, null);
}
public NeckCanvasOverlay(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
It is safer to include all three constructors for the custom View. For example, include the following Constructors for your NeckCanvasOverlay.
public NeckCanvasOverlay(Context context) {
super(context);
initView();
}
public NeckCanvasOverlay(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public NeckCanvasOverlay(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView();
}
I hope this helps. Give it a try and let me know if you get the same error.

Categories