-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>

一切正常,在我不单击按钮期间,应用程序将停止。

干杯;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)
4

1 回答 1

1

我猜这是问题所在,即使您没有发布日志转储:

            article2.setText(++licznik);

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

否则它正在寻找资源。

于 2013-10-06T20:59:46.947 回答