splash screen is not working in android 6 - java

I have added splash screen in my app but the app crashes when is run on Android 6. The interesting thing is that the app runs fine on Android 9.
Don't know what to do.
Target SDK - 28.
min SDK= 19
it is showing error on oncreate method while run app on android 6
splash.java
import android.os.Bundle;
import android.os.Handler;
public class splash extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // it showing error here.
setContentView(R.layout.activity_splash);
int SPLASH_TIME_OUT = 4000;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent splashintent=new Intent(splash.this,MainActivity.class);
startActivity(splashintent);
finish();
}
}, SPLASH_TIME_OUT);
}
}
logs
06-22 16:17:30.930 10294-10294/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.napps.wallpaper, PID: 10294
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.napps.wallpaper/com.napps.wallpaper.splash}: android.content.res.Resources$NotFoundException: Resource ID #0x7f07009e
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2655)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1572)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5896)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f07009e
at android.content.res.Resources.getValue(Resources.java:1595)
at androidx.appcompat.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:331)
at androidx.appcompat.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:198)
at androidx.appcompat.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:86)
at androidx.appcompat.app.AppCompatDelegateImpl.<init>(AppCompatDelegateImpl.java:260)
at androidx.appcompat.app.AppCompatDelegate.create(AppCompatDelegate.java:182)
at androidx.appcompat.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520)
at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:71)
at com.napps.wallpaper.splash.onCreate(splash.java:13)
at android.app.Activity.performCreate(Activity.java:6298)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2608)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1572) 
at android.os.Handler.dispatchMessage(Handler.java:111) 
at android.os.Looper.loop(Looper.java:207) 
at android.app.ActivityThread.main(ActivityThread.java:5896) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 

There might be problem with your drawable Image. While copying image Please check the folder in which you are copying.
Try to add the image in drawable folder not drawable-21
I would suggest to remove old image and Add Image again to proper directory.

Related

Hi! I am new to android app development. I tried to create a simple counter but after build getting error "Unable to instantiate activity"

public abstract class MainActivity extends AppCompatActivity {
Button decrement;
Button increment;
TextView counter_view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Log.i("tag", "onCreate: Created Successfully");
increment=findViewById(R.id.inc_btn);
decrement=findViewById(R.id.dec_btn);
counter_view=findViewById(R.id.counter);
increment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String cnt_text=counter_view.getText().toString();
int cnt_no= Integer.parseInt(cnt_text);
cnt_no=cnt_no+1;
counter_view.setText(cnt_no+"");
}
});
decrement.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String cnt_text=counter_view.getText().toString();
int cnt_no=Integer.parseInt(cnt_text);
cnt_no=cnt_no-1;
counter_view.setText(cnt_no+"");
}
});
}
Error:
2021-02-10 00:29:06.870 16714-16714/? E/Zygote: v2
2021-02-10 00:29:06.871 16714-16714/? E/Zygote: accessInfo : 0
2021-02-10 00:29:07.017 16714-16714/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.counter_app, PID: 16714
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.counter_app/com.example.counter_app.MainActivity}: java.lang.InstantiationException: java.lang.Class<com.example.counter_app.MainActivity> cannot be instantiated
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2849)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
Caused by: java.lang.InstantiationException: java.lang.Class<com.example.counter_app.MainActivity> cannot be instantiated
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1086)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2839)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045) 
at android.app.ActivityThread.-wrap14(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6776) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408) 
I suppose this is your first screen/activity/class that is displayed on launch i.e it is your launcher activity. If yes,
the launcher activity cannot be abstract. Because when an app is launched from the home screen on an Android device, the Android OS creates an instance of the activity in the application you have declared to be the launcher activity. And abstract classes can not be instantiated, they can only be sub-classed.
Please remove the word abstract before your class name.
add to your manifest file this line
<activity android:name="your.package.name.MainActivity"/>

New To Android Development: App Runs On My MacBook Pro But Fails With Kotlin Typecast Exception on iMac

This is the weirdest thing.. A few weeks ago I ran into this issue and found the solution to be to update the Gradle Runtime, which I did on both computers. I decided I wanted to sit at my desk with my iMac and work last night, but could not get the app to run do to a Kotlin Typecast Exception. I switched to the remote branch I have pulled on my MacBook, but got this error:
09-23 12:28:29.112 4141-4141/com.example.pmarl.peedeehealthadvisor E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pmarl.peedeehealthadvisor, PID: 4141
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pmarl.peedeehealthadvisor/com.example.pmarl.peedeehealthadvisor.MainActivity}: kotlin.TypeCastException: null cannot be cast to non-null type android.widget.Button
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: kotlin.TypeCastException: null cannot be cast to non-null type android.widget.Button
at com.example.pmarl.peedeehealthadvisor.MainActivity.onCreate(MainActivity.kt:31)
at android.app.Activity.performCreate(Activity.java:5990)
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:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
I panic'd and tested the MacBook again, but it worked fine.
`
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{
private Button MyHealthData;
private Button MyHealthResources;
private boolean firstStart;
#Override
protected void onCreate(Bundle saveInstanceState)
{
super.onCreate(saveInstanceState);
setContentView(R.layout.activity_main);
/*First time open code*/
SharedPreferences app_preferences = getApplicationContext()
.getSharedPreferences("MyPref",0);
SharedPreferences.Editor editor = app_preferences.edit();
firstStart = app_preferences.getBoolean("first_time_start",true);
/*If statement to see if the app has been open before or not*/
if(firstStart)
{
/*If the app hasn't been opened before, it runs the following code
* and sets firstStart to false*/
editor.putBoolean("first_time_start",false);
editor.commit();
//do your one time code here
launchFirstTimeLogIn();
//Toast.makeText(this,"This is first app run!", Toast.LENGTH_LONG).show();
}
else
{
//app open directly
Toast.makeText(this, "Welcome!!!", Toast.LENGTH_LONG).show();
}
this.MyHealthData = (Button) findViewById(R.id.MyHealthData);
this.MyHealthData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
launchMyHealthData();
}
});
this.MyHealthResources = (Button) findViewById(R.id.HealthResources);
this.MyHealthResources.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
launchSearchServiceActivity();
}
});
}
private void launchMyHealthData()
{
Intent intent = new Intent (this, MyHealthDataActivity.class);
intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK | intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
private void launchSearchServiceActivity()
{
Intent intent = new Intent (this, SearchServiceActivity.class);
intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK | intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
private void launchFirstTimeLogIn()
{
Intent intent = new Intent(this, FirstTimeLogin.class);
intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK | intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
}
`
Both are the same Gradle versions, and the same version of Android Studio. I"m not sure what this would be caused by? My logic says that if it is the Button causing the issue, like the posts I found say, it wouldn't work on my MacBook.
Thank you for the help everyone!
null cannot be cast to non-null type android.widget.Button
I'm assuming that you declared a Button like this: (for example)
mineBtn = findViewById(R.id.yourbtnid) as Button
Add "?" after the as:
mineBtn = findViewById(R.id.yourbtnid) as? Button
Then error should be gone.
Edit: Remove this. before declaring widgets. And declare a widget like this:
MyHealthData = (Button) findViewById(R.id.MyHealthData);
Note that it's better to change R.id.MyHealthData from xml layout to be make sure it won't cause other issues in future.

Why is my app crashing when I try to open a Navigation Drawer Activity from an Empty Activity? [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I am a beginner in android My app starts with a Splash Screen which goes to an Empty Activity where it takes some info. It works fine till here. After that I used a submit button to guide the user to the Navigation drawer activity, but somehow the app crashes. I tried to replace the navigation drawer activity with an Empty activity on button click, which worked fine. I also tried launching the navigation drawer activity from splash screen, didn't work as well.
submitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent gotoChoiceSelection = new Intent(getApplicationContext(), ChoiceSelection.class);
startActivity(gotoChoiceSelection);
}
});
(ChoiceSelection is my Navigation drawer activity)
logcat
08-09 20:39:15.765 19793-19793/comviewappisome.google.sites.iamengineermu E/AndroidRuntime: FATAL EXCEPTION: main
Process: comviewappisome.google.sites.iamengineermu, PID: 19793
java.lang.RuntimeException: Unable to start activity ComponentInfo{comviewappisome.google.sites.iamengineermu/comviewappisome.google.sites.iamengineermu.ChoiceSelection}: android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2904)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2986)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1671)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:206)
at android.app.ActivityThread.main(ActivityThread.java:6784)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:852)
Caused by: android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.app.ActionBar.getThemedContext()' on a null object reference
at comviewappisome.google.sites.iamengineermu.NavigationDrawerFragment.onCreateView(NavigationDrawerFragment.java:101)
at android.app.Fragment.performCreateView(Fragment.java:2611)
at android.app.FragmentManagerImpl.ensureInflatedFragmentView(FragmentManager.java:1459)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1247)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1454)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1701)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3611)
at android.app.FragmentController.onCreateView(FragmentController.java:98)
at android.app.Activity.onCreateView(Activity.java:6196)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
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:428)
at android.app.Activity.setContentView(Activity.java:2659)
at comviewappisome.google.sites.iamengineermu.ChoiceSelection.onCreate(ChoiceSelection.java:37)
at android.app.Activity.performCreate(Activity.java:6984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1235)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2857)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2986)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1671)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:206)
at android.app.ActivityThread.main(ActivityThread.java:6784)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:852)
Suggest me a solution for this.
if you are using a activity to Navigation drawer activity
you can try this in the place of getApplicationContext()
I think you should change your java code like this:
submitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent gotoChoiceSelection = new Intent(this, ChoiceSelection.class);
startActivity(gotoChoiceSelection );
}
});
Try this code, it worked for me:
submitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent gotoChoiceSelection = new Intent(this, ChoiceSelection.class);
startActivity(gotoChoiceSelection );
}
});
Just replace getApplicationContext() with this.

Error when accessing method from subclass

i`m new to android development and i wanted to make an audio recorder, when i want to access the method for starting of recording from my main activity it always gives an error. Below is my code. I hope you can help me:
This is the mainActivity:
public class MainActivity extends AppCompatActivity {
Boolean isRecording = false;
Record record;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
record = new Record();
}
public void recordAudio(View view){
if(!isRecording)
{
isRecording = true;
record.startRecording();
}
else{
isRecording = false;
record.stopRecording();
}
}
Below is the subclass:
public class Record extends MainActivity {
public void startRecording() {
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, getBufferSize());
int i = recorder.getState();
if (i == 1)
recorder.startRecording();
isRecording = true;
recordingThread = new Thread(new Runnable() {
#Override
public void run() {
writeAudioDataToFile();
}
}, "AudioRecorder Thread");
recordingThread.start();
buttonRecord.setText(R.string.button_stop_record);
}
Thanks for your help!
Here is the exact error code:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com, PID: 31778
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:6199)
at android.widget.TextView.performClick(TextView.java:11090)
at android.view.View$PerformClick.run(View.java:23647)
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:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
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:6199) 
at android.widget.TextView.performClick(TextView.java:11090) 
at android.view.View$PerformClick.run(View.java:23647) 
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:6682) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 
Caused by: java.lang.IllegalArgumentException: Invalid audio buffer size.
at android.media.AudioRecord.audioBuffSizeCheck(AudioRecord.java:751)
at android.media.AudioRecord.<init>(AudioRecord.java:385)
at android.media.AudioRecord.<init>(AudioRecord.java:289)
at com.Record.startRecording(Record.java:63)
at com.MainActivity.recordAudio(MainActivity.java:35)
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:6199) 
at android.widget.TextView.performClick(TextView.java:11090) 
at android.view.View$PerformClick.run(View.java:23647) 
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:6682) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 
In Android Activity is usually stared by the user code but the object creation is not the responsibility of the user, rather it is the Android framework which does this.
Your case has Record extends MainActivity and MainActivity being an Activity makes Record also an Activity. So you need to start it either make it the launcher main activity in manifest file or use startActivity() (or startActivityForResult()).
The code record = new Record(); Here you create the instance of Record yourself that too in the parent class MainActivity. This is not a very good idea from both programming in Android and Java Object oriented point of view. (Hence you can but should not choose to do so)
Refer one answer from another post
https://stackoverflow.com/a/14956056/504133
I suggest you to make simple Android application with two or three activities, with each having simple UI layout. You can learn there and then apply the concepts for more complex application as AudioRecorder.
The error says com.Record.startRecording(Record.java:63) is the cause of: java.lang.IllegalArgumentException: Invalid audio buffer size.
Please call AudioRecord.getMinBufferSize(int sampleRateInHz, int channelConfig, int audioFormat) to make sure your buffer size is big enough before you instantiate a new AudioRecord. Just like it says in the documentation.
Please note that you should also check if your AudioRecord instance was initialized correctly by calling getState() (and checking that it returns STATE_INITIALIZED).

Android recieving classcastexception despite being in correct class

I have an activity with a viewpager containing 2 fragments, inside each fragment contains a list, from this list a button can be pressed that creates a new fragment in the viewpager, this works fine until I create a new activity and close it, after the new activity is closed and I return to the previous activity pressing the button to create a new fragment again now gives a classcastexception, almost as if the closed activity is still open, but it is not, I have tried searching this issue and cant find any examples of this being encountered by anyone else which makes me wonder how this could be happening, I will post some code examples below, any help will go a long way thanks.
Click listener in ListAdapter:
holder.rlStatusContainer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//add new comment fragment and set page
try {
((HomeFragmentActivity) activity).addCommentFragment(data.get(finalPosition));
} catch (Exception e) {
e.printStackTrace();
}
}
});
Method in activity:
public void addCommentFragment(ArrayList<Object> fragmentExtras) {
list.add(NavigationFragment.newInstance(9, getApplicationContext(), this, fragmentExtras));
adapter.notifyDataSetChanged();
pager.setCurrentItem(list.size());
}
Error received:
12-13 13:37:50.177 17262-17262/test.test.com.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: test.test.com.test, PID: 17262
java.lang.ClassCastException: test.test.com.test.activities.NewPostActivity cannot be cast to test.test.com.test.activities.HomeFragmentActivity
at test.test.com.test.adapters.ListHomeAdapter$4.onClick(ListHomeAdapter.java:462)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Categories

Resources