2012-07-09 70 views
1

爲了在Facebook上分享(同時也是Gmail,短信......)後我已經使用如何在android上使用facebook意圖?

public class ShareActivity extends Activity { 

    // the button + the editText 
    Button button= (Button) findViewById(R.id.share); 
    EditText text= (EditText)findViewById(R.id.edittext); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_share);   

     button.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       Intent intent = new Intent(Intent.ACTION_SEND); 
       // type of intent 
       intent.setType("text/plain"); 
       intent.putExtra(Intent.EXTRA_TEXT, text.getText().toString()); 
       ShareActivity.this.startActivity(Intent.createChooser(intent, "Share : ")); 
      } 
     }); 
    }  
} 

,但是當我開始我的活動它之後setContentView(R.layout.activity_share);調用意外

+0

發表您的logcat的? – 2012-07-09 10:12:38

回答

0

地方這條線text= (EditText)findViewById(R.id.edittext);停止。

+0

當你在'setContentView(R.layout.activity_share);'調用之前初始化你的'EditText text'爲null。 – 2012-07-09 10:15:55

+0

是的,它的作品謝謝你,我只是移動EditText文本並將其設置爲最終,並在我的模擬器上安裝了facebook.apk應用程序。 – Angelika 2012-07-09 11:45:57

+0

如果它幫助你,然後接受答案:) – 2012-07-09 12:23:38

0

最終代碼:

公共類ShareActivity延伸活動{

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_share);  

// the button + the editText 
    Button button= (Button) findViewById(R.id.share); 
    final EditText text= (EditText)findViewById(R.id.edittext); 


    button.setOnClickListener(
      new OnClickListener() { 
       public void onClick(View v) { 
       Intent intent = new Intent(Intent.ACTION_SEND); 
        // type of intent 
         intent.setType("text/plain"); 
         intent.putExtra(Intent.EXTRA_TEXT, text.getText().toString()); 
         ShareActivity.this.startActivity(Intent.createChooser(intent, "Share : ")); 
       } 
       } 
     ); 

} 

}

相關問題