在花费大量时间重新安装、检查工作并从头开始后,我在遵循 MyFirstApp Android 教程时尚未解决我的问题。我正在使用 Androids eclipse bundle 对其进行编码,因此应该正确安装所有支持包。我的错误发生在以下代码中(错误旁边注释了来自编译器的错误)
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);
}
}
}
如有任何帮助,我们将不胜感激。据我所知,教程中的所有说明均已正确复制。
最佳答案
尝试将其更改为 getActivity()
或 getApplicationContext()
在 sendMessage 方法中尝试使用 getView().findViewByID()
而不仅仅是 findViewByID()。
关于android - MyFirstApp Android 编码教程中的错误“Intent 构造函数未定义。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23223916/