打开活动错误

时间:2014-11-10 11:26:42

标签: java android layout android-activity

嗨ppl(即时通讯与android) 我创建了一个应用程序,但应用程序无法打开一个新活动来打开更多布局,可以帮助我吗?

我的代码是:

布局 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:background="#f9f9f9"
>
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    >
        <LinearLayout

            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
        >
            <Button
                style="@style/Button"
                android:text="@string/wireless"
                android:onClick="OpenWireless"
            />
            <Button
                style="@style/Button"
                android:text="Button 1"
            />
            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView"
            />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

wireless.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:background="#f9f9f9"
>
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    >
        <LinearLayout

            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
        >
            <Button
                style="@style/Button"
                android:text="On/Off"
            />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

活动: MainActivity:

package pacl.hackdroid;

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

public class MainActivity extends Activity
{
    /** Called when the activity is first created.
     * @param savedInstanceState */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    }
    public void OpenWireless(View view)
    {
        TextView textView;
        textView = (TextView) findViewById(R.id.textView);
        textView.setText("123");

        Intent Wireless;
        Wireless = new Intent(this, WirelessActivity.class);
    }

}

WirelessActivity:

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pacl.hackdroid;

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

/**
 *
 * @author simao.lemos
 */
public class WirelessActivity extends Activity {

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        setContentView(R.layout.wireless);
        // ToDo add your GUI initialization code here
    }

}

我的错误是什么?

4 个答案:

答案 0 :(得分:1)

您忘了拨打startActivity

    Intent Wireless;
    Wireless = new Intent(this, WirelessActivity.class);
    startActivity(Wireless);

答案 1 :(得分:1)

在此处添加startActivity

Intent wirelessIntent = new Intent(this, WirelessActivity.class);
startActivity(wirelessIntent);

还提到应用程序标签之间的清单文件中的活动,例如

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

否则app会在点击按钮后显示强行关闭。

答案 2 :(得分:0)

Intent提供了一种工具,用于在不同应用程序中的代码之间执行延迟运行时绑定。它最重要的用途是发起活动,它可以被认为是活动之间的粘合剂。它基本上是一个被动数据结构,包含要执行的动作的抽象描述。

public class MainActivity extends Activity
{
    /** Called when the activity is first created.
     * @param savedInstanceState */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      OpenWireless();
    }
    public void OpenWireless()
    {
        TextView textView;
        textView = (TextView) findViewById(R.id.textView);
        textView.setText("123");

        Intent w;
        w = new Intent(this, WirelessActivity.class);
        startActivity(w);
    }

}        

答案 3 :(得分:0)

更改此方法

public void OpenWireless(View view)
        {
            TextView textView;
            textView = (TextView) findViewById(R.id.textView);
            textView.setText("123");

            Intent Wireless;
            Wireless = new Intent(this, WirelessActivity.class);
            startActivity(Wireless);

        }

这将打开下一个活动。