android新手,我简单的姓名获取应用程序崩溃了

问题描述

|| 大家好,我在学习android时正在编写一个名称获取应用程序,当我将其加载到模拟器中时,我被迫关闭该应用程序。我很新,所以可能很简单。 namegetter.java
package edu.calpoly.android.lab1;
// tools
import android.app.Activity;
import android.content.Intent;
import android.content.pm.LabeledIntent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class NameGetter extends Activity implements android.view.View.OnClickListener{
    // variables
    private android.widget.EditText textEdit_Name;
    private android.widget.Button button_Submit;
    private String string_Name;
    private android.content.Intent intent_GetText;
    private android.os.Bundle bundle;
    private android.widget.TextView textView_Top;
    @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.name_getter);

        // assign local variables
        textEdit_Name = (EditText) this.findViewById(R.id.editText_Name);
        button_Submit = (Button) this.findViewById(R.id.button_Submit);
        textView_Top = (TextView) this.findViewById(R.id.LABEL1);
        button_Submit.setOnClickListener(this);

       bundle = this.getIntent().getExtras();

       bundle.getString(string_Name);
       textView_Top.setText(string_Name);


    }

    public void onClick(View v) {
        string_Name = textEdit_Name.getText().toString();
        // create intent to pass
        intent_GetText= new Intent(this,HelloWorld.class);
        // pass to hello world activity
        intent_GetText.putExtra(string_Name,textEdit_Name.getText().toString());
        // initiate 
        this.startActivity(intent_GetText);
    }
}
Manifest.xml
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"
      package=\"edu.calpoly.android.lab1\"
      android:versionCode=\"1\"
      android:versionName=\"1.0\">
    <uses-sdk android:minSdkVersion=\"12\" />

    <application android:icon=\"@drawable/icon\" android:label=\"@string/app_name\">
        <activity android:name=\"NameGetter\"
                  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=\"HelloWorld\" ></activity>

    </application>
</manifest>
    

解决方法

        最可能的原因是
getExtras()
返回null。
bundle = this.getIntent().getExtras();
bundle.getString(string_Name);
要获得更多帮助,建议您收集logcat(
adb logcat
)并发布实际的异常。     ,        
bundle.getString(string_Name);
可能会抛出NullPointer,因为在第一次启动时,将没有捆绑添加到意图中。     ,        我直言不讳地说您需要删除这两行(空指针异常):
 bundle = this.getIntent().getExtras();
 bundle.getString(string_Name);
但是没有日志文件很难分辨。为什么?从主屏幕上轻按您的应用程序时,您将没有多余的东西。     ,        我认为您的问题在这里:
bundle = this.getIntent().getExtras();
bundle.getString(string_Name);
您想在那里做什么? 在第二行的位置,string_Name为空。     ,        我看到的有助于改善生活的事情: 您不必使用完全合格的
android.widget.EditText myEditText
。只需9英镑就可以了。多数民众赞成在您的导入语句处理;) 您的清单中有一个minSdkVersion = \“ 12 \”。这可能会带来问题,因为与您的应用程序相关的操作只能在装有Android 3.1的设备上运行。尝试使用minsdk = 7(Android 2.1及更高版本,多产) 清单中的错误可能更多,但我只有几分钟的时间要看:)     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...