【发布时间】:2015-02-18 18:10:07
【问题描述】:
我已经在开发者 android 网站上学习 Android 应用程序教程有一段时间了,但我无法让它在我的生活中完美运行。我的 DisplayMessageActivity.java 充满了错误(即获取未定义的方法和未解析的类型)这是我的文件...
MainActivity.java
package com.miller.lab3;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.mycompany.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
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);
}
}
DisplayMessageActivity.java
package com.miller.lab3;
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;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
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);
if (savedInstanceState == null) {
getSupportFragmentManager().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;
}
}
}
activity_main.xml
<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"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
<EditText
android:id="@+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
</RelativeLayout>
字符串.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Lab3</string>
<string name="hello_world">Hello world!</string>
<string name="title_activity_display_message">My Message</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
</resources>
我想知道是否有人看到任何不寻常的东西。如果您能指出来,将不胜感激。我听说这可能是也可能不是本教程的过时版本,而且我必须使用 Eclipse 来完成它的事实并没有完全帮助。提前致谢。
如果有人不知道我在这里说的是教程链接http://developer.android.com/training/basics/firstapp/starting-activity.html
【问题讨论】:
-
您能否发布您的 IDE 的屏幕截图或提供一些错误
-
getting undefined methods and unresolved types哪个方法没解决? -
ActionBarActivity 无法解析为类型,DisplayMessageActivity 类型的 getIntent() 方法未定义,DisplayMessageActivity 类型的构造函数 TextView(DisplayMessageActivity) 未定义,DisplayMessageActivity 类型的 setContentView(TextView) 方法未定义, DisplayMessageActivity 类型的方法 getSupportFragmentManager() 未定义,DisplayMessageActivity 类型的方法 onOptionsItemSelected(MenuItem) 必须重写或实现超类型方法
-
尝试添加
import com.miller.lab3.R作为导入 -
我只看到 MainActivity 两次,你能发布你的 DisplayMessageActivity 吗?