简体   繁体   English

startActivity()导致Android崩溃

[英]startActivity() causing crash in Android

So I just started learning to develop Android apps, and I have a programming background (Python mainly), so I somewhat know what I'm doing. 所以我刚开始学习开发Android应用程序,我有一个编程背景(主要是Python),所以我有点知道我在做什么。 I'm having a problem with using startActivity(). 我在使用startActivity()时遇到问题。 I've commented code to figure out exactly where the error gets thrown, and it happens right as startActivity() is encountered. 我已经注释了代码以确定错误被抛出的确切位置,并且遇到startActivity()时它会发生。 The error I get is from the emulator and it is just a popup window that says, "Unfortunately, Test has stopped." 我得到的错误来自模拟器,它只是一个弹出窗口,上面写着“不幸的是,测试已经停止了”。 (Test is my program name) after I click my button. 单击我的按钮后,(测试是我的程序名称)。 My code is this 我的代码是这样的

package com.test.test;

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

public class TestActivity extends Activity {

public final static String EXTRA_MESSAGE = "com.test.test.MESSAGE";

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

    public class DisplayMessageActivity extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        }
    }

    public void sendMessage(View view) {
        setContentView(R.layout.main);
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.textBox1);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }

}

Now I know that won't do anything yet, but why is it crashing? 现在我知道它什么都不会做,但为什么会崩溃呢? My XML: 我的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:orientation="horizontal" >

<EditText android:id="@+id/textBox1"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/textBox1Hint" />
<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button1"
    android:onClick="sendMessage" />   

</LinearLayout>

My manifest: 我的清单:

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

<uses-sdk android:minSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".TestActivity"
        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=".DisplayMessageActivity" >
    </activity>

</application>
</manifest>

I saw that a lot of other people were having problems with this because they had not declared their activities in the manifest, but I think I have done so properly. 我看到很多其他人都遇到了这个问题,因为他们没有在舱单中宣布他们的活动,但我认为我已经做得很好。 Any help would be so greatly appreciated. 任何帮助都会非常感激。

One possible problem is that 一个可能的问题是

EditText editText = (EditText) findViewById(R.id.textBox1);

returns null because you dont set anywhere the layout for your activity (with setContentView ) 返回null因为您没有在活动的布局中设置任何位置(使用setContentView

If your editetext is included in view (passed to sendMessage ), you can find it with 如果你的editetext包含在view (传递给sendMessage ),你可以找到它

EditText editText = (EditText) view.findViewById(R.id.textBox1);

I think problem is you are on DisplayMessageActivity and starting the same activity. 我认为问题是你在DisplayMessageActivity并开始相同的活动。 What you need to do is start test activity and call DisplayMessageActivity from intent. 您需要做的是启动测试活动并从intent调用DisplayMessageActivity。

the problem is this, 问题是这个,
you declare in the manifest that the main class is TestActivity: 你在清单中声明主类是TestActivity:

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

but you wanted to start DisplayMessageActivity, so, change this to the following: 但是您想要启动DisplayMessageActivity,因此,请将其更改为以下内容:

public class TestActivity extends Activity {
@Override

I can't understand what you are actually trying to achieve by starting the same activity again. 通过再次启动相同的活动,我无法理解您实际想要实现的目标。 Try this, 尝试这个,

    public class DisplayMessageActivity extends Activity {
    private  String message ;
        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
setLayout(R.layout.<name of the xml file> ); // change the name if it is not in layout folder
           EditText editText = (EditText) findViewById(R.id.textBox1);
           String message = editText.getText().toString();
           Button submitBtn = (Button) findViewById(R.id.button1);
           submitBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                     Toast toast = Toast.makeText(DisplayMessageActivity.this,
                                message,
                                Toast.LENGTH_SHORT);
                        toast.show();
    }});
        }
    }

The above code simply displays the String, This is just a sample code... Hope that helps!!! 上面的代码只显示字符串,这只是一个示例代码...希望有所帮助!

EDIT To run this code you must also add id tag to your Button Tag inside XML file having value button1.. 编辑要运行此代码,您还必须将id标签添加到具有值button1的XML文件中的Button标签中。

Actullay you have created the Activity inside activity. Actullay你已经创建了Activity里面的活动。 Which is not much pref-able but if want to stick with that then you have to change the android:name=".DisplayMessageActivity" to 哪个不是很好,但如果想坚持下去那么你必须将android:name =“。DisplayMessageActivity”更改为

Use separate files for TestActivity and DisplayMessageActivity classes. TestActivityDisplayMessageActivity类使用单独的文件。 In your given code you are not specifying a layout for your DisplayMessageActivity activity using setContentView . 在给定的代码中,您没有使用setContentViewDisplayMessageActivity活动指定布局。

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

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