打开一个app就闪退解决方法 最近在学android,这边有个小问题,打开这个应用就闪退是什么原因? package com.example.linearlayouttest; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.Button; import android.widget.LinearLayout; public class LinearLayoutTest extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_linear_layout_test); LinearLayout root =(LinearLayout)findViewById(R.id.root); Button bn1=(Button)findViewById(R.id.bn1); bn1.setText(R.string.bn1); root.addView(bn1); Button bn2=(Button)findViewById(R.id.bn2); bn2.setText(R.string.bn2); root.addView(bn2); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.linear_layout_test, menu); return true; } }
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"
android:gravity="bottom|center_horizontal" android:id="@+id/root" > [解决办法] LinearLayout root =(LinearLayout)findViewById(R.id.root); root.addView(bn1); root.addView(bn2); 本文来源:https://www.wddqw.com/doc/56e36da5cf22bcd126fff705cc17552707225e65.html