【问题标题】:Unable to move to new activity on click of a button单击按钮无法移动到新活动
【发布时间】:2012-05-07 22:16:21
【问题描述】:

我遇到了一个关于如何在一个活动中显示从用户获取的数据以显示在另一个活动中的教程,我试图提出自己的版本(根据我非常有限的知识)。

这是第一个通过

接受数据的活动的代码
package kk.screen;

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

public class ScreenActivity extends Activity {
/** Called when the activity is first created. */

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

public void btnclick(View view) {

    inputName = (EditText)findViewById(R.id.name);
    Intent nextscreen = new Intent(this, newscreen.class);

    //Sending value to the next activity

    nextscreen.putExtra("name",inputName.getText().toString());
    startActivity(nextscreen);
 }
}

这是下一个 Activity 的代码,它应该在点击 id 为“btnclick”的按钮时被激活:

package kk.screen;

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

public class newscreen extends Activity {

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

TextView txtName = (TextView) findViewById(R.id.txtName);

Intent i=getIntent();

//Receiving the data
String name = i.getStringExtra("name");

//Display received data
txtName.setText(name);

 }
}

问题是,单击按钮后,应用程序崩溃了,我又回到了主屏幕。

可能是什么问题?我应该使用 OnClickListener() 吗? (这似乎是我的方法和教程的方法之间的主要区别)

【问题讨论】:

  • 请发布 logcat 错误。发布 main.xml 也可能有帮助。

标签: android android-activity


【解决方案1】:

我打赌您会收到 NoClassDefFoundError 错误,因为您的 AndroidManifest.xml 文件中没有第二个 Activity,请参阅以下问题:

Add a new activity to the AndroidManifest?

此外,来自 Google 的文档:“所有活动都必须由清单文件中的元素表示”

http://developer.android.com/guide/topics/manifest/activity-element.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    相关资源
    最近更新 更多