How to mail TextView data on button click with Android-Intent? - java

I am pretty new to both android and NFC. I am working on an NFC related application as a college project that reads the data from tags and lists them. Although I am able to do so, I am facing problems with intent where I am supposed to mail this list to user on button click.
Can someone please tell me where I am going wrong and help with a detailed step-by-step approach. Huge thanks..!!
Here's the WebServiceActivity:
public class WebServiceActivity extends Activity {
Intent intent = getIntent();
String studlist = intent.getStringExtra("studlist");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webservice);
TextView mailtext = (TextView) findViewById(R.id.students);
TextView mailbody = (TextView) findViewById(R.id.emailtext);
mailbody.setText("Here is the list for Tranport Tracking For Today: \n" + studlist);
Button send=(Button) findViewById(R.id.emailsendbutton);
send.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Log.i("Send email", "");
String[] TO = {"h.trivedi04#gmail.com"};
String[] CC = {"h.trivedi04#gmail.com", "harshit.trivedi22#gmail.com"};
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_CC, CC);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Student Transport Track For Today");
emailIntent.putExtra(Intent.EXTRA_TEXT, studlist);
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
Log.i("Finished sending email...", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(WebServiceActivity.this,
"There is no email client installed.", Toast.LENGTH_SHORT).show();
}
}
});
}
}
And the main activity TransportActivity has:
Button webServiceButton = (Button)this.findViewById(R.id.webServiceButton);
webServiceButton.setOnClickListener(new android.view.View.OnClickListener()
{
public void onClick(View view) {
Intent intent = new Intent( view.getContext(), WebServiceActivity.class);
intent.putExtra("studlist", students.getText().toString());
startActivity(intent);
}
});
Here's the LogCat for the problem:
05-01 21:37:31.267: E/AndroidRuntime(27758): FATAL EXCEPTION: main
05-01 21:37:31.267: E/AndroidRuntime(27758): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.harshit.studenttranstrack/com.harshit.studenttranstrack.WebServiceActivity}: java.lang.NullPointerException
05-01 21:37:31.267: E/AndroidRuntime(27758): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2229)
05-01 21:37:31.267: E/AndroidRuntime(27758): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
05-01 21:37:31.267: E/AndroidRuntime(27758): at android.app.ActivityThread.access$700(ActivityThread.java:165)
05-01 21:37:31.267: E/AndroidRuntime(27758): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
05-01 21:37:31.267: E/AndroidRuntime(27758): at android.os.Handler.dispatchMessage(Handler.java:99)
05-01 21:37:31.267: E/AndroidRuntime(27758): at android.os.Looper.loop(Looper.java:137)
05-01 21:37:31.267: E/AndroidRuntime(27758): at android.app.ActivityThread.main(ActivityThread.java:5455)
05-01 21:37:31.267: E/AndroidRuntime(27758): at java.lang.reflect.Method.invokeNative(Native Method)
05-01 21:37:31.267: E/AndroidRuntime(27758): at java.lang.reflect.Method.invoke(Method.java:525)
05-01 21:37:31.267: E/AndroidRuntime(27758): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
05-01 21:37:31.267: E/AndroidRuntime(27758): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
05-01 21:37:31.267: E/AndroidRuntime(27758): at dalvik.system.NativeStart.main(Native Method)
05-01 21:37:31.267: E/AndroidRuntime(27758): Caused by: java.lang.NullPointerException
05-01 21:37:31.267: E/AndroidRuntime(27758): at com.harshit.studenttranstrack.WebServiceActivity.<init>(WebServiceActivity.java:22)
05-01 21:37:31.267: E/AndroidRuntime(27758): at java.lang.Class.newInstanceImpl(Native Method)

Try it like this inside onCreate() method.
Intent intent = this.getIntent(); //in the WebServiceActivity activity
String studlist = (String) intent .getStringExtra("studlist");

You can't getIntent() before onCreate() .There's simply no Intent available at that point. I believe the same is true for anything that requires a Context.
Set these in onCreate() method
String studlist = this.getIntent().getStringExtra("studlist");

Related

VideoView not playing video (crash)

I made a VideoView for an app in Android Studio. The VideoView plays a video in a new view, pretty simple. It did work all the time, until I changed the source of the video. When I changed the source from 'handleiding' to 'handleidingengels', the console gave the following error:
05-02 22:49:59.450 26710-26710/com.company.app E/MediaPlayer﹕ Unable to create media player
05-02 22:49:59.452 26710-26710/com.company.app D/MediaPlayer﹕ Couldn't open file on client side, trying server side
05-02 22:49:59.459 26710-26721/com.company.app E/MediaPlayer﹕ error (1, -2147483648)
05-02 22:49:59.557 26710-26710/com.company.app E/MediaPlayer﹕ Error (1,-2147483648)
05-02 22:49:59.558 26710-26710/com.company.app D/VideoView﹕ Error: 1,-2147483648
05-02 22:49:59.586 26710-26710/com.company.app
D/AndroidRuntime﹕ Shutting down VM
05-02 22:49:59.586 26710-26710/com.company.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x415ddd40)
05-02 22:49:59.589 26710-26710/com.company.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.company.app, PID: 26710
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:554)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:286)
at android.app.AlertDialog$Builder.show(AlertDialog.java:951)
at android.widget.VideoView$5.onError(VideoView.java:515)
at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:2264)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
This is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
FrameLayout fl = new FrameLayout(this);
fl.setLayoutParams(lp);
final int INSERT_ID = Menu.FIRST;
System.gc();
Intent i = getIntent();
Bundle extras = i.getExtras();
VideoView vv = new VideoView(getApplicationContext());
setContentView(vv);
FrameLayout.LayoutParams lp2 = new FrameLayout.LayoutParams(lp);
lp2.gravity = Gravity.CENTER;
vv.setLayoutParams(lp2);
vv.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.handleidingengels));
vv.setMediaController(new MediaController(this));
vv.requestFocus();
vv.start();
vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setVolume(0, 0);
}
});
}
private void createNote() {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
The issue is this line
VideoView vv = new VideoView(getApplicationContext());
You need to you the Activity context instead
VideoView vv = new VideoView(this);

lockNow() in broadcast receiver

it seems that i get an error when i try to use LockNow() in the broadcast receiver. can anyone help me.
public class Onlockreceive extends BroadcastReceiver {
LockSettings lockactivity;
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
lockactivity.mdevicepolicymanager.lockNow();
}
}
The error message :
12-31 03:14:16.322: D/AndroidRuntime(27478): Shutting down VM 12-31 03:14:16.322: W/dalvikvm(27478): threadid=1: thread exiting with uncaught exception (group=0x41b0d378) 12-31 03:14:16.332: E/AndroidRuntime(27478): FATAL EXCEPTION: main 12-31 03:14:16.332: E/AndroidRuntime(27478): java.lang.RuntimeException: Unable to start receiver com.example.settings2.Onlockreceive: java.lang.NullPointerException 12-31 03:14:16.332: E/AndroidRuntime(27478): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2257) 12-31 03:14:16.332: E/AndroidRuntime(27478): at android.app.ActivityThread.access$1500(ActivityThread.java:138) 12-31 03:14:16.332: E/AndroidRuntime(27478): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 12-31 03:14:16.332: E/AndroidRuntime(27478): at android.os.Handler.dispatchMessage(Handler.java:99) 12-31 03:14:16.332: E/AndroidRuntime(27478): at android.os.Looper.loop(Looper.java:213) 12-31 03:14:16.332: E/AndroidRuntime(27478): at android.app.ActivityThread.main(ActivityThread.java:4787) 12-31 03:14:16.332: E/AndroidRuntime(27478): at java.lang.reflect.Method.invokeNative(Native Method) 12-31 03:14:16.332: E/AndroidRuntime(27478): at java.lang.reflect.Method.invoke(Method.java:511) 12-31 03:14:16.332: E/AndroidRuntime(27478): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 12-31 03:14:16.332: E/AndroidRuntime(27478): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 12-31 03:14:16.332: E/AndroidRuntime(27478): at dalvik.system.NativeStart.main(Native Method) 12-31 03:14:16.332: E/AndroidRuntime(27478): Caused by: java.lang.NullPointerException 12-31 03:14:16.332: E/AndroidRuntime(27478): at com.example.settings2.Onlockreceive.onReceive(Onlockreceive.java:15) 12-31 03:14:16.332: E/AndroidRuntime(27478): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2250) 12-31 03:14:16.332: E/AndroidRuntime(27478): ... 10 more
My problem is similar to this one same problem.. but i don't understand how to fix it
this is the code i use to call the broadcast receiver
private void setupAlarm(int seconds,boolean s) {
// Finish the currently running activity
// MainActivity.this.finish();
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(getBaseContext(), Onlockreceive.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
LockSettings.this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Log.d(TAG, "Setup the alarm");
// Getting current time and add the seconds in it
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, seconds);
if (s == true){
//alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
//alarmManager.setInexactRepeating(type, triggerAtMillis, intervalMillis, operation)
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 5000, pendingIntent );
}else if (s == false)
{
alarmManager.cancel(pendingIntent);
}
}
Either lockactivity or mdevicepolicymanager is null. This is not surprising, as your process may well have been terminated before the BroadcastReceiver got control.
Static data members are only a cache. Do not rely upon them. And most certainly do not put an Activity in a static data member, as you are leaking a lot of memory by doing so.
You can use the Context passed into onReceive() to retrieve a DevicePolicyManager and call lockNow().
UPDATE
Here is the revised class, complete with better formatting and case:
public class OnLockReceive extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
((DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE)).lockNow();
}
}

Setting OnClickListener throws an exception?

I want to know why my application is throwing a runtime exception when I run it? I used to the code in the following link for my application,
dynamically add and remove view to viewpager.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar ab=getActionBar();
pagerAdapter = new MainPagerAdapter();
pager = (ViewPager) findViewById (R.id.pager);
pager.setAdapter (pagerAdapter);
inflater = (LayoutInflater)getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
v0 = (FrameLayout) inflater.inflate (R.layout.listlayout, null);
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(this);
pagerAdapter.addView (v0, 0);
pagerAdapter.notifyDataSetChanged();
}
#Override
public void onClick(View v) {
if(v.getId()==R.id.button1)
{
pagerAdapter.addView (v0);
pagerAdapter.notifyDataSetChanged();
}
}
LogCat:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.todolistworkable/com.example.todolistworkable.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
at android.app.ActivityThread.access$600(ActivityThread.java:128)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4514)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)Caused by: java.lang.NullPointerException
at com.example.todolistworkable.MainActivity.onCreate(MainActivity.java:43)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
the object on line 43 of your code within your onCreate method is null. Most likely the target of one of your findViewById calls is not returning what you expect it to.
See this for more info.
How to interpret Logcat

how to find null pointer exception with logcat

I'm trying to build a small app that takes a data file in external storage and emails it. I keep getting 'null pointer exceptions' right away in logcat and then my app dies. I can't seem to locate my exception and I am wondering if there is a way to determine the line of code that is causing this. I have the MainActivity class and then a class called SendData- the code is below. I'm new at this so any help would be greatly appreciated- thank you.
private static final String TAG = "MainActivity_ErrorLog";
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
// Create the getData intent
Intent intentgetData = new Intent(MainActivity.this, SendData.class);
public void onStart()
{
{
try
{
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite())
{
// verify the paths
String currentDBPath = "TLC_COMMON/database.db";
String backupDBPath = "database.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (currentDB.exists())
{
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
}
catch (Exception e)
{
// change the V below to E when complete
Log.v(TAG,"ERROR: Database file not created");
}
startActivity(intentgetData);
}
}
}
--new class
public class SendData extends Activity
{
private static final String TAG = "MainActivity";
/* Checks if external storage is available to read */
public boolean isExternalStorageReadable()
{
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
{
return true;
}
return false;
}
/** Called when the user clicks the Send My Data button */
public SendData(View view)
{
// Send data by email
{
File root = Environment.getExternalStorageDirectory();
// verify it is saving as this file name; also path in previous class
String fileName = "database.db";
if (root.canWrite())
{
File attachment = new File(root, fileName);
Intent email = new Intent(Intent.ACTION_SENDTO);
email.putExtra(android.content.Intent.EXTRA_SUBJECT, "Exercise data");
email.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"test#gmail.com"});
// is the Uri necessary?
email.putExtra(android.content.Intent.EXTRA_TEXT, Uri.fromFile(attachment));
// look at this VVV
startActivity(Intent.createChooser(email, "Send the data via Email"));}
else
{
// Change the V below to E when complete
Log.v(TAG, "Email send failed");
}
}
}
public void finish()
{
}
}
11-13 13:29:37.343: W/dalvikvm(3319): threadid=1: thread exiting with uncaught exception (group=0x418e3300)
11-13 13:29:37.343: E/AndroidRuntime(3319): FATAL EXCEPTION: main
11-13 13:29:37.343: E/AndroidRuntime(3319): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.va.datasender/com.example.va.datasender.MainActivity}: java.lang.NullPointerException
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.os.Looper.loop(Looper.java:137)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-13 13:29:37.343: E/AndroidRuntime(3319): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 13:29:37.343: E/AndroidRuntime(3319): at java.lang.reflect.Method.invoke(Method.java:511)
11-13 13:29:37.343: E/AndroidRuntime(3319): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-13 13:29:37.343: E/AndroidRuntime(3319): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-13 13:29:37.343: E/AndroidRuntime(3319): at dalvik.system.NativeStart.main(Native Method)
11-13 13:29:37.343: E/AndroidRuntime(3319): Caused by: java.lang.NullPointerException
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.content.ComponentName.<init>(ComponentName.java:75)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.content.Intent.<init>(Intent.java:3301)
11-13 13:29:37.343: E/AndroidRuntime(3319): at com.example.va.datasender.MainActivity.<init>(MainActivity.java:36)
11-13 13:29:37.343: E/AndroidRuntime(3319): at java.lang.Class.newInstanceImpl(Native Method)
11-13 13:29:37.343: E/AndroidRuntime(3319): at java.lang.Class.newInstance(Class.java:1319)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
11-13 13:29:37.343: E/AndroidRuntime(3319): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
11-13 13:29:37.343: E/AndroidRuntime(3319): ... 11 more
Find the lowest "Caused by" and continue down until you reach a line from your code:
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
at android.content.ComponentName.<init>(ComponentName.java:75)
at android.content.Intent.<init>(Intent.java:3301)
at com.example.va.datasender.MainActivity.<init>(MainActivity.java:36)
The <init> means you are doing something as a class variable or inside a constructor before the Activity has a valid Context... But the specific problem is on line 36.
I would guess that you are trying to create an Intent with this. Initialize your Intent inside onCreate() instead.
Found it. Change this:
// Create the getData intent
Intent intentgetData = new Intent(MainActivity.this, SendData.class);
to:
Intent intentgetData;
and inside onCreate() (or onStart() just before calling startActivity()) add:
intentgetData = new Intent(MainActivity.this, SendData.class);
Your problem comes from line 36 on your MainActivity. Check there for the problem.
11-13 13:29:37.343: E/AndroidRuntime(3319): at com.example.va.datasender.MainActivity.<init>(MainActivity.java:36)
The exeption from line number 36: at com.example.va.datasender.MainActivity.<init>(MainActivity.java:36).
Since the error log also shows android.content.Intent.<init> after the MainActivity.java:36, I would check the value for the first parameter in the following line, when instantiating intentgetData:
Intent intentgetData = new Intent(MainActivity.this, SendData.class);
Instead of having the instatiation done there, try doing it in the onStart() method just before calling startActivity(intentgetData);:
Intent intentgetData = new Intent(MainActivity.this, SendData.class);
startActivity(intentgetData);

App Crashes on Launch, Trying to see if file exist's for login screen

I'm creating a Class that checks to see if the file has been created (Has username and passwords.) and if it does it creates an intent to go to another class to read the data and check it againts a server via FTP. For some reason, I can't get it to work, I've tried everything and read every single web page I could, but no luck.
My Code:
public class LogIn extends Activity implements OnClickListener {
Button send;
EditText user;
EditText pass;
CheckBox staySignedIn;
FileOutputStream Fos;
String a;
String b;
String string = a;
String string2 = b;
String FILENAME = "userandpass";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
send = (Button) findViewById(R.id.bLogIn);
user = (EditText) findViewById(R.id.eTuser);
pass = (EditText) findViewById(R.id.eTpassword);
staySignedIn = (CheckBox) findViewById(R.id.Cbstay);
send.setOnClickListener(this);
if (staySignedIn.isChecked()) {
String a = user.getText().toString();
String b = pass.getText().toString();
File f = new File(FILENAME);
try {
Fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
Fos.write(string.getBytes());
Fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File file = getBaseContext().getFileStreamPath(FILENAME);
if(file.exists());
Intent i = new Intent(LogIn.this, ChatService.class);
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bLogIn:
if (pass.length() == 0)
Toast.makeText(this,
"Try to type in your username and password again!",
Toast.LENGTH_LONG).show();
else if (user.length() == 0)
Toast.makeText(this,
"Try to type in your username and password again!",
Toast.LENGTH_LONG).show();
else {
String u = user.getText().toString();
String p = pass.getText().toString();
Bundle send = new Bundle();
send.putString("key", u);
send.putString("key1", p);
Intent a = new Intent(LogIn.this, logincheck.class);
a.putExtra("key", u);
a.putExtra("key1", p);
startActivity(a);
Toast.makeText(this, "Were signing you in!", Toast.LENGTH_LONG)
.show();
break;
}
}
}
}
LogCat:
01-19 11:37:17.601: W/dalvikvm(4411): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-19 11:37:17.621: E/AndroidRuntime(4411): FATAL EXCEPTION: main
01-19 11:37:17.621: E/AndroidRuntime(4411): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gta5news.bananaphone/com.gta5news.bananaphone.LogIn}: java.lang.NullPointerException
01-19 11:37:17.621: E/AndroidRuntime(4411): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-19 11:37:17.621: E/AndroidRuntime(4411): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-19 11:37:17.621: E/AndroidRuntime(4411): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-19 11:37:17.621: E/AndroidRuntime(4411): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-19 11:37:17.621: E/AndroidRuntime(4411): at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 11:37:17.621: E/AndroidRuntime(4411): at android.os.Looper.loop(Looper.java:123)
01-19 11:37:17.621: E/AndroidRuntime(4411): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-19 11:37:17.621: E/AndroidRuntime(4411): at java.lang.reflect.Method.invokeNative(Native Method)
01-19 11:37:17.621: E/AndroidRuntime(4411): at java.lang.reflect.Method.invoke(Method.java:521)
01-19 11:37:17.621: E/AndroidRuntime(4411): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-19 11:37:17.621: E/AndroidRuntime(4411): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-19 11:37:17.621: E/AndroidRuntime(4411):
at dalvik.system.NativeStart.main(Native Method)
01-19 11:37:17.621: E/AndroidRuntime(4411): Caused by: java.lang.NullPointerException
01-19 11:37:17.621: E/AndroidRuntime(4411): at com.gta5news.bananaphone.LogIn.onCreate(LogIn.java:55)
01-19 11:37:17.621: E/AndroidRuntime(4411): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-19 11:37:17.621: E/AndroidRuntime(4411): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
If line 55 is
Fos.write(string.getBytes());
then either Fos or string is uninitialized. Given that string is initialized to a which is itself uninitialized, that explains it. You need to assign a proper value to string.
Fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
It seems for some reason openFileOutput returning null which makes Fos null, which is throwing NullPointerException.
Add
if(Fos != null) {
Fos.write(string.getBytes());
Fos.close();
} check.
OR catch NullpointerException.

Categories

Resources