java - Android - 如何开始新 Activity

标签 java android android-activity drawing

我正在尝试用随机颜色制作一个牛眼,我将使用正方形而不是圆形。

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

这是主要 Activity ,启动 DrawActivity 的 Activity 。

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;
    }
}

这就是抽奖 Activity ,我想要开始的 Activity 。 (它没有我想做的事情。因为我做不到,所以问题就在前面)

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 

你的类没有扩展 Activity

相反,您可以执行以下操作

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

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

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);  

关于java - Android - 如何开始新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18988396/

相关文章:

java - 如何在 JSF 中使用 Facelets 创建左侧导航 Pane ?

java - 使用 HashMap 和 List 进行嵌套双括号初始化

Android widget 需要返回两次

java - 将不可序列化的数据发送到新的 Activity

android - 自动短信应用程序崩溃

java - 谁创建事件对象以及它们在哪里创建?

JavaMail自动发送问题

android - 将多边形添加到 map 时出现 IndexOutOfBoundsException

javascript - 短信:带有字母参数的链接在 Samsung Galaxy S7 上被解释为数字

java - 将Java代码运行到Hadoop