花了好几个小时重新安装,检查工作并从头开始我还没有解决我的问题,同时遵循MyFirstApp Android教程。我正在使用Androids eclipse包来编码它,所以支持包应该都正确安装。我的错误发生在以下代码中(错误旁边的编译器错误)
package com.example.myfirstapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
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 ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@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;
}
/** Called when the user clicks the Send button */
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class); // **Error"The constructor Intent(MainActivity.PlaceholderFragment, Class<DisplayMessageActivity>) is undefined"**
EditText editText = (EditText) findViewById(R.id.edit_message);// **Error "edit_message cannot be resolved or is not a field"**
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
}
非常感谢任何帮助。据我所知,本教程中的所有说明都已正确复制。
答案 0 :(得分:2)
尝试将其更改为getActivity()
或getApplicationContext()
在sendMessage方法中,请尝试使用getView().findViewByID()
而不是findViewByID()。
答案 1 :(得分:1)
至于第一个错误,您只需将this
替换为getActivity()
,因为this
引用您的PlaceholderFragment
实例,该实例不是有效{{1}但是,包含Context
的是。
Activity
更改为
Intent intent = new Intent(this, DisplayMessageActivity.class);
您的第二个错误可能意味着您的布局中未定义ID Intent intent = new Intent(getActivity(), DisplayMessageActivity.class);
,或者这意味着您只是忘记导入edit_message
课程。
话虽如此:如果你不得不在这里询问这些错误,我强烈建议你在深入研究Android开发之前先获得一本Java书。
答案 2 :(得分:0)
您需要先创建另一个名为DisplayMessageActivity
的类。你可以找到它here
对于第二个错误,请检查您的fragment_main.xml
是否有必要的观点。它应该是这样的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal">
<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send" />
</LinearLayout>
developer.android.com的培训教程是渐进式的。请继续下一课。
答案 3 :(得分:0)
您应该this
Intent intent = new Intent(this, DisplayMessageActivity.class);
使用getActivity()
Intent intent = new Intent(getActivity(), DisplayMessageActivity.class);
方法,{{1}}