2013-08-03 48 views
2

我試圖在警報對話框中顯示一些文本作爲超鏈接。部分過程需要我使用SpannableString來格式化一些文本。問題是我的應用程序在我的代碼的SpannableString部分遇到運行時錯誤。Spannable String Error

TextView Tv= (TextView) findViewById(R.id.textView3); 
SpannableString s = new SpannableString("www.google.com"); 
Linkify.addLinks(s, Linkify.WEB_URLS); 
Tv.setText(s); 
Tv.setMovementMethod(LinkMovementMethod.getInstance()); 

我在DDMS中查找,錯誤說Java.Lang.NullPointerException。有沒有人經歷過這個?我應該能夠傳遞SpannableString方法一個硬編碼的字符串。我不知道爲什麼它會像這樣崩潰。

這是我的java文件中的OnCreate函數:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    //System.out.println("+++++++++++++++++++++++"); 
    TextView Tv= (TextView) findViewById(R.id.textviewH); 

    Tv.setText("GO!"); 
    //System.out.println("+++++++++++++++++++++++");   
    SpannableString s = new SpannableString("www.google.com"); 
    Linkify.addLinks(s, Linkify.WEB_URLS); 
    Tv.setText(s); 
    Tv.setMovementMethod(LinkMovementMethod.getInstance()); 


    AlertDialog.Builder dialog = new AlertDialog.Builder(this); 
    dialog.setTitle("About T2M"); 
    dialog.setIcon(R.drawable.ic_launcher); 
    dialog.setView(getLayoutInflater().inflate(R.layout.activity_about_t2m, null)); 
    dialog.setCancelable(false); 
    dialog.setPositiveButton(android.R.string.ok, 
     new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int id) 
    { 
     dialog.cancel(); 
    } 
}); 
    //System.out.println("+++++++++++++++++++++++"); 
    dialog.create(); 
    dialog.show(); 


} 

這是我的XML文件中的文本視圖:

​​

My Layout

+0

你可以給我們一行NullPointerException發生PLZ嗎? – Aleks

+0

我做了一些使用println的測試,發現錯誤發生在SpannableString施工線 –

+1

你確定它不能來自** findViewById **嗎? 因爲我想不出爲什麼** SpannableString **會出現NullPointerException ... – Aleks

回答

1

好了,我認爲來自於通脹過程中的問題。 看起來好像你正在嘗試在膨脹佈局之前訪問TextView。所以findViewById找不到任何東西,因爲它會在活動佈局中搜索。

這裏的技巧將被虛增您自定義佈局第一(,這裏是你能做什麼個例:

AlertDialog.Builder dialog = new AlertDialog.Builder(this); 
    //Inflating the custom Layout 
    View view = LayoutInflater.from(this).inflate(R.layout.activity_about_t2m, null); 

    //Searching for the TextView in it 
    TextView tv = (TextView)view.findViewById(R.id.textviewH); 

    //Then making the links 
    SpannableString s = new SpannableString("www.google.fr"); 
    Linkify.addLinks(s, Linkify.WEB_URLS); 

    //Adding the text to the View and make the links clickable 
    tv.setText(s); 
    tv.setMovementMethod(LinkMovementMethod.getInstance()); 

    //Finally applying the custom view we inflated (R.layout.activity_about_t2m) on the AlertDialog and .... 
    dialog.setView(view); 
    dialog.setTitle("About T2M"); 
    dialog.setIcon(R.drawable.ic_launcher); 
    dialog.setCancelable(false); 
    dialog.setPositiveButton(android.R.string.ok, 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) 
       { 
        dialog.cancel(); 
       } 
      } 
    ); 

編輯1: 我犯了一個錯誤這個例子...在你情況下,getApplicationContext()應該(我做的代碼修復)

此處瞭解詳情: Android - Linkify Problem

編輯2: 好了,所以現在應該是可以理解的吧?

+0

你在創建後如何處理視圖?我沒有看到它在任何地方使用 –

+0

我發現裏面的TextView: TextView tv =(TextView)view.findViewById(R.id.text); – Aleks

+0

好吧即時將要嘗試這 –

1

該代碼是工作的罰款我。我希望這會對你有所幫助。

TextView richTextView = (TextView) findViewById(R.id.text_aboutus); 
    // this is the text we'll be operating on 
    SpannableString text = new SpannableString("Hi Click on Google.com and yahoo.com or Pmstudy.com"); 
    text.setSpan(
      new URLSpan("http://www.google.com/"), 
      13, 22, 0); 
    text.setSpan(new URLSpan("http://www.yahoo.com/"), 
      28, 36, 0); 
    text.setSpan(new URLSpan("http://www.pmstudy.com"), 41, 51, 0); 

    richTextView.setMovementMethod(LinkMovementMethod.getInstance()); 
    richTextView.setText(text, BufferType.SPANNABLE); 

享受......

感謝, 穆克什

+0

我正在使用自定義警報對話框。我該如何做到這一點? –

1
**You can Use Same code in your custom Dialog as well. This is currently working for me. 

you need to inflate your custom-dialog-layout in your code .** 

custom_dialog.xml 

//this xml is for custom Dialog. 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/text_aboutus" 
     android:text="Hi Click on Google.com and yahoo.com or Pmstudy.com"/> 

</LinearLayout> 



package PackageName; 

// import Files 

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

    Button show_dialog=(Button) findViewById(R.id.imgView); 
    show_dialog.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      Dialog d=new Dialog(MainActivity.this); 
      View v=getLayoutInflater().inflate(R.layout.custom_dialog, null); 
      TextView richTextView = (TextView) v.findViewById(R.id.text_aboutus); 
      SpannableString text = new SpannableString("Hi Click on Google.com and yahoo.com or Pmstudy.com"); 
      text.setSpan(
        new URLSpan("http://www.google.com/"), 
        13, 22, 0); 
      text.setSpan(new URLSpan("http://www.yahoo.com/"), 
        28, 36, 0); 
      text.setSpan(new URLSpan("http://www.pmstudy.com"), 41, 51, 0); 

      richTextView.setMovementMethod(LinkMovementMethod.getInstance()); 
      richTextView.setText(text, BufferType.SPANNABLE); 

      d.setContentView(v); 
      d.show(); 

     } 
    }); 

    } 

} 
相關問題