2013-10-06 45 views
-1

我有片段,並有textview和按鈕,我發現了一個解決方案,爲片段內的按鈕添加動作,但現在我必須在textview中更改文本。我是這樣做的,但它不起作用,你能檢查它嗎?片段中的按鈕動作

片段:

public class ArticleFragment extends Fragment { 
    static public int licznik=0; 
    public View view; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 

     view = inflater.inflate(R.layout.article_view, container, false); 
     final TextView article2 = (TextView)view.findViewById(R.id.article); 

     Button menuButton = (Button)view.findViewById(R.id.button1); 
     menuButton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       article2.setText(++licznik); 
      } 
     }); 

     return view; 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 

     TextView article = (TextView)view.findViewById(R.id.article); 
     article.setText("Cos tam"); 
    } 

}

XML片段:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/article" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/reso" 
     android:onClick="action" /> 

</LinearLayout> 

一切都是好,在我不點擊一個按鈕,然後應用程序被sttoped。

乾杯; d

編輯::

10-06 20:58:18.921: E/Trace(828): error opening trace file: No such file or directory (2) 
    10-06 20:58:19.841: I/Choreographer(828): Skipped 39 frames! The application may be doing too much work on its main thread. 
    10-06 20:58:19.882: D/gralloc_goldfish(828): Emulator without GPU emulation detected. 
    10-06 20:58:21.931: W/ResourceType(828): No package identifier when getting value for resource number 0x00000001 
    10-06 20:58:21.931: D/AndroidRuntime(828): Shutting down VM 
    10-06 20:58:21.931: W/dalvikvm(828): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 
    10-06 20:58:21.971: E/AndroidRuntime(828): FATAL EXCEPTION: main 
    10-06 20:58:21.971: E/AndroidRuntime(828): android.content.res.Resources$NotFoundException: String resource ID #0x1 
    10-06 20:58:21.971: E/AndroidRuntime(828): at android.content.res.Resources.getText(Resources.java:230) 
    10-06 20:58:21.971: E/AndroidRuntime(828): at android.widget.TextView.setText(TextView.java:3769) 
    10-06 20:58:21.971: E/AndroidRuntime(828): at com.example.developer.ArticleFragment$1.onClick(ArticleFragment.java:43) 
    10-06 20:58:21.971: E/AndroidRuntime(828): at android.view.View.performClick(View.java:4204) 
    10-06 20:58:21.971: E/AndroidRuntime(828): at android.view.View$PerformClick.run(View.java:17355) 
    10-06 20:58:21.971: E/AndroidRuntime(828): at android.os.Handler.handleCallback(Handler.java:725) 
    10-06 20:58:21.971: E/AndroidRuntime(828): at android.os.Handler.dispatchMessage(Handler.java:92) 
    10-06 20:58:21.971: E/AndroidRuntime(828): at android.os.Looper.loop(Looper.java:137) 
    10-06 20:58:21.971: E/AndroidRuntime(828): at android.app.ActivityThread.main(ActivityThread.java:5041) 
    10-06 20:58:21.971: E/AndroidRuntime(828): at java.lang.reflect.Method.invokeNative(Native Method) 
    10-06 20:58:21.971: E/AndroidRuntime(828): at java.lang.reflect.Method.invoke(Method.java:511) 
    10-06 20:58:21.971: E/AndroidRuntime(828): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
    10-06 20:58:21.971: E/AndroidRuntime(828): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
    10-06 20:58:21.971: E/AndroidRuntime(828): at dalvik.system.NativeStart.main(Native 
Method) 
+1

發表您的logcat的轉儲,如果你想幫助:) – marshallino16

+0

對不起,我的錯:d – bkowalczyyk

回答

1

我猜這就是問題所在,雖然你們已經張貼任何日誌轉儲:

  article2.setText(++licznik); 

  article2.setText(String.valueOf(++licznik)); 

否則它正在尋找一個資源烏爾斯河。

+0

我不相信,這是它...但是,非常感謝; d – bkowalczyyk

+0

@bkowalczyyk您剛剛發佈的logcat的指示它**是**的問題,因爲它正在尋找一個資源,因爲你傳遞了一個整數而不是一個字符串。 –

+0

其中,我沒有看到它,我無法讀取logcat ...:/ – bkowalczyyk