未知的崩溃

时间:2014-04-03 03:40:31

标签: java android

我目前正在尝试开发一个从一个页面切换到另一个页面的简单功能,但每次启动应用程序时都会崩溃。

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Screen One......"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/scan"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Click me to another screen" />

</LinearLayout>

MainActivity.java

package com.example.mdpmk1;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addListenerOnButton();
}

public void addListenerOnButton() {

    final Context context = this;

    button = (Button) findViewById(R.id.scan);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(context, ScanScreen.class);
            startActivity(intent);   

        }

     });

   }

}

ScanScreen.java

package com.example.mdpmk1;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;

public class ScanScreen extends Activity {

Button button;

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

}

scan_screen.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="You have done it!!"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

我对这种东西很新,但我正在为我的专业在学校做一个应用程序,并希望它实际工作。任何帮助都很可爱。感谢

5 个答案:

答案 0 :(得分:0)

在您的MainActivity onCreate中,您需要添加

setContentView(R.layout.activity_main);

答案 1 :(得分:0)

尝试将类MainActivity中的intent语句编辑为:

Intent intent = new Intent(MainActivity.this, ScanScreen.class);
startActivity(intent);

您还必须添加:

setContentView(R.layout.activity_main);

在ActivityMain类onCreate函数中设置视图。

答案 2 :(得分:0)

setContentView(R.your_layout)在您的MainActivity的onCreate()中不存在。尝试为您的活动提供布局。

答案 3 :(得分:0)

您忘了在MainActivity的onCreate()方法中写下以下声明,

setContentView(R.layout.activity_main);

写在这里,

public class MainActivity extends Activity 
{

Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);     // You forgot this line
        addListenerOnButton();
    }

还要更改以下代码行,

public void onClick(View arg0) 
{
     Intent intent = new Intent( MainActivity.this, ScanScreen.class );  // Change here
     startActivity(intent);   
}

答案 4 :(得分:0)

首先,在MainActivity.OnCreate方法中添加SetContentView(R.layout.activity_main)以调用addListenerOnButton。 然后确保在manifest.xml中添加活动条目并将MainActivity作为主应用程序活动,如下所示:

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

    <activity android:name=".ScanScreen"/>