[英]Unfortunately, my Android app has stopped
我對Android開發非常陌生,當我制作了一個非常簡單的應用程序時,卻感到非常困惑,並收到了一個非常非描述性的錯誤:“不幸的是,MyApp已停止”。 沒有語法錯誤。 只是這個有點無用的信息。 這是我所做的。
方法:
public void DoStuff(){
TextView tv = (TextView)this.findViewById(R.id.textView1);
tv.setText("Hello dude");
}
按鈕xml:
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/textView1"
android:layout_marginTop="82dp"
android:text="Button"
android:onClick="DoStuff" />
我希望你能幫助我。
我在其他地方找到了解決方案。 看起來DoStuff需要將View作為參數,因此看起來像這樣:
public void DoStuff(View v){
TextView tv = (TextView)this.findViewById(R.id.textView1);
tv.setText("Hello dude");
}
為什么,我不知道,但是它有效:-)
在Android中,對於不同類型的事件和動作,相應的偵聽器具有其指定的功能簽名。 例如,對於按鈕的動作偵聽器,該函數必須具有以下簽名:
public void functionName(View view);
在您的布局文件中,對於Button的onClick屬性,您將僅需要提供函數名稱。
其中View參數告訴觸發該操作的視圖對象(例如,您所用的按鈕)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.