2013-07-18 62 views
4

我有自定義listview包含內容和TTS選項,而使用TTS其拋出空指針異常,Listview也不會顯示我的應用程序已停止.Error在Logcat中顯示。在CustomListview中使用TTS時出現NullPointerException

Applicationadapter.java

public class ApplicationAdapter extends ArrayAdapter<Application> implements 
TextToSpeech.OnInitListener{ 
    private List<Application> items; 
    private LayoutInflater inflator; 
    private MainActivity activity; 

    private ProgressDialog dialog; 
    public TextToSpeech tts; 
    public ImageButton btnaudioprayer; 
    public TextView text1; 

    ArrayAdapter<String> adapter; 

    public ApplicationAdapter(MainActivity context, List<Application> items){ 
     super(context, R.layout.activity_row, items); 
     this.items = items; 

     inflator = LayoutInflater.from(getContext()); 
     activity=context; 


    } 

    @Override 
    public int getCount(){ 
     return items.size(); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent){ 
     ViewHolder holder = null; 

     tts = new TextToSpeech(activity, ApplicationAdapter.this); 


     //View v = convertView; 
     if (convertView == null){ 
      convertView = inflator.inflate(R.layout.activity_row, null); 
      holder = new ViewHolder(); 
      holder.text2 = (TextView) convertView.findViewById(R.id.text2); 
      holder.text1 = (TextView) convertView.findViewById(R.id.text1); 
      holder.count = (TextView) convertView.findViewById(R.id.count); 
      holder.pray = (Button) convertView.findViewById(R.id.pray); 
      holder.chk = (CheckBox) convertView.findViewById(R.id.checkbox); 
      holder.btnSpeak = (ImageButton) convertView.findViewById(R.id.btnaudioprayer); 

      convertView.setTag(holder); 
     }else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton view, 
        boolean isChecked) { 
       int getPosition = (Integer) view.getTag(); 
       items.get(getPosition).setSelected(view.isChecked()); 

      } 
     }); 

     holder.pray.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       int getPosition= (Integer)v.getTag(); 
       StringBuffer sb1 = new StringBuffer(); 
       sb1.append("ID :"); 
       sb1.append(Html.fromHtml(""+items.get(getPosition).getId())); 
       sb1.append("\n"); 
       activity.praydata(items.get(getPosition).getId()); 

      } 


     }); 


     holder.btnSpeak.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View V) { 


        speakOut(); 
       } 

      }); 

     Application app = items.get(position); 
     holder.chk.setTag(position); 
     holder.pray.setTag(position); 
     holder.text2.setText(Html.fromHtml(app.getTitle())); 
     holder.text1.setText(Html.fromHtml(app.getContent())); 
     holder.count.setText(app.getCount()+""); 
     holder.chk.setChecked(app.isSelected()); 

     return convertView; 
    } 
    static class ViewHolder { 
     public TextView text2; 
     public TextView text1; 
     public TextView count; 
     public CheckBox chk; 
     public Button pray; 
     public ImageButton btnSpeak; 
     private TextToSpeech tts; 
    } 
    @Override 
    public void onInit(int status) { 
     if (status == TextToSpeech.SUCCESS) { 

      int result = tts.setLanguage(Locale.US); 

      if (result == TextToSpeech.LANG_MISSING_DATA 
        || result == TextToSpeech.LANG_NOT_SUPPORTED) { 
       Log.e("TTS", "This Language is not supported"); 
      } else { 

       speakOut(); 
      } 

     } else { 
      Log.e("TTS", "Initilization Failed!"); 
     } 


    } 

    private void speakOut() { 

      String text = text1.getText().toString(); 

      tts.speak(text, TextToSpeech.QUEUE_FLUSH, null); 
     } 

這裏我提到我logcat的錯誤也。在我的logcat錯誤中,它顯示錯誤在這一行 String text = text1.getText().toString();。它扔NullPointerException它也顯示log.e文件。它沒有運行speakout()方法。

07-18 12:17:11.456: E/TTS(2314): This Language is not supported 
07-18 12:17:11.456: I/TextToSpeech(2314): Connected to ComponentInfo{com.svox.pico/com.svox.pico.PicoService} 
07-18 12:17:11.586: D/AndroidRuntime(2314): Shutting down VM 
07-18 12:17:11.586: W/dalvikvm(2314): threadid=1: thread exiting with uncaught exception (group=0x40a70930) 
07-18 12:17:11.597: E/AndroidRuntime(2314): FATAL EXCEPTION: main 
07-18 12:17:11.597: E/AndroidRuntime(2314): java.lang.NullPointerException 
07-18 12:17:11.597: E/AndroidRuntime(2314):  at com.example.jsonandroid.ApplicationAdapter.speakOut(ApplicationAdapter.java:182) 
07-18 12:17:11.597: E/AndroidRuntime(2314):  at com.example.jsonandroid.ApplicationAdapter.onInit(ApplicationAdapter.java:168) 
07-18 12:17:11.597: E/AndroidRuntime(2314):  at android.speech.tts.TextToSpeech.dispatchOnInit(TextToSpeech.java:640) 
07-18 12:17:11.597: E/AndroidRuntime(2314):  at android.speech.tts.TextToSpeech.access$1000(TextToSpeech.java:52) 
07-18 12:17:11.597: E/AndroidRuntime(2314):  at android.speech.tts.TextToSpeech$Connection.onServiceConnected(TextToSpeech.java:1297) 
07-18 12:17:11.597: E/AndroidRuntime(2314):  at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1101) 
07-18 12:17:11.597: E/AndroidRuntime(2314):  at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1118) 
07-18 12:17:11.597: E/AndroidRuntime(2314):  at android.os.Handler.handleCallback(Handler.java:725) 
07-18 12:17:11.597: E/AndroidRuntime(2314):  at android.os.Handler.dispatchMessage(Handler.java:92) 
07-18 12:17:11.597: E/AndroidRuntime(2314):  at android.os.Looper.loop(Looper.java:137) 
07-18 12:17:11.597: E/AndroidRuntime(2314):  at android.app.ActivityThread.main(ActivityThread.java:5039) 
07-18 12:17:11.597: E/AndroidRuntime(2314):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-18 12:17:11.597: E/AndroidRuntime(2314):  at java.lang.reflect.Method.invoke(Method.java:511) 
07-18 12:17:11.597: E/AndroidRuntime(2314):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 

在我的列表視圖custome我有文本和TTS鍵,同時點擊該按鈕有播放內容的音頻。

+0

delcare一個類成員獲取按鈕上的文本單擊將其分配給變量,然後調用'speakOut()'。 – Raghunandan

+0

@Raghunandan什麼哥們,我有什麼做 –

+0

@Raghunandan我想文本1只d –

回答

2

我會刪除的speakOut();的呼叫onInit(int status),我會修改speakOut();功能是這樣的:

private void speakOut(String text) { 
    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null); 
} 

和你onclicklistener我會在這裏改成這樣:

@Override 
public void onClick(View v) { 
    View parent = (View)v.getParent(); 
    ViewHolder vh = (ViewHolder)parent.getTag(); 
    TextView tv = vh.text1; 
    speakOut(tv.getText().toString()); 
} 

這應該工作爲你。

+0

現在儀式後改變我的佈局顯示該行中的錯誤TextView tv = vh.text1;它再次拋出Null指針異常 –

3

你的變量text1沒有初始化,但申報,因此做了text1.getText(),你基本上從null試圖getText()

也許你想要的是holder.text1(來自ViewHolderholder)?

+1

在'getView'。 'holder.text1'。 – Raghunandan

+0

是的,我想text1,因爲我想將text1轉換成音頻,text1它的持有人,如何得到那一個 –

+0

@ buhake辛迪,raghunandan是正確的,我想text1只有花花公子 –

相關問題