简体   繁体   English

创建新的/下一个Android活动时遇到的问题

[英]Problems with creating a new/next Android activity

I just wanted to click on a button in the MainActivity. 我只想单击MainActivity中的一个按钮。 After I clicked on this button a new activity appears. 单击此按钮后,将出现一个新活动。 In this activity is also a button and when I click on this button a text appears. 在此活动中,还有一个按钮,当我单击此按钮时,将显示一个文本。 My Problem now is, that the text doesn't appear, when I click on the button. 我的问题现在是,当我单击按钮时,文本没有出现。 My code for clicking on a button so that a text appears is actually working. 我用于单击按钮以使文本出现的代码实际上正在工作。 But not if I use it in a new Activity/Layout/ page. 但是如果我在新的“活动/布局/页面”中使用它,则不会。 So there must be s.th. 所以一定有…… wrong with the Connection between the first and the second activity?! 第一个和第二个活动之间的连接有问题吗?

MainActivity.java: MainActivity.java:

package com.example.xxx;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;

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.main, menu);
        return true;}
        {}
    public void page2 (View view){
        setContentView(R.layout.pagetwo);


}}

activity_main.xml: 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" >

    <Button
        android:id="@+id/BtnKlick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginTop="178dp"
        android:text="Button" 
        android:onClick="page2"/>

</RelativeLayout>

Pagetwo.java: Pagetwo.java:

package com.example.xxx;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Pagetwo extends Activity implements OnClickListener {

    public Button btn1;
    public TextView tw1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pagetwo);

    btn1 = (Button) findViewById(R.id.BtnKlick);
    tw1 = (TextView) findViewById(R.id.Text);

    btn1.setOnClickListener(this);

}
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        tw1.setText("Hallo");

    }}

pagetwo.xml: pagetwo.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/BtnKlick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Klick" />

    <TextView
        android:id="@+id/Text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="87dp" />

</RelativeLayout>

AndroidManifest.xml: AndroidManifest.xml中:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.xxx"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.xxx.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Update 更新

All you seem to be doing is swapping your layouts within PageOne activity. 您似乎要做的就是在PageOne活动中交换布局。

Change 更改

public void page2 (View view){
    setContentView(R.layout.pagetwo);
}

to

public void page2 (View view){
Intent i = new Intent(this, Pagetwo.class);             
    startActivity(i);
}

This will actually launch your pagetwo activity rather than just swapping layouts within pageone activity 这实际上将启动您的pagetwo活动,而不仅仅是在pageone活动中交换布局

Make that change and all should be good. 进行更改,一切都应该很好。 You will most likely have to add some imports, at least for the intent class 您很有可能必须添加一些导入,至少对于intent类而言

Also Activities must be registered in the manifest. 此外,必须在清单中注册活动。 To add the activity to your manifest check the code below 要将活动添加到清单中,请检查以下代码

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.xxx.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <activity
        android:name="Pagetwo"
        android:label="@string/app_name" >
    </activity>

</application>

Change the label to something more appropriate than app name once you have it working End update 标签更改为比应用程序的名称更合适的东西,一旦你有工作结束时更新

Try renaming your xml view id's to meet coding standards by using all lower case letters and separating words with underscores. 通过使用所有小写字母并用下划线分隔单词,尝试重命名xml视图id以符合编码标准。 The Android platform may well be getting confused as Text is a reserved word. 由于Text是保留字,因此Android平台可能会感到困惑。

so 所以

<TextView
    android:id="@+id/Text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="71dp"
    android:textSize="70sp" />

becomes

<TextView
    android:id="@+id/tv_hallo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="71dp"
    android:textSize="70sp" />

and your code becomes 你的代码变成

...
    public TextView tw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.eclipse);

        btn = (Button) findViewById(R.id.BtnKlick);
        tw = (TextView) findViewById(R.id.tv_hallo);

        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        tw.setText("Hallo");

    }
...

Not sure if this is going to have much of an effect but it is just possible android is getting the word Text confused 不知道这是否会产生很大的影响,但是android可能会将Text一词弄糊涂了

Also check your logcat output to see if there are any errors. 还要检查您的logcat输出,看是否有任何错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM