我是Java和Android开发的新手。我正在为儿童开发一个应用程序,在Java部分包含一些类。其中一个类用于交换名为ColorCustomSwipeAdapter
的页面,还有另一个用于将文本转换为包含名为ReadName
的方法的speakOut
语音的类,问题在我尝试时开始通过speakOut
类调用方法ColorCustomSwipeAdapter
。它抛出NPE
并且应用程序崩溃。
以下是代码:
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();
}
}