片段中的按钮动作

时间:2013-10-06 20:48:32

标签: java android android-fragments

我有片段,有textview和button,我找到了一个解决方案,用于为片段内的按钮添加动作,但现在我必须在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

EDIT ::

   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 个答案:

答案 0 :(得分:1)

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

            article2.setText(++licznik);

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

否则它正在寻找资源。