Android应用程序停止工作

时间:2013-12-04 01:37:10

标签: android android-intent

它没有说它有任何错误,只是当我尝试启动它时,它会停止并显示以下消息:

  

“不幸的是,LifeSaver已经停止了”。

activity_saver.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Saver" >

    <Button
        android:id="@+id/button1"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/circle" 
        android:onClick="sendMessage" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/button1"
        android:layout_marginTop="68dp"
        android:text="Button"
        android:onClick="schimba" />

</RelativeLayout>

Saver.java:

public class Saver extends Activity {

    Button panica = (Button) findViewById(R.id.button1);
    Button b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_saver);
        b = (Button) findViewById(R.id.button2);
    }


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

    public void sendMessage(View v) {
        panica.setBackgroundColor(Color.BLUE);
    }

    public void schimba (View v){
        Intent i = new Intent (Saver.this, MainActivity.class);
        startActivity(i);
    }
}

activity_main.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

MainActivity.java:

public class MainActivity extends Activity {

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

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

activity_main.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:2)

目前您在为button1活动设置布局之前初始化Saver,因此请在活动的setContentView之后移动视图的初始化:

 Button b,panica;  //<<< Declare here

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_saver);
    b = (Button) findViewById(R.id.button2);
    panica = (Button) findViewById(R.id.button1);   //<< initialize here
}

并确保您已在AndroidManifest.xml

中注册了这两项活动

答案 1 :(得分:1)

Button panica = (Button) findViewById(R.id.button1);
Button b;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_saver);
    b = (Button) findViewById(R.id.button2);
}

您无法在panica方法之外初始化onCreate(您的布局尚未设置)。

所以将代码更改为:

Button panica;
Button b;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_saver);
    b = (Button) findViewById(R.id.button2);
    panica = (Button) findViewById(R.id.button1);
}