以下是代码:
<RelativeLayout android:id="@+id/TextDisplayLayout"
android:layout_width="match_parent"
android:layout_height="@dimen/PrintSetupTextHeight"
android:layout_marginTop="45dp"
android:layout_marginBottom="15dp">
</RelativeLayout>
和
public class MainScreen extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
RelativeLayout infoDisplay = (RelativeLayout) findViewById(R.id.TextDisplayLayout);
TextView sdCardInfo = new TextView(this);
sdCardInfo.setId(1);
sdCardInfo.setText("SD Card Info Here");
sdCardInfo.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 100));
sdCardInfo.setBackgroundColor(Color.GRAY);
sdCardInfo.setGravity(Gravity.CENTER);
infoDisplay.addView(sdCardInfo);
}
我已将问题缩小到了 infoDisplay.addView(sdCardInfo);
当这个部分被注释掉时,该应用程序可以工作,但显然我需要这部分代码。有人知道它为什么不起作用吗?
答案 0 :(得分:1)
试试这个..
改变这个..
setContentView(R.layout.activity_main_screen);
到
setContentView(R.layout.fragment_main_screen);
并删除
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
也
改变这个..
public class MainScreen extends ActionBarActivity
到
public class MainScreen extends Activity
OR
在PlaceholderFragment
内 onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_screen, container,
false);
RelativeLayout infoDisplay = (RelativeLayout) rootView.findViewById(R.id.TextDisplayLayout);
TextView sdCardInfo = new TextView(getActivity());
sdCardInfo.setId(1);
sdCardInfo.setText("SD Card Info Here");
sdCardInfo.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 100));
sdCardInfo.setBackgroundColor(Color.GRAY);
sdCardInfo.setGravity(Gravity.CENTER);
infoDisplay.addView(sdCardInfo);
return rootView;
}
答案 1 :(得分:0)
试试这段代码......
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout infoDisplay = (RelativeLayout) findViewById(R.id.TextDisplayLayout);
TextView sdCardInfo = new TextView(this);
sdCardInfo.setId(1);
sdCardInfo.setText("SD Card Info Here");
sdCardInfo.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 100));
sdCardInfo.setBackgroundColor(Color.GRAY);
sdCardInfo.setGravity(Gravity.CENTER);
infoDisplay.addView(sdCardInfo);
}
}