I'm new to Java and Android development. I'm developing an app for children, in the Java section contain some classes. One of the classes is for swapping the pages named ColorCustomSwipeAdapter, and there is another class for converting text to speech named ReadName that contain a method named speakOut, the problem starts when I try to call the method speakOut through the ColorCustomSwipeAdapter class. It throws NPE and the app crashes.
Here is the code:
ColorCustomSwipeAdapter.java
package com.example.ibrahim.toddlerssmartbook;
import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Locale;
/**
* Created by Ibrahim on 3/19/2016.
*/
public class ColorCustomSwipeAdapter extends PagerAdapter {
ReadName sayName=new ReadName() ;
private int[] image_resource = {R.drawable.red,R.drawable.green,R.drawable.blue,R.drawable.pink,R.drawable.gray ,R.drawable.yellow,R.drawable.brown,
R.drawable.black,R.drawable.purple,R.drawable.orange,R.drawable.white};
private String[] color_name= {"Red","Green","Blue","Pink","Gray","Yellow","Brown","Black","Purple","Orange","White"};
private Context ctx;
private LayoutInflater layoutInflater;
public ColorCustomSwipeAdapter(MainActivity ctx){this.ctx=ctx;}
@Override
public int getCount() {
return image_resources.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view==(LinearLayout)object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater= (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view=layoutInflater.inflate(R.layout.swipe_layout,container,false);
ImageView imageView=(ImageView)item_view.findViewById(R.id.image_view);
TextView textView=(TextView)item_view.findViewById(R.id.name);
imageView.setImageResource(image_resources[position]);
textView.setText(color_name[position]);
sayName.speakOut(color_name[position]);//The ERROR IS HERE
container.addView(item_view);
return item_view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout)object);
}
}
Readname.java
package com.example.ibrahim.toddlerssmartbook;
import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import java.io.IOException;
import java.util.Locale;
/**
* Created by Ibrahim on 4/7/2016.
*/
public class ReadName extends AppCompatActivity {
private int result;
public ReadName() {
result=0;
}
TextToSpeech ttsObject=new TextToSpeech(this,new TextToSpeech.OnInitListener(){
@Override
public void onInit(int status){
if(status==TextToSpeech.SUCCESS){
result = ttsObject.setLanguage(Locale.US);
}else{
Toast.makeText(getApplicationContext(),"Feature not supported in your device.",Toast.LENGTH_SHORT).show();
}}
});
public void speakOut(String name){
if (result == TextToSpeech.LANG_NOT_SUPPORTED || result == TextToSpeech.LANG_MISSING_DATA) {
Toast.makeText(getApplicationContext(), "Feature not supported in your device.", Toast.LENGTH_SHORT).show();
} else {
ttsObject.speak(name, TextToSpeech.QUEUE_FLUSH, null);
}
}
@Override
public void onDestroy() {
if (ttsObject != null) {
ttsObject.stop();
ttsObject.shutdown();
}
super.onDestroy();
}
}