gpt4 book ai didi

android - onClick 方法问题

转载 作者:搜寻专家 更新时间:2023-11-01 09:00:01 30 4
gpt4 key购买 nike

好的,我正在浏览 devoloper.android.com 网站上的“MyFirstApp”教程。我正在上最后一个教程,我已经完成了第一章的所有内容,你可以说是这样。唯一的问题是,在“开始另一项 Activity ”类(class)结束时,它会告诉您运行该应用程序。好吧,当我运行它时,它会在模拟器上显示一个可单击的按钮,但是当您单击它时,它会出现运行时错误并强制关闭。我曾尝试对此进行一些研究,但对正在发生的事情一无所知。错误是:java.lang.IlleglStateException:在 android.widet.B 上的 View 类上的 onClick 处理程序的 Activity 类 com.example.MyFirstApp.MainActivity 中找不到方法 sendmessage(view)我将为此展示代码:

主要 Activity :

package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
// Do something in response to button
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

Activity 显示消息:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".DisplayMessageActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

</RelativeLayout>

DisplayMessageActivity.java:

package com.example.myfirstapp;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.widget.TextView;

public class DisplayMessageActivity extends Activity {

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);

// Set the text view as the activity layout
setContentView(textView);

// Make sure we're running on Fry or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
// Show the Up button in the action bar.
getupActionBar().setDisplayHomeAsUpEnabled(true);
}
}

private ActionBar getupActionBar() {
// TODO Auto-generated method stub
return null;
}

/**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}

}

最佳答案

看起来您在 xml 中为 Button onClick() 拼错了函数名称。你的 java 代码中有一个大写的“m”

 public void sendMessage(View view) {

你的 xml 中可能还有一个小写的“m”

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

为了符合java标准,在你的xml中把它改成

 android:onClick="sendMessage"/>

关于android - onClick 方法问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15845181/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com