我有一个非常简单的项目,其中我想要实现的是当用户点击按钮时它显示文本。但在亚洲开发银行,它说不幸的是showtext停止工作。这背后的原因是什么?
我曾尝试但没有得到任何结果。我的代码很好但不起作用。下面是它的java代码。我省略了xml代码,因为它只包含按钮和不太复杂的文本视图。
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);
}
}
尝试解决我的这个小问题。在此先感谢。
正如有人在这里要求logcat错误的那样 -
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
答案 0 :(得分:3)
您需要在布局中的android:onClick="ShowMeText"
中添加Button
。像这样
<Button .............
android:onClick="ShowMeText"
......... />
并使用小写方法创建方法作为Java命名约定。像这样showmetext
并将ShowMeText(...)
方法定义为Public
答案 1 :(得分:2)
ShowMeText(View v)必须公开。
顺便说一下。使用Java代码样式指定的小写字符启动函数名称。
答案 2 :(得分:1)
<Button
android:id="@+id/tvShowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="ShowMeText" />
在java代码中
public void ShowMeText(View v){
DisplayText=(TextView)findViewById(R.id.tvShowText);
DisplayText.setVisibility(View.VISIBLE);
}
答案 3 :(得分: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);
}
}
答案 4 :(得分: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);
}
}
}
}
答案 5 :(得分:0)
试试这个:
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");
}
}