Error when accessing method from subclass - java

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).

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"/>

bottom bar button crash [duplicate]

This question already has answers here:
Could not instantiate activity - android studio
(3 answers)
Closed 1 year ago.
I have been trying to figure out why suddenly my bottom bar navigation button for a particular activity causes crashes.
On MainActivity, I have the following bottom bar nav:
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
//mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_events:
//mTextMessage.setText(R.string.title_events);
return true;
case R.id.navigation_offpeak:
//mTextMessage.setText(R.string.title_offpeak);
return true;
case R.id.navigation_deals:
Intent reservationsIntent = new Intent(MainActivity.this, DealsHomeActivity.class);
startActivity(reservationsIntent);
//mTextMessage.setText(R.string.title_bookings);
return true;
case R.id.navigation_me:
Intent memberProfileIntent = new Intent(MainActivity.this, MemberProfileActivity.class);
startActivity(memberProfileIntent);
//logout();
//mTextMessage.setText(R.string.title_me);
return true;
}
return false;
}
};
Then, my activity is instantiated like all my other activities, like the MemberProfileActivity.class.
However pushing the button to get to MemberProfileActivity.class works as expected on the phone, but when doing this for DealsHomeActivity.class results in a crash with the following logcat:
05-27 15:38:40.473 31978-31978/asia.diningcity.android E/AndroidRuntime: FATAL EXCEPTION: main
Process: asia.diningcity.android, PID: 31978
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{asia.diningcity.android/asia.diningcity.android.activities.DealsHomeActivity}: java.lang.InstantiationException: java.lang.Class cannot be instantiated
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2737)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2911)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1608)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6665)
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:781)
Caused by: java.lang.InstantiationException: java.lang.Class cannot be instantiated
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2727)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2911)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1608)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6665)
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:781)
I have been looking at the two activities in Java to see if there are any differences in the onCreate method to start the activity but none.
So I don't understand where this could be coming from.
Please note also this was working fine before, and since then I haven't touched the menu bar or that Deals activity so I don't know what could have caused this.
i believe your class is either an abstract class or a private class, abstract or private classes can not be instantiated,
Abstract classes are by definition not instantiable.
if your class is abstract or private, i assume it's something like this
private abstract class DealsHomeActivity extends AppCompatActivity
change to
public class DealsHomeActivity extends AppCompatActivity

Getting Crash When trying to initiate an Activity

Almost all of the current navigation in the application is working perfectly fine and when doing the same things for other parts i navigate with.
I am getting this error when trying to navigate from one activity to another:
FATAL EXCEPTION: main
Process: com.package, PID: 15338
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.package/ui.activity.AccountActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getId()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getId()' on a null object reference
at ui.activity.BaseActivity.setInitialFragment(BaseActivity.java:181)
at ui.activity.BaseActivity.setInitialFragment(BaseActivity.java:174)
at ui.activity.BaseActivity.setInitialFragment(BaseActivity.java:160)
at ui.activity.AccountActivity.setInitialFragment(AccountActivity.java:38)
at ui.activity.BaseActivity.onCreate(BaseActivity.java:56)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
I can track where the Null reference is being made and have tried a few things to try and bypass it but they all crash the application.
Here is the block where i initiate the activity:
#Override
public void onMyAccountOptionSelected() {
Intent intent = new Intent(this, AccountActivity.class);
startActivity(intent);
}
Then it gets caught here on view.getId(), this one is in baseActivity:
private void setInitialFragment(View view, Fragment fragment) {
if (this.getCurrentFragment() == null) {
FragmentManager fragmentManager = this.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(view.getId(), fragment).commit();
}
}
I am initially calling setInitialFragment() in AccountActivity which extends my baseActivity:
#Override
protected void setInitialFragment() {
fragment = (AccountFragment) AccountFragment.newInstance();
setInitialFragment(fragment);
}
As you can see above there is now an AccountFragment, in this class i extend from an abstractFragment and have a method to declare a newInstance:
public static Fragment newInstance() {
AccountFragment fragment = new AccountFragment();
return fragment;
}
I cant find anywhere else where it may be affecting the process used for navigating to this activity. I am hoping to find a method to make a quick fix or to find a more appropriate way to solve this.
Any help is much appreciated!
You didn't provide us with all the information needed to be really helpfull.
i'm imaging that your setInitialFragment in your AccountActivity would be something like this:
public class AccountActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.container);
AccountFragment fragment = AccountFragment.getInstance();
setInitialFragment(fragment);
}
}
Based on what you posted, your are calling 2 different methods, this:
setInitialFragment(Fragment fragment);
is not the same as this
setInitialFragment(View view, Fragment fragment)

NullPointerException (GameActivity does not open)

Please check whats the problem with my code here :
Main Activity :
I'm using Android Studio. Import statements are managed for me. It won't be necessary to write them here
public class MainActivity extends Activity implements View.OnClickListener {
SharedPreferences prefs;
String dataName = "MyData";
String intName = "MyString";
int defaultInt = 0;
public static int highScore;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonPlay = (Button) findViewById(R.id.btnPlay);
TextView textHighScore = (TextView) findViewById(R.id.textHighScore);
buttonPlay.setOnClickListener(this);
prefs = getSharedPreferences(dataName, MODE_PRIVATE);
highScore = prefs.getInt(intName, defaultInt);
textHighScore.setText("High Score: " + highScore);
}
#Override
public void onClick(View view) {
Intent intent;
intent = new Intent(this, GameActivity.class);
startActivity(intent);
}
}
When I run this application Main Activity is loaded but when I hit play button application closes. I have checked the play button's ID is O.K. I had the same problem in GameActivity but at last I figured out that the problem was with the ID but this time I think problem is with the this I used in Intent();. Activity fails to load.
Exception :
06-24 01:34:05.196 12826-12826/com.cruzerblade.memorygame E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.cruzerblade.memorygame, PID: 12826
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.cruzerblade.memorygame/com.cruzerblade.memorygame.GameActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:87)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81)
at android.view.animation.AnimationUtils.loadAnimation(AnimationUtils.java:75)
at com.cruzerblade.memorygame.GameActivity.<init>(GameActivity.java:55)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Please help me out! It took me too much time still I couldn't figure out the problem.
Quoting from your logcat:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method
'android.content.res.Resources android.content.Context.getResources()'
on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:87)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81)
at android.view.animation.AnimationUtils.loadAnimation(AnimationUtils.java:75)
at com.cruzerblade.memorygame.GameActivity.<init>(GameActivity.java:55)
This last line is the only line in the trace that is your code:
Look on line 55 of GameActivity.java. It is calling loadAnimation. There is something wrong with the animation it is trying to load (probably a missing resource ID, but it could also be a null or invalid context argument for loadAnimation.)

OnClickListener in separate class won't work

I've tried to use an OnClickListener from a different class but somehow it throws me an error. Can someone help me to solve this problem?
Thanks in advance.
public class TestClass extends Activity{
View.OnClickListener l = new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Clicked", Toast.LENGTH_LONG).show();
}};}
Part of the MainActivity:
#Override protected void onCreate(Bundle savedInstanceState) {
...
btnSpeech = (ImageButton) (findViewById(R.id.microphone));
obj=new TestClass();
btnSpeech.setOnClickListener(obj.l);
...
Error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.project/com.example.user.project.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.user.project.MainActivity.onCreate(MainActivity.java:74)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
You get a NullPointerException because your btnSpeech is null. findViewById() returns null if you use a wrong id for the view, maybe this is the problem.
What is sure is that your exception has nothing to do with the OnClickListener. If you read your stacktrace carefully you see that it says that setOnClickListener() was called on a object that was null.
And, as Mike commented, you cannot instantiate activities with the new keyword. Use startActivity() with an intent or make TestClass not extend Activity.
this is what you have just done quickly, but not if this will solve your problem
public class TestClass {
public static Context context;
public static View.OnClickListener getListener(){
return new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Clicked", Toast.LENGTH_LONG).show();
}
};
}
}
In Activity
TestClass.context = this;
my_button.setOnClickListener(TestClass.getListener());
Hope this help..

Categories

Resources