我一直在下面的代码中遇到错误,特别是星号包围的两个部分(R.id.action_settings
和fragment_display_message
),
package com.example.completelypointlessapp;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
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 {
@Override
public 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);
}
@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;
}
}
}
错误状态:" fragment_display_message cannot be resolved or is not a field" and "action_settings cannot be resolved or is not a field
"
有人可以帮忙解决这个问题吗? (对于某些不正确的格式的道歉,我是这个网站的新手)
答案 0 :(得分:0)
View rootView = inflater.inflate(R.layout.fragment_display_message,
container, false);
fragment_display_message.xml
布局文件吗?if (id == R.id.action_settings)
示例 -
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:title="Settings"
android:showAsAction="never"
>
</item>
</menu>
如果两者的答案都是NO,则错误很明显。