我将从没有编程经验到学习 Android。我正在使用带有 ADT 插件的 Eclipse 做 Google 的在线教程。
初学者部分的最后一个教程是让用户输入一个语句并启动一个显示该输入文本的新活动。当我在我的 Nexus 10 上运行该应用程序时,每次我点击提交按钮时它都会强制关闭。
我猜这是在我的主要活动中发送文本或在我的显示消息活动中检索文本的问题。
我为我的菜鸟性质提前道歉。我会很感激任何帮助。
MainActivity.java:
package com.charteacher.gnuoral;
import android.content.Intent;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.gnuoral.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
/**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);
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
*这是 Displaymssageactivity
package com.charteacher.gnuoral;
import android.widget.TextView;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class DisplayMessageActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
//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);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_display_message,
container, false);
return rootView;
}
}
}
我做了 logcat/ 不幸的是,目前这对我来说都是希腊语。这是我的结果。我只想得到一个功能程序。稍后我会担心形式上的错误。
05-09 15:44:42.611: D/dalvikvm(14082): Late-enabling CheckJNI
05-09 15:44:43.006: D/mali_winsys(14082): new_window_surface returns 0x3000
05-09 15:44:43.076: D/OpenGLRenderer(14082): Enabling debug mode 0
05-09 15:44:43.201: I/ActivityManager(14082): Timeline: Activity_idle id: android.os.BinderProxy@42220640 time:3937368
05-09 15:44:43.256: E/SpannableStringBuilder(14082): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-09 15:44:43.256: E/SpannableStringBuilder(14082): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-09 15:44:43.266: E/SpannableStringBuilder(14082): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-09 15:44:43.266: E/SpannableStringBuilder(14082): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-09 15:44:44.071: I/ActivityManager(14082): Timeline: Activity_idle id: android.os.BinderProxy@42220640 time:3938235
05-09 15:44:53.736: D/AndroidRuntime(14082): Shutting down VM
05-09 15:44:53.736: W/dalvikvm(14082): threadid=1: thread exiting with uncaught exception (group=0x41f8dc80)
05-09 15:44:53.736: E/AndroidRuntime(14082): FATAL EXCEPTION: main
05-09 15:44:53.736: E/AndroidRuntime(14082): Process: com.charteacher.gnuoral, PID: 14082
05-09 15:44:53.736: E/AndroidRuntime(14082): java.lang.IllegalStateException: Could not find a method sendmessage(View) in the activity class com.charteacher.gnuoral.MainActivity for onClick handler on view class android.widget.Button
05-09 15:44:53.736: E/AndroidRuntime(14082): at android.view.View$1.onClick(View.java:3817)
05-09 15:44:53.736: E/AndroidRuntime(14082): at android.view.View.performClick(View.java:4445)
05-09 15:44:53.736: E/AndroidRuntime(14082): at android.view.View$PerformClick.run(View.java:18429)
05-09 15:44:53.736: E/AndroidRuntime(14082): at android.os.Handler.handleCallback(Handler.java:733)
05-09 15:44:53.736: E/AndroidRuntime(14082): at android.os.Handler.dispatchMessage(Handler.java:95)
05-09 15:44:53.736: E/AndroidRuntime(14082): at android.os.Looper.loop(Looper.java:136)
05-09 15:44:53.736: E/AndroidRuntime(14082): at android.app.ActivityThread.main(ActivityThread.java:5081)
05-09 15:44:53.736: E/AndroidRuntime(14082): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 15:44:53.736: E/AndroidRuntime(14082): at java.lang.reflect.Method.invoke(Method.java:515)
05-09 15:44:53.736: E/AndroidRuntime(14082): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
05-09 15:44:53.736: E/AndroidRuntime(14082): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-09 15:44:53.736: E/AndroidRuntime(14082): at dalvik.system.NativeStart.main(Native Method)
05-09 15:44:53.736: E/AndroidRuntime(14082): Caused by: java.lang.NoSuchMethodException: sendmessage [class android.view.View]
05-09 15:44:53.736: E/AndroidRuntime(14082): at java.lang.Class.getConstructorOrMethod(Class.java:472)
05-09 15:44:53.736: E/AndroidRuntime(14082): at java.lang.Class.getMethod(Class.java:857)
05-09 15:44:53.736: E/AndroidRuntime(14082): at android.view.View$1.onClick(View.java:3810)
05-09 15:44:53.736: E/AndroidRuntime(14082): ... 11 more
05-09 15:44:55.871: I/Process(14082): Sending signal. PID: 14082 SIG: 9