Reputation:
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
MainActivity
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);
}
});
}
}
Manifest
<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>
<activity
android:name=".AppSettings"
android:label="@string/title_activity_app_settings" >
</activity>
</application>
AppSettings
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;
}
}
The Problem is
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
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.
Upvotes: 0
Views: 72
Reputation: 38319
In the AppSettings activity, you are setting the content view to R.layout.activity_main. I'm guessing that not what you intended. It probably does not have the TextViews your expect to find. The calls to find them return null, which causes the exception when you invoke setOnClickListener().
Upvotes: 1
Reputation: 8774
You're using the wrong layout in your AppSettings
activity:
setContentView(R.layout.activity_main);
That's the same as your MainActivity
, which is probably not right. As a result you're button isn't found here:
btnok = (Button) findViewById(R.id.btnok);
and subsequently will be null and when you try to set an onClickListener here:
btnok.setOnClickListener(this);
you get the NullPointerException
.
Upvotes: 0
Reputation: 271
I can't be sure what the problem exactly is without a logcat, but here are some things you can try:
1.) Try using getApplicationContext() instead of MyActivity.this in your Intent constructor.
2.) Check if the AppSettings class is in the same package as your MainActivity class.
I'd try to help more if you post a relevant logcat
Upvotes: 0