-3

I have a very very simple project in which what I am trying to achieve is to that when a user click on the button it display the text. But in the ADB it says unfortunately showtext stop working. What is the reason behind this?

I had tried but not got any result. My code is fine but not working. Below is java code for it. I omitted xml code as it contain only the button and a text view not so complex.

public class MainActivity extends Activity {

    Button  ShowText; 
    TextView DisplayText;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ShowText=(Button)findViewById(R.id.bShowText);

        }
        void ShowMeText(View view){
            DisplayText=(TextView)findViewById(R.id.tvShowText);
            DisplayText.setVisibility(View.VISIBLE);

        }
    }

Try solve my this little issue. Thanks in Advance.

As someone asked for logcat error here is it -

06-11 06:51:32.376: E/AndroidRuntime(1223): FATAL EXCEPTION: main
06-11 06:51:32.376: E/AndroidRuntime(1223): java.lang.IllegalStateException: Could not find a method ShowMeText(View) in the activity class com.mylearning.showtext.MainActivity for onClick handler on view class android.widget.Button with id 'bShowText'
06-11 06:51:32.376: E/AndroidRuntime(1223):     at android.view.View$1.onClick(View.java:3620)
06-11 06:51:32.376: E/AndroidRuntime(1223):     at android.view.View.performClick(View.java:4240)
06-11 06:51:32.376: E/AndroidRuntime(1223):     at android.view.View$PerformClick.run(View.java:17721)
06-11 06:51:32.376: E/AndroidRuntime(1223):     at android.os.Handler.handleCallback(Handler.java:730)
06-11 06:51:32.376: E/AndroidRuntime(1223):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-11 06:51:32.376: E/AndroidRuntime(1223):     at android.os.Looper.loop(Looper.java:137)
06-11 06:51:32.376: E/AndroidRuntime(1223):     at android.app.ActivityThread.main(ActivityThread.java:5103)
06-11 06:51:32.376: E/AndroidRuntime(1223):     at java.lang.reflect.Method.invokeNative(Native Method)
06-11 06:51:32.376: E/AndroidRuntime(1223):     at java.lang.reflect.Method.invoke(Method.java:525)
06-11 06:51:32.376: E/AndroidRuntime(1223):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
06-11 06:51:32.376: E/AndroidRuntime(1223):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-11 06:51:32.376: E/AndroidRuntime(1223):     at dalvik.system.NativeStart.main(Native Method)
06-11 06:51:32.376: E/AndroidRuntime(1223): Caused by: java.lang.NoSuchMethodException: ShowMeText [class android.view.View]
06-11 06:51:32.376: E/AndroidRuntime(1223):     at java.lang.Class.getConstructorOrMethod(Class.java:423)
06-11 06:51:32.376: E/AndroidRuntime(1223):     at java.lang.Class.getMethod(Class.java:787)
06-11 06:51:32.376: E/AndroidRuntime(1223):     at android.view.View$1.onClick(View.java:3613)
06-11 06:51:32.376: E/AndroidRuntime(1223):     ... 11 more
Rajeev Kumar
  • 435
  • 2
  • 7
  • 20

6 Answers6

3

You need to add android:onClick="ShowMeText" in your Button in your Layout. like so

           <Button .............
            android:onClick="ShowMeText"
            ......... />

and used lower case for method creation as Java naming Convention . like so showmetext

and also defined your ShowMeText(...) method as Public

M D
  • 47,665
  • 9
  • 93
  • 114
2

ShowMeText(View v) has to be public.

Btw. start function names with lower case characters as specified by Java code style.

brummfondel
  • 1,202
  • 1
  • 8
  • 11
1
 <Button
     android:id="@+id/tvShowText"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:onClick="ShowMeText" />

in java code

 public void ShowMeText(View v){
   DisplayText=(TextView)findViewById(R.id.tvShowText);
   DisplayText.setVisibility(View.VISIBLE);
 }
MilapTank
  • 9,988
  • 7
  • 38
  • 53
Abin
  • 540
  • 4
  • 15
1
public class MainActivity extends Activity implements OnClickListener {

Button ShowText;
TextView DisplayText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ShowText = (Button) findViewById(R.id.bShowText);
    DisplayText = (TextView) findViewById(R.id.tvShowText);
    ShowText.setOnClickListener(this);

}


@Override
public void onClick(View v) {

    if (v.getId() == ShowText.getId())
        // need to check ID because from which view has fire event soo we can handle appropriate 
      DisplayText.setVisibility(View.VISIBLE);
  }
}
MilapTank
  • 9,988
  • 7
  • 38
  • 53
0
// Try this way,hope this will help you to solve your problem.

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <Button
        android:id="@+id/bShowText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ShowText"
        android:onClick="ShowMeText"/>
    <TextView
        android:id="@+id/tvShowText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:visibility="gone"
        android:text="TextView With Some Text"/>


</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

    private TextView tvShowText;
    private Button bShowText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvShowText = (TextView)findViewById(R.id.tvShowText);
        bShowText = (Button)findViewById(R.id.bShowText);

    }

    public void ShowMeText(View v){
        if(v.getId() == bShowText.getId()) {
            if(tvShowText.getVisibility() == View.VISIBLE){
                tvShowText.setVisibility(View.GONE);
            }else{
                tvShowText.setVisibility(View.VISIBLE);
            }


        }
    }

}
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
0

Try this:

public class MainActivity extends Activity {
        Button  ShowText; 
        TextView DisplayText;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                ShowText=(Button)findViewById(R.id.bShowText);
                DisplayText=(TextView)findViewById(R.id.tvShowText);
                DisplayText.setVisibility(View.Gone);
                ShowText.setonClickListener(this);


            }
            public void ShowMeText(View view){

                DisplayText.setVisibility(View.VISIBLE);
                DisplayText.setText("Show your Text");

            }
        }
Aman Singh
  • 370
  • 1
  • 9