这是我第一次制作Android应用程序,我希望它可以重用于我稍后可能制作的任何其他应用程序。这就是为什么我想创建一个按钮类,动态地生成一个按钮而不使用XML。但是当我想要这样做时,当我在makeButton方法中使用“setContentView”方法时程序崩溃。
此外,当我将“makeButton”的内容粘贴到onCreate方法时,它会以某种方式工作。为什么它在onCreate方法中工作而不在“makeButton”方法中?
MainActivity.java
package mw.mobilepccommunication;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ButtonService button = new ButtonService("test", 100, 100, 100, 100, this);
button.makeButton();
//Contents of makeButton.
Button mButton = new Button(this);
mButton.setText("test");
setContentView(mButton);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
ButtonSerivce.java
package mw.mobilepccommunication;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class ButtonService extends AppCompatActivity {
private String mNameButton;
private int mHeight;
private int mWidth;
private int mXBeginPoint;
private int mYBeginPoint;
private Button mButton;
private Context mContext;
public ButtonService(String aNameButton, int aHeight, int aWdith, int aXBeginPoint, int aYBeginPoint, Context aContext)
{
mNameButton = aNameButton;
mHeight = aHeight;
mWidth = aWdith;
mXBeginPoint = aXBeginPoint;
mYBeginPoint = aYBeginPoint;
mContext = aContext;
}
public void makeButton()
{
mButton = new Button(mContext);
mButton.setText(mNameButton);
setContentView(mButton); // <-- It fails here.
}
}
答案 0 :(得分:0)
您对过程和活动生命周期的理解是错误的。
当您的活动由Android系统创建时,它会遇到几个必需的steps。
活动生命周期中的第一次附加到Window
。
考虑setContentView
方法
public void More ...setContentView(View view, ViewGroup.LayoutParams params) {
getWindow().setContentView(view, params); // retrieve attached window
initWindowDecorActionBar();
}
由于之前Acrivity没有附加到窗口,getWindow
方法返回null。
这就是你的应用程序崩溃的原因
如果您想向MainActivity
添加按钮,请尝试执行类似
public class ButtonService{
private String mNameButton;
private int mHeight;
private int mWidth;
private int mXBeginPoint;
private int mYBeginPoint;
private Button mButton;
private Activity mContext;
public ButtonService(String aNameButton, int aHeight, int aWdith, int aXBeginPoint, int aYBeginPoint, Activity aContext)
{
mNameButton = aNameButton;
mHeight = aHeight;
mWidth = aWdith;
mXBeginPoint = aXBeginPoint;
mYBeginPoint = aYBeginPoint;
mContext = aContext;
}
public void makeButton()
{
mButton = new Button(mContext);
mButton.setText(mNameButton);
mContext.setContentView(mButton);
}
}