Reputation: 344
I'm currently using Eclipse to do a simple interface that allow user to insert their name then will intent to other page.
However I'm stuck now because some strange problem. Although it shows that "Shutting Down VM" but I doubt that's the main reason.
here's my code
package com.ytl.test;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.ytl.test.MainActivity;
public class MainActivity extends Activity {
private TextView tvName;
private EditText Name;
private Button btnConfirm = new Button(this) ;
private String stringName = new String();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
tvName = new TextView(this);
tvName.setText("Please enter your name: ");
Name = new EditText(this);
Name.setHint("Your Name");
btnConfirm = new Button(this);
btnConfirm.setText("Confirm");
btnConfirm.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
stringName = Name.getText().toString();
if(stringName.isEmpty()){
alertDialog.setTitle("Error");
alertDialog.setMessage("Name cannot be left empty. Please insert your name");
alertDialog.setCancelable(false);
alertDialog.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
Name.setFocusable(true);
Name.setFocusableInTouchMode(true);
Name.requestFocus();
}
});
alertDialog.show();
Name.requestFocus();
}
else{
StringBuffer sbName = new StringBuffer();
sbName.append(stringName);
Intent intNext = new Intent(MainActivity.this,MainPage.class);
intNext.putExtra("Name",stringName.toString()); // i.putExtra only allow for String
startActivity(intNext);
}
}
});
layout.addView(tvName);
layout.addView(Name);
layout.addView(btnConfirm);
setContentView(layout);
}
@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;
}
}
and here's my logcat
03-15 14:37:38.143: D/AndroidRuntime(7217): Shutting down VM
03-15 14:37:38.143: W/dalvikvm(7217): threadid=1: thread exiting with uncaught exception (group=0x55e04b20)
03-15 14:37:38.143: D/AndroidRuntime(7217): procName from cmdline: com.ytl.test
03-15 14:37:38.143: E/AndroidRuntime(7217): in writeCrashedAppName, pkgName :com.ytl.test
03-15 14:37:38.143: D/AndroidRuntime(7217): file written successfully with content: com.ytl.test StringBuffer : ;com.ytl.test
03-15 14:37:38.153: E/AndroidRuntime(7217): FATAL EXCEPTION: main
03-15 14:37:38.153: E/AndroidRuntime(7217): Process: com.ytl.test, PID: 7217
03-15 14:37:38.153: E/AndroidRuntime(7217): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ytl.test/com.ytl.test.MainActivity}: java.lang.NullPointerException
03-15 14:37:38.153: E/AndroidRuntime(7217): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
03-15 14:37:38.153: E/AndroidRuntime(7217): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-15 14:37:38.153: E/AndroidRuntime(7217): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-15 14:37:38.153: E/AndroidRuntime(7217): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-15 14:37:38.153: E/AndroidRuntime(7217): at android.os.Handler.dispatchMessage(Handler.java:102)
03-15 14:37:38.153: E/AndroidRuntime(7217): at android.os.Looper.loop(Looper.java:136)
03-15 14:37:38.153: E/AndroidRuntime(7217): at android.app.ActivityThread.main(ActivityThread.java:5021)
03-15 14:37:38.153: E/AndroidRuntime(7217): at java.lang.reflect.Method.invokeNative(Native Method)
03-15 14:37:38.153: E/AndroidRuntime(7217): at java.lang.reflect.Method.invoke(Method.java:515)
03-15 14:37:38.153: E/AndroidRuntime(7217): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
03-15 14:37:38.153: E/AndroidRuntime(7217): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
03-15 14:37:38.153: E/AndroidRuntime(7217): at dalvik.system.NativeStart.main(Native Method)
03-15 14:37:38.153: E/AndroidRuntime(7217): Caused by: java.lang.NullPointerException
03-15 14:37:38.153: E/AndroidRuntime(7217): at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
03-15 14:37:38.153: E/AndroidRuntime(7217): at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
03-15 14:37:38.153: E/AndroidRuntime(7217): at android.view.View.<init>(View.java:3443)
03-15 14:37:38.153: E/AndroidRuntime(7217): at android.view.View.<init>(View.java:3510)
03-15 14:37:38.153: E/AndroidRuntime(7217): at android.widget.TextView.<init>(TextView.java:623)
03-15 14:37:38.153: E/AndroidRuntime(7217): at android.widget.Button.<init>(Button.java:107)
03-15 14:37:38.153: E/AndroidRuntime(7217): at android.widget.Button.<init>(Button.java:103)
03-15 14:37:38.153: E/AndroidRuntime(7217): at android.widget.Button.<init>(Button.java:99)
03-15 14:37:38.153: E/AndroidRuntime(7217): at com.ytl.test.MainActivity.<init>(MainActivity.java:22)
03-15 14:37:38.153: E/AndroidRuntime(7217): at java.lang.Class.newInstanceImpl(Native Method)
03-15 14:37:38.153: E/AndroidRuntime(7217): at java.lang.Class.newInstance(Class.java:1208)
03-15 14:37:38.153: E/AndroidRuntime(7217): at android.app.Instrumentation.newActivity(Instrumentation.java:1064)
03-15 14:37:38.153: E/AndroidRuntime(7217): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
03-15 14:37:38.153: E/AndroidRuntime(7217): ... 11 more
03-15 14:37:38.213: D/AndroidRuntime(7230): Shutting down VM
03-15 14:37:38.213: W/dalvikvm(7230): threadid=1: thread exiting with uncaught exception (group=0x55e04b20)
Thanks in advance for the help
Update of my XML file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ytl.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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>
<service android:name=".BackgroundService" >
</service>
<activity
android:name=".MainPage"
android:label="@string/title_activity_main_page" >
</activity>
</application>
</manifest>
Upvotes: 2
Views: 2778
Reputation: 3042
This happens because you create a new Button
using the this
-keyword before this
equals to something.
You can't use the this
-keyword outside of the onCreate()
or methods which are members of the class (non-static).
The solution is simply to initialize the Button
and the AlertDialog.Builder
in the onCreate()
method instead.
Upvotes: 0
Reputation: 48871
As far as I can tell the problem is with the first of these two lines (although the second will also cause the same exception if allowed to execute)...
private Button btnConfirm = new Button(this) ;
...
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
...you can't use this
in the main body of an Activity
as the Activity
hasn't been fully instantiated at that stage and this
will be null.
Change them to...
private Button btnConfirm;
...
AlertDialog.Builder alertDialog;
You are instantiating your btnConfirm
in your onCreate(...)
method anyway so all you need to do is add a line such as...
alertDialog = new AlertDialog.Builder(this);
...somewhere in your onCreate(...)
method also.
Upvotes: 1