请检查我的程序并找出错误

时间:2011-05-16 11:12:34

标签: android android-activity

我想做一个简单的动作,当我按下按钮然后我应该跳到另一个活动。但是在这个程序中,每当我按下提交按钮时,都会发生错误并终止程序。

请检查我的代码并帮助我完成了哪些错误。

我的第一项活动

package login.test;

import android.app.Activity;
import android.os.Bundle;
import android.content.*;
import android.widget.*;
import android.view.View;

import android.view.View.OnClickListener;

public class login extends Activity  
{
    /** Called when the activity is first created. */
    EditText e;
    Button b;
    @Override

    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         b=(Button)findViewById(R.id.submit);
       e=(EditText)findViewById(R.id.editText1);

    b.setOnClickListener( new OnClickListener()
    {

       @Override
       public void onClick(View v)
        {
           if (v.getId() == R.id.submit) 
            {
               Intent myIntent = new Intent(v.getContext(), some.class);
               startActivityForResult(myIntent, 0);

            } 
            else
            {
                e.setText("cant go");
            }
       }
    });
    }
}

第二项活动

package login.test;
import android.os.Bundle;
import android.app.Activity;
import android.content.*;
import android.widget.*;
import android.view.View;
import android.view.View.OnClickListener;

public class some extends Activity {
     public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.welcome);
        }
}

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:orientation="vertical">
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="saan"
    />

<Button android:text="Submit" android:id="@+id/submit" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:text="EditText" android:id="@+id/editText1"></EditText>
</LinearLayout>

的welcome.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <EditText android:text="You are welcome" android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText>
</LinearLayout>

2 个答案:

答案 0 :(得分:4)

我想你错过了在清单中加入你的第二项活动..

所以plz在menifest中添加了第二个活动。

第二件事是无需检查

if (v.getId() == R.id.submit) 

因为您在按钮上设置了setOnClickListener,所以v将始终按下。

删除此条件

将我的代码与您的代码进行比较。

b.setOnClickListener( new OnClickListener()
    {

       @Override
       public void onClick(View v)
        {
           Intent myIntent = new Intent(login.this, some.class);
           startActivityForResult(myIntent, 0);


       }
    });

答案 1 :(得分:0)

试试这个

b.setOnClickListener(new OnClickListener() {

@覆盖    public void onClick(查看v)     {

       Intent myIntent = new Intent(v.getContext(), some.class);
       startActivityForResult(myIntent, 0);

} });

以及AndroidManifest.xml中的第二个活动类