Hi I'm trying to build a calculator to calculate compound interest, I'm keep getting a java.lang.RuntimeException error. I've read some topics on stackoverflow and their problems seems something wrong with their AndroidManifest.xml. I didn't edit that file at all but I'm still getting this error.
Here's the code:
package com.hychentsa.compoundinterest_v001;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.lang.Math;
public class MainActivity extends Activity {
private Button btn1;
private EditText edt1, edt2, edt3, edt4 ,edt5, edt6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.btn1);
edt1 = (EditText)findViewById(R.id.edt1);
edt2 = (EditText)findViewById(R.id.edt2);
edt3 = (EditText)findViewById(R.id.edt3);
edt4 = (EditText)findViewById(R.id.edt4);
edt5 = (EditText)findViewById(R.id.edt5);
edt6 = (EditText)findViewById(R.id.edt6);
btn1.setOnClickListener(btn1listener);
}
double capital = Double.parseDouble(edt1.getText().toString());
double everyyear = Double.parseDouble(edt2.getText().toString());
double interest = Double.parseDouble(edt3.getText().toString());
double year = Double.parseDouble(edt4.getText().toString());
private Button.OnClickListener btn1listener = new Button.OnClickListener()
{
public void onClick (View v)
{
double capital = Double.parseDouble(edt1.getText().toString());
double everyyear = Double.parseDouble(edt2.getText().toString());
double interest = Double.parseDouble(edt3.getText().toString());
double year = Double.parseDouble(edt4.getText().toString());
double interestperyear = capital * Math.pow(1 + interest, year) +
everyyear * (1 - Math.pow(1 + interest, year)) / interest * -1;
edt5.setText(String.valueOf(interestperyear));
edt6.setText(String.valueOf(interestperyear / 12));
}
};
#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 the logcat:
07-15 13:45:23.640: D/AndroidRuntime(1763): Shutting down VM
07-15 13:45:23.650: W/dalvikvm(1763): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-15 13:45:23.660: E/AndroidRuntime(1763): FATAL EXCEPTION: main
07-15 13:45:23.660: E/AndroidRuntime(1763): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.hychentsa.compoundinterest_v001/com.hychentsa.compoundinterest_v001.MainActivity}: java.lang.NullPointerException
07-15 13:45:23.660: E/AndroidRuntime(1763): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
07-15 13:45:23.660: E/AndroidRuntime(1763): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-15 13:45:23.660: E/AndroidRuntime(1763): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-15 13:45:23.660: E/AndroidRuntime(1763): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-15 13:45:23.660: E/AndroidRuntime(1763): at android.os.Handler.dispatchMessage(Handler.java:99)
07-15 13:45:23.660: E/AndroidRuntime(1763): at android.os.Looper.loop(Looper.java:137)
07-15 13:45:23.660: E/AndroidRuntime(1763): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-15 13:45:23.660: E/AndroidRuntime(1763): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 13:45:23.660: E/AndroidRuntime(1763): at java.lang.reflect.Method.invoke(Method.java:511)
07-15 13:45:23.660: E/AndroidRuntime(1763): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-15 13:45:23.660: E/AndroidRuntime(1763): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-15 13:45:23.660: E/AndroidRuntime(1763): at dalvik.system.NativeStart.main(Native Method)
07-15 13:45:23.660: E/AndroidRuntime(1763): Caused by: java.lang.NullPointerException
07-15 13:45:23.660: E/AndroidRuntime(1763): at com.hychentsa.compoundinterest_v001.MainActivity.<init>(MainActivity.java:30)
07-15 13:45:23.660: E/AndroidRuntime(1763): at java.lang.Class.newInstanceImpl(Native Method)
07-15 13:45:23.660: E/AndroidRuntime(1763): at java.lang.Class.newInstance(Class.java:1319)
07-15 13:45:23.660: E/AndroidRuntime(1763): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
07-15 13:45:23.660: E/AndroidRuntime(1763): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
07-15 13:45:23.660: E/AndroidRuntime(1763): ... 11 more
double capital = Double.parseDouble(edt1.getText().toString());
Happens at the init of your activity, which is much before you call edt1 = (EditText)findViewById(R.id.edt1);.
You can declare your double capital ... in the implicit init of the class, but the reference to edt1 must be after onCreate is called.
Also, the actual content of edt1.getText is not relevant even at this point as it is empty, hence will also cause an Exception.
Note that declaring the variable under the onCreate method doesn't make it be called after onCreate.
I don't think you need those 4 lines al all:
double capital = Double.parseDouble(edt1.getText().toString());
double everyyear = Double.parseDouble(edt2.getText().toString());
double interest = Double.parseDouble(edt3.getText().toString());
double year = Double.parseDouble(edt4.getText().toString());
Remove the below which is before button listener. This causes NPE. Its outside of the method. Also you have the same inside button click so you can remove the ones outside the methods.
double capital = Double.parseDouble(edt1.getText().toString());
double everyyear = Double.parseDouble(edt2.getText().toString());
double interest = Double.parseDouble(edt3.getText().toString());
double year = Double.parseDouble(edt4.getText().toString());
Related
I have popupMenu and CheckBox. I need make write status CheckBox to boolean.
This code not working:
MenuItem fast_result;
boolean fast=false;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.FastResult:
fast_result = item.getSubMenu().getItem(R.id.FastResult);//This is 182 line
fast_result.setChecked(!fast_result.isChecked());
fast=fast_result.isChecked();
return true;
}
}
It is errors:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.alexvsalex.HelpforMath.RootsActivity.onOptionsItemSelected(RootsActivity.java:182)
at android.app.Activity.onMenuItemSelected(Activity.java:2502)
at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:950)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:163)
at android.widget.AdapterView.performItemClick(AdapterView.java:292)
at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
at android.widget.AbsListView$1.run(AbsListView.java:3168)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
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:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
What to do?
The problem is solved:
case R.id.FastResult:
fast_result = item; //There was an error
fast_result.setChecked(!fast_result.isChecked());
fast=fast_result.isChecked();
return true;
I'm getting a NullPointerException while trying to start an Activity which contains a ListView .
In the getView method of the adapter class, the exception happens when the setText function of a textView is being called .
The code bellow is my adapter class:
public class QuestionsListAdapter extends ArrayAdapter<Question> {
Context context;
List<Question> questions;
public QuestionsListAdapter(Context context, List<Question> questions){
super(context, R.layout.list_item_question, questions);
this.context = context;
this.questions = questions;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = vi.inflate(R.layout.list_item_question, null);
Question question = questions.get(position);
TextView tv = (TextView) view.findViewById(R.id.question_list_item_string);
Log.i(TableCreator.LOG_TAG, question.toString()); //this works fine and the string is not null .
tv.setText(question.toString()+""); //NULL POINTER EXCEPTION .
return view;
}
}
As you see, I've logged the string in the logcat and it works just fine, but the next line makes the mistake .
And this is the logcat output:
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist I/Operation Checklist﹕ |-Question-> id:1 summary:mySummary comment:myComment solution:mySolution ownerList:dummyOwner
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist D/AndroidRuntime﹕ Shutting down VM
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb0f5f648)
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at org.kabiri.operationchecklist.adapter.QuestionsListAdapter.getView(QuestionsListAdapter.java:43)
at android.widget.AbsListView.obtainView(AbsListView.java:2177)
at android.widget.ListView.makeAndAddView(ListView.java:1840)
at android.widget.ListView.fillDown(ListView.java:675)
at android.widget.ListView.fillFromTop(ListView.java:736)
at android.widget.ListView.layoutChildren(ListView.java:1655)
at android.widget.AbsListView.onLayout(AbsListView.java:2012)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
The logcat shows that the error happens on this line of my adapter class:
tv.setText(question.toString()+"");
I really appreciate your help .
You already know where the problem is!
tv.setText(question.toString()+"");
is causing the NPE that means the TextView tv is null. And that means that the line
TextView tv = (TextView) view.findViewById(R.id.question_list_item_string);
is not able to find the TextView. Check the question_list_item_string id and make sure it matches the id in your list_item_question.xml file
So I am making an app that takes data from a webpage as text, parses it into data structures, and then displays it in a table. The trouble is, I wrote the code that creates the data structures and places the data in a table, but now, the app crashes when I run it. Here is the relevant code:
protected void onPostExecute(String result) {
String s1 = makeReadable(result);
ArrayList <OfferRequest> al = makeStructures(s1);
makeRows(al);
}
}
public String makeReadable(String input){
String result = input.replace("~~", "[empty]");
String result2 = result.replace("~", "");
String result3 = result2.replace("<", "");
String result4 = result3.replace(">", "");
return result4;
}
public ArrayList <OfferRequest> makeStructures(String input){
OfferRequest or;
ArrayList <OfferRequest> orList = new ArrayList <OfferRequest>();
String [] items = input.split ("\n");
String [] splitItems;
for(int i = 0; i < items.length; i++){
splitItems = items[i].split("|");
int id = Integer.parseInt(splitItems[0]);
int isOffer = Integer.parseInt(splitItems[1]);
boolean isOffer2 = (isOffer != 0);
int units = Integer.parseInt(splitItems[2]);
double price = Double.parseDouble(splitItems[3]);
String currency = splitItems[4];
String created = splitItems[5];
String specs = splitItems[6];
String comment = splitItems[7];
String companyName = splitItems[8];
String category = splitItems[9];
String itemName = splitItems[10];
String user = splitItems[11];
String toBeDelivered = splitItems[12];
String lastUpdated = splitItems[13];
or = new OfferRequest(id, isOffer2, units, price, currency, created, specs, comment, companyName, category, itemName, user, toBeDelivered, lastUpdated);
orList.add(or);
}
return orList;
}
public void makeRows(ArrayList <OfferRequest> input){
TableRow t;
TextView t1, t2, t3, t4, t5, t6, t7, t8;
for (int i = 0; i < input.size(); i++){
t1 = new TextView(this);
t2 = new TextView(this);
t3 = new TextView(this);
t4 = new TextView(this);
t5 = new TextView(this);
t6 = new TextView(this);
t7 = new TextView(this);
t8 = new TextView(this);
t1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
t2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
t3.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
t4.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
t5.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
t6.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
t7.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
t8.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
t1.setText(input.get(i).getCategory());
t2.setText(input.get(i).getItemName());
t3.setText(Integer.toString(input.get(i).getUnits()));
t4.setText(input.get(i).getCurrency());
t5.setText(Double.toString(input.get(i).getPrice()));
t6.setText(input.get(i).getUser());
t7.setText("Unknown");
t8.setText(input.get(i).getCreated());
t = new TableRow(this);
t.addView(t1);
t.addView(t2);
t.addView(t3);
t.addView(t4);
t.addView(t5);
t.addView(t6);
t.addView(t7);
t.addView(t8);
table.addView(t);
}
}
Am I just overloading the device's processor or something? The app ran, albeit slowly, when I just had it printing unformatted data in a table.
Here's the logcat stuff:
04-29 12:11:37.570: E/AndroidRuntime(1763): FATAL EXCEPTION: main
04-29 12:11:37.570: E/AndroidRuntime(1763): java.lang.NumberFormatException: Invalid int: ""
04-29 12:11:37.570: E/AndroidRuntime(1763): at java.lang.Integer.invalidInt(Integer.java:138)
04-29 12:11:37.570: E/AndroidRuntime(1763): at java.lang.Integer.parseInt(Integer.java:359)
04-29 12:11:37.570: E/AndroidRuntime(1763): at java.lang.Integer.parseInt(Integer.java:332)
04-29 12:11:37.570: E/AndroidRuntime(1763): at com.onegdd.orbit.MainActivity.makeStructures(MainActivity.java:263)
04-29 12:11:37.570: E/AndroidRuntime(1763): at com.onegdd.orbit.MainActivity.makeReadable(MainActivity.java:253)
04-29 12:11:37.570: E/AndroidRuntime(1763): at com.onegdd.orbit.MainActivity$DownloadWebpageTask.onPostExecute(MainActivity.java:203)
04-29 12:11:37.570: E/AndroidRuntime(1763): at com.onegdd.orbit.MainActivity$DownloadWebpageTask.onPostExecute(MainActivity.java:1)
04-29 12:11:37.570: E/AndroidRuntime(1763): at android.os.AsyncTask.finish(AsyncTask.java:631)
04-29 12:11:37.570: E/AndroidRuntime(1763): at android.os.AsyncTask.access$600(AsyncTask.java:177)
04-29 12:11:37.570: E/AndroidRuntime(1763): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
04-29 12:11:37.570: E/AndroidRuntime(1763): at android.os.Handler.dispatchMessage(Handler.java:99)
04-29 12:11:37.570: E/AndroidRuntime(1763): at android.os.Looper.loop(Looper.java:137)
04-29 12:11:37.570: E/AndroidRuntime(1763): at android.app.ActivityThread.main(ActivityThread.java:4895)
04-29 12:11:37.570: E/AndroidRuntime(1763): at java.lang.reflect.Method.invokeNative(Native Method)
04-29 12:11:37.570: E/AndroidRuntime(1763): at java.lang.reflect.Method.invoke(Method.java:511)
04-29 12:11:37.570: E/AndroidRuntime(1763): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
04-29 12:11:37.570: E/AndroidRuntime(1763): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
04-29 12:11:37.570: E/AndroidRuntime(1763): at dalvik.system.NativeStart.main(Native Method)
Nothing to do with 'overloading the processor', it's a data problem:
12:11:37.570: E/AndroidRuntime(1763): java.lang.NumberFormatException: Invalid int: ""
You're code will need to check for dodgy data if you want it to be robust.
Yes you are getting .. java.lang.NumberFormatException
Try like this if number is not in int format ...
Integer.parseInt(string);
I have some problems with my simple app in Android with Java code. I'm trying to set a RadioGroup that works like settings for color of buttons. When I start my app in Settings activity (Settings.java), it crashes.
package com.app.testing;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class Settings extends Main implements OnCheckedChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.ButtonSettingsView);
int checkedRadioButton = radioGroup.getCheckedRadioButtonId();
switch (checkedRadioButton) {
case R.id.redbtn :
add.setBackgroundColor(21);
break;
case R.id.blubtn :
add.setBackgroundColor(58);
break;
case R.id.grebtn :
add.setBackgroundColor(13);
break;
}
Button back = (Button) findViewById(R.id.back);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
}
}
Log:
05-27 16:27:49.611: E/AndroidRuntime(4970): FATAL EXCEPTION: main
05-27 16:27:49.611: E/AndroidRuntime(4970): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.testing/com.app.testing.Settings}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.RadioGroup
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.os.Handler.dispatchMessage(Handler.java:99)
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.os.Looper.loop(Looper.java:137)
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-27 16:27:49.611: E/AndroidRuntime(4970): at java.lang.reflect.Method.invokeNative(Native Method)
05-27 16:27:49.611: E/AndroidRuntime(4970): at java.lang.reflect.Method.invoke(Method.java:511)
05-27 16:27:49.611: E/AndroidRuntime(4970): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-27 16:27:49.611: E/AndroidRuntime(4970): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-27 16:27:49.611: E/AndroidRuntime(4970): at dalvik.system.NativeStart.main(Native Method)
05-27 16:27:49.611: E/AndroidRuntime(4970): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.RadioGroup
05-27 16:27:49.611: E/AndroidRuntime(4970): at com.app.testing.Settings.onCreate(Settings.java:16)
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.app.Activity.performCreate(Activity.java:5104)
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-27 16:27:49.611: E/AndroidRuntime(4970): ... 11 more
Thanks
It seems the problem is in this line
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.ButtonSettingsView);
May be you are trying to cast a TextView to RadioGroup. Check your xml. I am afraid that your id ButtonSettingsView is a textView
I think the LogCat says it all:
Check your XML layout file: is ButtonSettingsView actually a RadioGroup?
I try to change 3 textviews in a class called UserProfile() calling the method update() from the class UpdateProfile(), the class UserProfile do something like this:
package com.safm;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class UserProfile extends Activity {
TextView username, usersurname, useremail;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
username = (TextView) findViewById(R.id.username);
usersurname = (TextView) findViewById(R.id.usersurname);
useremail = (TextView) findViewById(R.id.useremail);
}
public void updateButton(View view){
Intent i = new Intent(this, UpdateProfile.class);
startActivity(i);
}
public void update(String nname, String nusername, String nemail){
System.out.println("2");
System.out.println(nname);
username.setText(nname);
usersurname.setText(nusername);
useremail.setText(nemail);
System.out.println("3");
}
}
The updateButton method invoke the UpdateProfile class:
package com.safm;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class UpdateProfile extends Activity {
EditText newusernametxt, newsurnametxt, newemailtxt;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_actualizarperfil);
newusernametxt = (EditText) findViewById(R.id.newusernametxt);
newsurnametxt = (EditText) findViewById(R.id.newsurnametxt );
newemailtxt = (EditText) findViewById(R.id.newemailtxt );
}
public void updateInfo(View view){
String nname = newusernametxt .getText().toString();
String nsurname = newsurnametxt .getText().toString();
String nemail = newemailtxt .getText().toString();
if(nname.compareTo("") != 0 && nsurname.compareTo("") != 0 && nemail.compareTo("") != 0){
UserProfile profile = new UserProfile();
System.out.println("1");
profile.update(nname, nsurname, nemail);
System.out.println("4");
Toast.makeText(this, "Success, the profile has been updated", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "You can't empty fields", Toast.LENGTH_SHORT).show();
}
}
This is my logcat file, as you can see, the first 2 flags are printed correctly, the error is when I try to set another text on the TextView, but the value of the strings (nname, nsurname and nemail) are not empty cause also is printed in the console HELP!
05-14 21:43:31.140: I/System.out(878): 1
05-14 21:43:31.140: I/System.out(878): 2
05-14 21:43:31.150: I/System.out(878): TextEditValue
05-14 21:43:31.150: D/AndroidRuntime(878): Shutting down VM
05-14 21:43:31.150: W/dalvikvm(878): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-14 21:43:31.310: E/AndroidRuntime(878): FATAL EXCEPTION: main
05-14 21:43:31.310: E/AndroidRuntime(878): java.lang.IllegalStateException: Could not execute method of the activity
05-14 21:43:31.310: E/AndroidRuntime(878): at android.view.View$1.onClick(View.java:3599)
05-14 21:43:31.310: E/AndroidRuntime(878): at android.view.View.performClick(View.java:4204)
05-14 21:43:31.310: E/AndroidRuntime(878): at android.view.View$PerformClick.run(View.java:17355)
05-14 21:43:31.310: E/AndroidRuntime(878): at android.os.Handler.handleCallback(Handler.java:725)
05-14 21:43:31.310: E/AndroidRuntime(878): at android.os.Handler.dispatchMessage(Handler.java:92)
05-14 21:43:31.310: E/AndroidRuntime(878): at android.os.Looper.loop(Looper.java:137)
05-14 21:43:31.310: E/AndroidRuntime(878): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-14 21:43:31.310: E/AndroidRuntime(878): at java.lang.reflect.Method.invokeNative(Native Method)
05-14 21:43:31.310: E/AndroidRuntime(878): at java.lang.reflect.Method.invoke(Method.java:511)
05-14 21:43:31.310: E/AndroidRuntime(878): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-14 21:43:31.310: E/AndroidRuntime(878): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-14 21:43:31.310: E/AndroidRuntime(878): at dalvik.system.NativeStart.main(Native Method)
05-14 21:43:31.310: E/AndroidRuntime(878): Caused by: java.lang.reflect.InvocationTargetException
05-14 21:43:31.310: E/AndroidRuntime(878): at java.lang.reflect.Method.invokeNative(Native Method)
05-14 21:43:31.310: E/AndroidRuntime(878): at java.lang.reflect.Method.invoke(Method.java:511)
05-14 21:43:31.310: E/AndroidRuntime(878): at android.view.View$1.onClick(View.java:3594)
05-14 21:43:31.310: E/AndroidRuntime(878): ... 11 more
05-14 21:43:31.310: E/AndroidRuntime(878): Caused by: java.lang.NullPointerException
05-14 21:43:31.310: E/AndroidRuntime(878): at com.safm.UserProfile.update(UserProfile.java:31)
05-14 21:43:31.310: E/AndroidRuntime(878): at com.safm.UserProfile.updateInfo(UpdateProfile.java:33)
05-14 21:43:31.310: E/AndroidRuntime(878): ... 14 more
NOTE: I've clean the project and defined the TextView in ProfileUser class and the TextEdit in UpdateProfile outside the onCreate method but doesn't work.
Thanks.
Activities in Android are separate containers, that are in no way allowed to directly touch eachother. This is because the application stack behind Android 'freezes' the activities when they are no longer on the foreground, and could even be swapped to hard storage to save on RAM at the operating system's discretion.
Your UserProfile encapsulation should just reload its data after the UpdateProfile activity is closed, or request communication back the proper way via startActivityForResult.
Your current implementation is technically incorrect from each perspective, since you're hardcoding the UpdateProfile activity to always be called from a UserProfile activity. What if some day you introduce a new menu option to call it directly from the home screen? Activities are always separate, and should not make such assumptions.