Java Android教程问题
我在开发者Android网站上学习Android应用程序教程已经有一段时间了,但我无法让它在我的一生中完美运行。我的DisplayMessageActivity.java中充满了错误(即获取未定义的方法和未解析的类型),以下是我的文件 MainActivity.javaJava Android教程问题,java,android,eclipse,Java,Android,Eclipse,我在开发者Android网站上学习Android应用程序教程已经有一段时间了,但我无法让它在我的一生中完美运行。我的DisplayMessageActivity.java中充满了错误(即获取未定义的方法和未解析的类型),以下是我的文件 MainActivity.java package com.miller.lab3; import android.app.Activity; import android.content.Intent; import android.os.Bundle; im
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);
}
}
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;
}
}
}
DisplayMessageActivity.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);
}
}
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>
strings.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>
Lab3
你好,世界!
我的留言
输入消息
邮寄
我想知道是否有人看到了不同寻常的事情。如果您能指出这一点,我们将不胜感激。我听说这可能是也可能不是一个过时的教程版本,而我必须使用eclipse来完成这项工作的事实并没有什么帮助。提前谢谢
如果有人不知道我这里说的是教程链接,那么你的代码似乎是正确的。尝试清理项目。转到“项目>清理>清理所选项目”。如果在某些情况下无法执行此操作,请尝试创建新项目。然而,如果这是一个大项目,这种方法是无用的。在这种情况下,有更好的解决方案。使用git,它是一种版本控制系统,可以让您的项目保持超级安全和干净。您没有导入
ActionBarActivity
添加到DisplayMessageActivity.java中的导入:
import android.support.v7.app.ActionBarActivity;
或者单击
ActionBarActivity
并按ctrl+1,然后单击导入“ActionBarActivity”您是否可以发布IDE的屏幕截图或提供一些错误获取未定义的方法和未解析的类型
哪个方法未解析?ActionBarActivity无法解析为类型,方法getIntent()类型DisplayMessageActivity未定义,构造函数TextView(DisplayMessageActivity)未定义,类型DisplayMessageActivity未定义方法setContentView(TextView),类型DisplayMessageActivity未定义方法getSupportFragmentManager(),方法OnOptions ItemSelected(MenuItem)类型的DisplayMessageActivity必须重写或实现超类型方法尝试添加import com.miller.lab3.R
作为导入。我只看到MainActivity两次,你能发布你的DisplayMessageActivity吗?我尝试过多次清理。它除了删除我的r.java文件外什么都不做。是的,有时候r文件会被删除。。。但在这种情况下,请重新创建项目并重试。。。cam请告诉我错误来自何处DisplayMessageActivity.java文件中的所有错误都张贴在您的评论上方。上面说的是:导入android.support.v7无法解决。CTRL+SHIFT+O不应该添加我丢失的导入吗?因为我总是确保先这样做。@YoungMogul如果没有将支持库正确添加到eclipse中,就不会这样做。请使用以下指南添加(或重新添加):。请确保添加资源。不过,我还有三个错误。无法解析代码的容器、操作设置和片段显示消息部分。