我已经按照教程开发人员网站上关于如何启动新活动的信。但是有两个错误我无法清楚,它们是直接从教程中获取的代码?
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 */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class); //error 1
EditText editText = (EditText) findViewById(R.id.edit_message); //error 2
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
}
错误1建议删除匹配intent()的参数 错误2说 - 无法从类型Activity
中对非静态方法findViewById(int)进行静态引用 <?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:id="@id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send" />
</LinearLayout>
答案 0 :(得分:0)
将其移出Fragment
。以下内容必须在“活动”中。
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class); //error 1
EditText editText = (EditText) findViewById(R.id.edit_message); //error 2
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
我猜你有
android:onClick="sendMessage"
用于xml中的按钮
我还猜测视图属于片段布局。如果按钮和EditText属于fragment_main.xml
那么
EditText editText;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
editText = (EditText)rootView.findViewById(R.id.edit_message)
Button b = (Button) rootView.findViewById(R.id.button1); //button id
b.setOnClickListener( new OnClickListener()
{
@Override
public void onClick(View V)
{
Intent intent = new Intent(getActivity(), DisplayMessageActivity.class); /
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
});
return rootView;
}
你可以摆脱android:onClick="sendMessage"