【发布时间】:2014-04-28 05:27:09
【问题描述】:
我刚开始在 android 中编码,但我被困在这一点上,我的代码很简单,它仅在这些行中显示错误。 在 setContentView(R.layout.activity_starting_point); 行中显示 activity_starting_point 无法解析或不是字段的错误。为什么那么请帮忙!
package com.thenewboston.abhi;
import android.R;
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.Button;
import android.widget.TextView;
public class StartingPoint extends ActionBarActivity {
int counter;
Button add, sub;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
counter=0;
add = (Button)findViewById(R.id.button1);
sub = (Button)findViewById(R.id.button2);
display = (TextView)findViewById(R.id.text1);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
display.setText("Your total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter--;
display.setText("Your total is " + counter);
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.starting_point, 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() {
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_starting_point, container, false);
return rootView;
}
}
}
【问题讨论】:
-
我们需要多几行代码来帮助解决您的问题。您的
onCreate()和实际错误将是一个好的开始。 -
能否请您发布您的 xml 和 java 的完整代码。以便我们了解更多。
-
@mbs 之前通过电话询问了问题,因此无法粘贴整个代码。现在粘贴的代码请帮忙。
-
@shweta_jain 请帮忙!
-
尝试清理并构建您的项目。
标签: android android-layout android-activity