在模拟器中运行Android应用程序时没有布局

时间:2021-10-28 18:54:14

So, I have this code:

所以,我有这个代码:

package com.example.ponto_2d;

import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.TextView;

public class MainActivity extends Activity {
pontos p1=new pontos();
pontos p2=new pontos();

            @Override
            public boolean onTouchEvent(MotionEvent event) {

                TextView tv1 = (TextView) findViewById(R.id.textView2);

                   int eventaction= event.getAction();

                   switch (eventaction){
                   case MotionEvent.ACTION_DOWN:
                       p1.x=event.getX();
                       break;   

                   }

                   tv1.setText(""+p1.x);

                   return true;
            }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

This class:

这个班:

package com.example.ponto_2d;

public class pontos {
        public double x,y;


        public double distancia(pontos p2){

            return Math.sqrt((p2.x-x)*(p2.x-x) + (p2.y-y)*(p2.y-y));
        }
}

This xml layout:

这个xml布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="38dp"
        android:text="@string/titulo_app"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="30dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="@string/valor"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignRight="@+id/textView1"
        android:text="@string/distancia_1"
        android:textAppearance="?android:attr/textAppearanceMedium" />


</RelativeLayout>

I think that it is all right, but when I run the emulator, I get nothing, not even a single button. Any idea about what what could be the problem?

我认为没关系,但是当我运行模拟器时,我什么都没得到,甚至没有一个按钮。什么可能是什么问题的任何想法?

2 个解决方案

#1


1  

You don't have an onCreate() method, so nothing really starts up. onCreate() is the android equivalent of main() in Java. Try using:

你没有onCreate()方法,所以没有真正启动。 onCreate()是Java中main()的android等价物。尝试使用:

public class MainActivity extends Activity {
pontos p1=new pontos();
pontos p2=new pontos();


    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.<yourXMLFile>);
    }
            @Override
            public boolean onTouchEvent(MotionEvent event) {

                TextView tv1 = (TextView) findViewById(R.id.textView2);

                   int eventaction= event.getAction();

                   switch (eventaction){
                   case MotionEvent.ACTION_DOWN:
                       p1.x=event.getX();
                       break;   

                   }

                   tv1.setText(""+p1.x);

                   return true;
            }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

Replace with the name of your xml layout file, minus the extension.

替换为xml布局文件的名称,减去扩展名。

#2


0  

You don't have an onCreate() method in your Activity. In that method you would need to call setContentView() and pass in the resource id of your xml layout. This link should get you started.

您的Activity中没有onCreate()方法。在该方法中,您需要调用setContentView()并传入xml布局的资源ID。这个链接应该让你开始。

#1


1  

You don't have an onCreate() method, so nothing really starts up. onCreate() is the android equivalent of main() in Java. Try using:

你没有onCreate()方法,所以没有真正启动。 onCreate()是Java中main()的android等价物。尝试使用:

public class MainActivity extends Activity {
pontos p1=new pontos();
pontos p2=new pontos();


    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.<yourXMLFile>);
    }
            @Override
            public boolean onTouchEvent(MotionEvent event) {

                TextView tv1 = (TextView) findViewById(R.id.textView2);

                   int eventaction= event.getAction();

                   switch (eventaction){
                   case MotionEvent.ACTION_DOWN:
                       p1.x=event.getX();
                       break;   

                   }

                   tv1.setText(""+p1.x);

                   return true;
            }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

Replace with the name of your xml layout file, minus the extension.

替换为xml布局文件的名称,减去扩展名。

#2


0  

You don't have an onCreate() method in your Activity. In that method you would need to call setContentView() and pass in the resource id of your xml layout. This link should get you started.

您的Activity中没有onCreate()方法。在该方法中,您需要调用setContentView()并传入xml布局的资源ID。这个链接应该让你开始。