繁体   English   中英

Android - 如何开始新活动

[英]Android - How to start a new activity

我试图用随机颜色制作牛眼,而不是圆圈,我将使用正方形。

但问题是,当我在模拟器上运行应用程序时,当他启动新活动时,它会停止响应。

这是主要活动,即启动DrawActivity的活动。

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent coiso = new Intent(this, Draw.class);
        startActivity(coiso);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

这是Draw活动,我想要开始的活动。 (它没有我想做的事情。因为我不能,问题就在前面)

public class Draw extends View {

    public Draw(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);   
    }
}

有人能帮我吗? 对不起英文。

你有这个

 public class Draw extends View 

您的课程不会扩展活动

相反,你可以这样做

Draw draw = new Draw(this); 
setContentView(draw);

或者具有线性或相对的布局,并将其放置在您喜欢的位置,在初始化后将Draw视图添加到布局中。

setContentView(R.layout.activity_main);
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout);
// linear layout or relative layout in activity_main.xml.
// place the layout ahere you want along with other views  
Draw draw = new Draw(this); 
ll.addView(draw);  
// add your customview to linearlayout   

编辑:

删除它

 Intent coiso = new Intent(this, Draw.class);
 startActivity(coiso);

在您的activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
     // customize linear layout to your needs. 
    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:id="@+id/linearLayout"
        android:orientation="vertical" >
    </LinearLayout>
      // other widgets
</RelativeLayout>

在你的onCreate

setContentView(R.layout.activity_main);
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout);
Draw draw = new Draw(this); 
ll.addView(draw);  

Draw类需要extend Activity而不是View 由于你想开始一个新的ActivityDraw类,这意味着这应该扩展Activity 此外,您需要在Draw类中覆盖onCreate()

如果您的Draw类是View,那么我建议您使用addView()将视图添加到正在使用的Layout

您需要确保更改Draw extends Activity您不能在没有布局且没有OnCreate的情况下进行新活动。据我所知。 尝试创建一个常规活动,扩展Activity并在那里实现Draw。

public class DrawActivity extends Activity {

@SuppressLint("ShowToast")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_draw);
    Toast.makeText(DrawActivity.this, "YO", Toast.LENGTH_LONG);
}

从那里实现你的绘制功能。 或创建一个实现绘图需求的JAVA类,并在主屏幕中使用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM