New to android, My simple name-getter app is crashing
hey guys I'm just writing a name getter app as I'm learning android and I am forced to close the app when it loads in the emulator. I'm quite new so It's probably something simple.
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.na开发者_StackOverflow中文版me_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>
The most likely cause is that getExtras()
is returning null.
bundle = this.getIntent().getExtras();
bundle.getString(string_Name);
For more help, I suggest collecting the logcat (adb logcat
) and posting the actual exception.
bundle.getString(string_Name);
probably throws a NullPointer since on first start, there will be no bundle added to the intent.
Out of a hunch I'd say you need to remove these two lines (null pointer exception):
bundle = this.getIntent().getExtras();
bundle.getString(string_Name);
But it's hard to tell without the log file. Why? You won't have extras when tapping on your app from the home screen.
I think your problem is here:
bundle = this.getIntent().getExtras();
bundle.getString(string_Name);
What are you trying to do there?
At the point of that second line, string_Name is null.
Things I see to help make life better:
You don't have to use fully qualified android.widget.EditText myEditText
. Just EditText myEditText
is fine. Thats what your import statements handle ;)
You have a minSdkVersion="12" in your manifest. This may present issues, as that correlates to your app will only run on a device with android 3.1. Try using minsdk=7 (Android 2.1 and above, much more prolific)
Likely a lot more wrong in manifest but I have only a few mins to look :)
精彩评论