title: Android Intent - got crazy
body: <p>OK, I've been programming all day and searched almost everywhere but nothing yet. I hope, you help me a little bit.
  I have two activities and I want them to communicate. Here is the code</p>
  
  <p><strong>MainActivity</strong></p>
  
  <pre><code>public class MainActivity extends Activity{
  
      ImageView imgsettings;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          imgsettings = (ImageView) findViewById(R.id.imgviewsettings);        
          imgsettings.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  Intent intent = new Intent(MainActivity.this, AppSettings.class);
                  startActivity(intent);
              }
          });
      }
  }
  </code></pre>
  
  <p><strong>Manifest</strong></p>
  
  <pre><code>&lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name=".MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".AppSettings"
              android:label="@string/title_activity_app_settings" &gt; 
          &lt;/activity&gt;
      &lt;/application&gt;
  </code></pre>
  
  <p><strong>AppSettings</strong></p>
  
  <pre><code>package com.example.oldie;
  
  import java.io.BufferedReader;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.OutputStreamWriter;
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class AppSettings extends Activity implements OnClickListener {
  
      TextView lblkeyword, txtkeyword;
      TextView lblnumber, txtnumber;
      TextView lbladdress, txtaddress;
      Button btnok;
      String filename = "preferences.txt";
      public static int count = 0;
      private static String keyword = "";
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          lblkeyword = (TextView) findViewById(R.id.lblkeyword);
          txtkeyword = (TextView) findViewById(R.id.txtkeyword);
          lblnumber = (TextView) findViewById(R.id.lblnumber);
          txtnumber = (TextView) findViewById(R.id.txtnumber);
          lbladdress = (TextView) findViewById(R.id.lbladdress);
          txtaddress = (TextView) findViewById(R.id.txtaddress);
          btnok = (Button) findViewById(R.id.btnok);
  
          btnok.setOnClickListener(this);
          lblkeyword.setOnClickListener(this);
          lblnumber.setOnClickListener(this);
          lbladdress.setOnClickListener(this);
  
          //metritis gia na emfanisei mono mia fora to minima
          if(count==0){
              Toast.makeText(getApplicationContext(), "Tab on the labels for more information", Toast.LENGTH_LONG).show();
              count = count + 1;
          }
  
  
          updateUIFromFiles();
  
      }
  
      @Override
      public void onClick(View v) {
          switch(v.getId()){
          case R.id.lblkeyword:
              Toast.makeText(getApplicationContext(), "Set a keyword set that will trigger the SMS checker", Toast.LENGTH_SHORT).show();
              break;
          case R.id.lblnumber:
              Toast.makeText(getApplicationContext(), "Set a number that the app will communicate in case of emmergency", Toast.LENGTH_SHORT).show();
              break;
          case R.id.lbladdress:
              Toast.makeText(getApplicationContext(), "Set the home address\nex. Agias Lavras 19, Piraeus, Greece", Toast.LENGTH_SHORT).show();
              break;
          case R.id.btnok:
              if((txtkeyword.getText().toString().equals("")) || (txtaddress.getText().toString().equals("")) ||(txtnumber.getText().toString().equals(""))){
                  Toast.makeText(this, "Please fill all the Text Fields.", Toast.LENGTH_SHORT).show();
              }
              else{
                  savePreferences();
                  keyword = txtkeyword.getText().toString();
                  finish();
              }
              break;
          }
  
      }
  
      private void savePreferences() {
          //thewrithike anagkaio na apothikeutoun ta stoixeia se ena arxeio gia na mi xathoun.
          try{
              OutputStreamWriter outfile = new OutputStreamWriter(openFileOutput(filename, 0));
              outfile.write(txtkeyword.getText().toString() + "\n" + txtnumber.getText().toString() + "\n" + txtaddress.getText().toString());
              outfile.close();
              Toast.makeText(this, "The contents are saved in the file.", Toast.LENGTH_LONG).show();
          }
          catch(Throwable t) {
              Toast.makeText(this, "Exception: "+t.toString(), Toast.LENGTH_LONG).show();
              }
      }
  
      private void updateUIFromFiles() {
          try { 
              InputStream infile = openFileInput(filename);
              if (infile != null){
                  InputStreamReader tmp = new InputStreamReader(infile);
                  BufferedReader reader = new BufferedReader(tmp);
                  txtkeyword.setText(reader.readLine());
                  txtnumber.setText(reader.readLine());
                  txtaddress.setText(reader.readLine());
                  infile.close();
              }
          }
          catch (java.io.FileNotFoundException e) {
          // that's OK, we probably haven't created it yet
          }
          catch (Throwable t) {
              Toast.makeText(this, "Exception: "+t.toString(), Toast.LENGTH_LONG).show();
          }
      }
  
      public String GetMessage(){ //Methodos gia na parei to service to keyword.
          return keyword;
      }
  
  }
  </code></pre>
  
  <p><strong>The Problem is</strong></p>
  
  <pre><code>06-19 22:17:03.789: E/AndroidRuntime(17204): FATAL EXCEPTION: main
  06-19 22:17:03.789: E/AndroidRuntime(17204): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.oldie/com.example.oldie.AppSettings}: java.lang.NullPointerException
  06-19 22:17:03.789: E/AndroidRuntime(17204):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
  06-19 22:17:03.789: E/AndroidRuntime(17204):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
  06-19 22:17:03.789: E/AndroidRuntime(17204):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
  06-19 22:17:03.789: E/AndroidRuntime(17204):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
  06-19 22:17:03.789: E/AndroidRuntime(17204):    at android.os.Handler.dispatchMessage(Handler.java:99)
  06-19 22:17:03.789: E/AndroidRuntime(17204):    at android.os.Looper.loop(Looper.java:137)
  06-19 22:17:03.789: E/AndroidRuntime(17204):    at android.app.ActivityThread.main(ActivityThread.java:4441)
  06-19 22:17:03.789: E/AndroidRuntime(17204):    at java.lang.reflect.Method.invokeNative(Native Method)
  06-19 22:17:03.789: E/AndroidRuntime(17204):    at java.lang.reflect.Method.invoke(Method.java:511)
  06-19 22:17:03.789: E/AndroidRuntime(17204):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
  06-19 22:17:03.789: E/AndroidRuntime(17204):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
  06-19 22:17:03.789: E/AndroidRuntime(17204):    at dalvik.system.NativeStart.main(Native Method)
  06-19 22:17:03.789: E/AndroidRuntime(17204): Caused by: java.lang.NullPointerException
  06-19 22:17:03.789: E/AndroidRuntime(17204):    at com.example.oldie.AppSettings.onCreate(AppSettings.java:39)
  06-19 22:17:03.789: E/AndroidRuntime(17204):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
  06-19 22:17:03.789: E/AndroidRuntime(17204):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
  06-19 22:17:03.789: E/AndroidRuntime(17204):    ... 11 more
  </code></pre>
  
  <p>The code for the Intent doesn't work at all. My app crashes and ends. I'm one step before taking pills to control my anger... I' don't know what's wrong and the intent doesn't work. Please help me.</p>