I'm trying to start a service, but always getting NPE. What could I be doing wrong?
Even the onServiceConnected() method does not get called, why ever?
#Override
protected void onCreate(Bundle savedInstanceState) {
mServiceIntent = new Intent(this, MyService.class);
bindService(mServiceIntent, mConnection, Context.BIND_AUTO_CREATE);
//other stuff
}
usage:
private okButton(View v) {
startService(mServiceIntent);
}
result: NullPointerException!
private ServiceConnection mConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className, IBinder service) {
Log.v("service", "gets never executed!");
mDownloadService = ((LocalBinder<MyService>) service).getService();
}
#Override
public void onServiceDisconnected(ComponentName className) {
}
};
}
public class LocalBinder<S> extends Binder {
private String TAG = "LocalBinder";
private WeakReference<S> mService;
public LocalBinder(S service){
mService = new WeakReference<S>(service);
}
public S getService() {
return mService.get();
}
}
Of course I also have the service in manifest:
<service android:enabled="true" android:name=".service.MyService" />
Logcat:
06-06 23:48:20.411: W/dalvikvm(592): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
06-06 23:48:20.461: E/AndroidRuntime(592): FATAL EXCEPTION: main
06-06 23:48:20.461: E/AndroidRuntime(592): java.lang.RuntimeException: Unable to start activity ComponentInfo{MyActivity}: java.lang.NullPointerException
06-06 23:48:20.461: E/AndroidRuntime(592): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.os.Looper.loop(Looper.java:137)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-06 23:48:20.461: E/AndroidRuntime(592): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 23:48:20.461: E/AndroidRuntime(592): at java.lang.reflect.Method.invoke(Method.java:511)
06-06 23:48:20.461: E/AndroidRuntime(592): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-06 23:48:20.461: E/AndroidRuntime(592): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-06 23:48:20.461: E/AndroidRuntime(592): at dalvik.system.NativeStart.main(Native Method)
06-06 23:48:20.461: E/AndroidRuntime(592): Caused by: java.lang.NullPointerException
06-06 23:48:20.461: E/AndroidRuntime(592): at MyActivity.okButton(MyActivity.java:217)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1939)
06-06 23:48:20.461: E/AndroidRuntime(592): ... 11 more
The intent seems to be ok:
Log.v("service", String.valueOf(mServiceIntent == null)); //false
Your stack trace clearly shows the NullPointerException happening in okButton(), and according to the code you posted, the only line in that function is
startService(mServiceIntent);
So clearly mServiceIntent is null at this point, despite being set in onCreate().
Do you have any other lines where you set mServiceIntent?
Or do you have a constructor in which you call okButton()? A constructor is the only thing I can think of that would run before onCreate(). That would also explain why onServiceConnected() is never run despite the call to bindService() in onCreate().
This code is not proper Java syntax, as the method definition has no return type :
private okButton(View v) {
startService(mServiceIntent);
}
You need something like:
private void okButton(View v) {
startService(mServiceIntent);
}
It looks to me from the stack trace that the okButton() method is being called during the creation of the MyActivity instance before the onCreate() method is called and of course at that time mService will be null.
Post more code or correct the code you've posted so that it actually matches what you are using.
Related
im making a simple calories calculator and it gives me that error, i already reviewed the code and searched here for the solution but im not able to see why is not running.
I thought it was because i had not initialized the variable, so i did it and still got the same error, maybe is something with the spinners, im new on using spinners
here is the code:
public class CaloriesCalculator extends ActionBarActivity {
EditText etAge, etWeight, etHeight;
Button btnCalculate;
TextView tvResult;
Spinner spinnerGender, spinnerActivity;
String itemGender, itemActivity;
int Height=0;
int Weight=0;
int Age=0;;
double bmr=0.0;
double tdee=0.0;
String result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.caloriescalculator);
spinnerGender=(Spinner)findViewById(R.id.spinnerGender);
spinnerActivity=(Spinner)findViewById(R.id.spinnerActivity);
etAge=(EditText)findViewById(R.id.etAge);
etWeight=(EditText)findViewById(R.id.etWeight);
etHeight=(EditText)findViewById(R.id.etHeight);
tvResult=(TextView)findViewById(R.id.tvResult);
List<String> list = new ArrayList<String>();
list.add("Male");
list.add("Female");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, list );
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerGender.setAdapter(dataAdapter);
List<String> list2 = new ArrayList<String>();
list.add("Sedentary");
list.add("Lightly Active");
list.add("Moderalety Active");
list.add("Very Active");
list.add("Extremely Active");
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, list2 );
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerActivity.setAdapter(dataAdapter2);
btnCalculate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
spinnerGender.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
spinnerActivity.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
itemGender=String.valueOf(spinnerGender.getSelectedItem());
itemActivity=String.valueOf(spinnerActivity.getSelectedItem());
if(itemGender=="Male"){
Weight=Integer.parseInt(etWeight.getText().toString());
Height=Integer.parseInt(etHeight.getText().toString());
Age=Integer.parseInt(etAge.getText().toString());
if(itemActivity=="Sedentary"){
bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
tdee=bmr*1.2;
}
else if(itemActivity=="Lightly Active"){
bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
tdee=bmr*1.375;
}
else if(itemActivity=="Moderalety Active"){
bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
tdee=bmr*1.55;
}
else if(itemActivity=="Very Active"){
bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
tdee=bmr*1.725;
}
else if(itemActivity=="Extremely Active"){
bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
tdee=bmr*1.9;
}
}
else if(itemGender=="Female") {
Weight=Integer.parseInt(etWeight.getText().toString());
Height=Integer.parseInt(etHeight.getText().toString());
Age=Integer.parseInt(etAge.getText().toString());
if(itemActivity=="Sedentary"){
bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
tdee=bmr*1.2;
}
else if(itemActivity=="Lightly Active"){
bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
tdee=bmr*1.375;
}
else if(itemActivity=="Moderalety Active"){
bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
tdee=bmr*1.55;
}
else if(itemActivity=="Very Active"){
bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
tdee=bmr*1.725;
}
else if(itemActivity=="Extremely Active"){
bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
tdee=bmr*1.9;
}
}
result=Double.toString(tdee);
tvResult.setText(result);
}
});
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
logcat:
11-28 17:20:05.501: E/AndroidRuntime(1455): FATAL EXCEPTION: main
11-28 17:20:05.501: E/AndroidRuntime(1455): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.calculadoracalorias/com.app.calculadoracalorias.CaloriesCalculator}: java.lang.NullPointerException
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.os.Looper.loop(Looper.java:137)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-28 17:20:05.501: E/AndroidRuntime(1455): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 17:20:05.501: E/AndroidRuntime(1455): at java.lang.reflect.Method.invoke(Method.java:525)
11-28 17:20:05.501: E/AndroidRuntime(1455): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-28 17:20:05.501: E/AndroidRuntime(1455): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-28 17:20:05.501: E/AndroidRuntime(1455): at dalvik.system.NativeStart.main(Native Method)
11-28 17:20:05.501: E/AndroidRuntime(1455): Caused by: java.lang.NullPointerException
11-28 17:20:05.501: E/AndroidRuntime(1455): at com.app.calculadoracalorias.CaloriesCalculator.onCreate(CaloriesCalculator.java:67)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.Activity.performCreate(Activity.java:5133)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-28 17:20:05.501: E/AndroidRuntime(1455): ... 11 more
11-28 17:24:42.341: D/AndroidRuntime(1507): Shutting down VM
11-28 17:24:42.341: W/dalvikvm(1507): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-28 17:24:42.371: E/AndroidRuntime(1507): FATAL EXCEPTION: main
11-28 17:24:42.371: E/AndroidRuntime(1507): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.calculadoracalorias/com.app.calculadoracalorias.CaloriesCalculator}: java.lang.NullPointerException
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.os.Looper.loop(Looper.java:137)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-28 17:24:42.371: E/AndroidRuntime(1507): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 17:24:42.371: E/AndroidRuntime(1507): at java.lang.reflect.Method.invoke(Method.java:525)
11-28 17:24:42.371: E/AndroidRuntime(1507): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-28 17:24:42.371: E/AndroidRuntime(1507): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-28 17:24:42.371: E/AndroidRuntime(1507): at dalvik.system.NativeStart.main(Native Method)
11-28 17:24:42.371: E/AndroidRuntime(1507): Caused by: java.lang.NullPointerException
11-28 17:24:42.371: E/AndroidRuntime(1507): at com.app.calculadoracalorias.CaloriesCalculator.onCreate(CaloriesCalculator.java:70)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.Activity.performCreate(Activity.java:5133)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-28 17:24:42.371: E/AndroidRuntime(1507): ... 11 more
Yout btnCalculate is null, you should initialize before doing the onclickListener:
btnCalculate = (Button)findViewById(R.id.btnCalculate); //your id
Also note that you are initializing the dataAdapter2 but actually you are using dataAdapert, and list2 dont have any elements, because you declare it but u are filling always list1
I want to stop a running thread while click on the splash screen, if I don't click on screen, after the thread execution, it will launch another Activity. But getting UnSupportedException, how do I solve it?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
iImage = (ImageView) findViewById(R.id.iIcon);
splashImage = (ImageView) findViewById(R.id.splash_image);
iImage.setOnClickListener(this);
splashImage.setOnClickListener(this);
}
#Override
protected void onStart() {
super.onStart();
splashTimer = new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
startActivity(new Intent(SplashActivity.this, LoginAuthenticationActivity.class));
finish();
}
});
splashTimer.start();
}
#Override
public void onClick(View view) {
if(splashTimer.isAlive())
splashTimer.stop();
switch (view.getId()) {
case R.id.iIcon:
startActivity(new Intent(this, AboutUsActivity.class));
break;
case R.id.splash_image:
startActivity(new Intent(this, LoginAuthenticationActivity.class));
break;
default:
break;
}
finish();
}
Log:
01-27 03:27:01.189: W/dalvikvm(1080): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
01-27 03:27:01.209: E/AndroidRuntime(1080): FATAL EXCEPTION: main
01-27 03:27:01.209: E/AndroidRuntime(1080): java.lang.UnsupportedOperationException
01-27 03:27:01.209: E/AndroidRuntime(1080): at java.lang.Thread.stop(Thread.java:1076)
01-27 03:27:01.209: E/AndroidRuntime(1080): at java.lang.Thread.stop(Thread.java:1063)
01-27 03:27:01.209: E/AndroidRuntime(1080): at com.app.wooqer.SplashActivity.onClick(SplashActivity.java:48)
01-27 03:27:01.209: E/AndroidRuntime(1080): at android.view.View.performClick(View.java:3511)
01-27 03:27:01.209: E/AndroidRuntime(1080): at android.view.View$PerformClick.run(View.java:14105)
01-27 03:27:01.209: E/AndroidRuntime(1080): at android.os.Handler.handleCallback(Handler.java:605)
01-27 03:27:01.209: E/AndroidRuntime(1080): at android.os.Handler.dispatchMessage(Handler.java:92)
01-27 03:27:01.209: E/AndroidRuntime(1080): at android.os.Looper.loop(Looper.java:137)
01-27 03:27:01.209: E/AndroidRuntime(1080): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-27 03:27:01.209: E/AndroidRuntime(1080): at java.lang.reflect.Method.invokeNative(Native Method)
01-27 03:27:01.209: E/AndroidRuntime(1080): at java.lang.reflect.Method.invoke(Method.java:511)
01-27 03:27:01.209: E/AndroidRuntime(1080): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-27 03:27:01.209: E/AndroidRuntime(1080): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-27 03:27:01.209: E/AndroidRuntime(1080): at dalvik.system.NativeStart.main(Native Method)
What you're doing is very wasteful (any splash screen is wasteful, but using Threads like this is more so), but to fix your issue:
use interrrupt(); intead of stop();
As the docs say for stop()
Throws UnsupportedOperationException.
And to fix the duplicate issue, move the startActivity() inside the try so it looks like this:
public void run() {
try {
Thread.sleep(5000);
startActivity(new Intent(SplashActivity.this, LoginAuthenticationActivity.class));
} catch (InterruptedException e) {
e.printStackTrace();
}
finish();
}
That way when you call interrupt() all your Activity does is finish() and the duplicate startActivity() is not called.
To further explain:
Very first issue: stop() throws an exception by default, since it's an unsafe method which you're not supposed to use.
Then when you used interrupt(), you had startActivity() in the run method after the catch block. When you interrupted, startActivity() was called once in run() and once in onClick(). By moving startActivity() inside the try block to right after Thread.sleep(), when interrupt() interrupts the Thread, the rest of the try block isn't executed. This means that you now only have one startActivity() call instead of two. For more information, read up on exceptions.
Am I doing this right? I'm trying to implement a simple countdown timer just to get my feet of the ground with it.
my timer class is not nested in my activity, and all by itself in it's own java file, for the sake of neatness. here:
public class McatTimer extends CountDownTimer {
private TextView timer_tv;
public McatTimer(long millisInFuture, long countDownInterval, TextView textview) {
super(millisInFuture, countDownInterval);
this.timer_tv = textview;
}
#Override
public void onFinish() {
timer_tv.setText("DONE!");
}
#Override
public void onTick(long millisUntilFinished) {
timer_tv.setText("seconds remaining: " + millisUntilFinished / 1000);
}
}
and here is logcat:
10-03 08:40:36.007: E/AndroidRuntime(2582): FATAL EXCEPTION: main
10-03 08:40:36.007: E/AndroidRuntime(2582): java.lang.NullPointerException
10-03 08:40:36.007: E/AndroidRuntime(2582): at com.mangodeveloper.mcathomie.McatTimer.onTick(McatTimer.java:22)
10-03 08:40:36.007: E/AndroidRuntime(2582): at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:124)
10-03 08:40:36.007: E/AndroidRuntime(2582): at android.os.Handler.dispatchMessage(Handler.java:99)
10-03 08:40:36.007: E/AndroidRuntime(2582): at android.os.Looper.loop(Looper.java:123)
10-03 08:40:36.007: E/AndroidRuntime(2582): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-03 08:40:36.007: E/AndroidRuntime(2582): at java.lang.reflect.Method.invokeNative(Native Method)
10-03 08:40:36.007: E/AndroidRuntime(2582): at java.lang.reflect.Method.invoke(Method.java:507)
10-03 08:40:36.007: E/AndroidRuntime(2582): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-03 08:40:36.007: E/AndroidRuntime(2582): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-03 08:40:36.007: E/AndroidRuntime(2582): at dalvik.system.NativeStart.main(Native Method)
and my activity has these in their appropriate places:
private McatTimer mcatTimer;
....
mcatTimer = new McatTimer(GAME_PREFERENCES_ROUNDTIME*1000, 1000, timerTv);
mcatTimer.start();
Also is there some trick to reading these logcats? I'm sorry but i'm self-teaching.
Looks like your TextView object is null. Please ensure that you have intialized your TextView before you call the
mcatTimer = new McatTimer(GAME_PREFERENCES_ROUNDTIME*1000, 1000, timerTv);
Call this,
timerTv=(TextView)findViewById(R.id.textview);
I am creating an app in which the players enter their names into edit texts. This information is retrieved and sent to a second activity through extras. Whenever I run this, I get a Resources$NotFoundException. I am nut sure if this is from my code or from my resources folder.
Here is my code (UPDATED WITHOUT TOAST STATEMENTS)
private void getNames(int number){
Intent intent = new Intent(Setup.this, Test.class);
intent.putExtra("numberofplayers", number);
try {
if(!player1.getText().toString().contentEquals(""))
p1 = player1.getText().toString();
else
p1= "Player 1";
if(!player2.getText().toString().contentEquals(""))
p2 = player2.getText().toString();
else
p2= "Player 2";
if(!player3.getText().toString().contentEquals(""))
p3 = player3.getText().toString();
else
p3= "Player 3";
if(!player4.getText().toString().contentEquals(""))
p4 = player4.getText().toString();
else
p5= "Player 5";
if(!player6.getText().toString().contentEquals(""))
p6 = player6.getText().toString();
else
p6= "Player 6";
if(!player7.getText().toString().contentEquals(""))
p7 = player7.getText().toString();
else
p7= "Player 7";
if(!player8.getText().toString().contentEquals(""))
p8 = player8.getText().toString();
else
p8= "Player 8";
switch (number) {
case 2:
String[] a = {p1,p2};
names=a;
break;
case 3:
String[] b = {p1,p2,p3};
names=b;
break;
case 4:
String[] c = {p1,p2,p3,p4};
names=c;
break;
case 5:
String[] d= {p1,p2,p3,p4,p5};
names=d;
break;
case 6:
String[] e = {p1,p2,p3,p4,p5,p6};
names=e;
break;
case 7:
String[] f = {p1,p2,p3,p4,p5,p6,p7};
names=f;
break;
case 8:
String[] g = {p1,p2,p3,p4,p5,p6,p7,p8};
names=g;
break;
}
} catch (Exception e) {
Log.e("Setup.class Error:", e.getMessage());
}
//Toast.makeText(this, number + "", Toast.LENGTH_SHORT).show();
//for(int q =0;q<names.length;q++)
// Toast.makeText(this, names[q], Toast.LENGTH_SHORT).show();
intent.putExtra("namearray", names);
startActivity(intent);
}
Here is the code for the receiving activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle ext = getIntent().getExtras();
int i= ext.getInt("numberofplayers");
String[] names = ext.getStringArray("namearray");
Toast.makeText(this, i, Toast.LENGTH_SHORT).show();
for(int q =0;q<names.length;q++)
Toast.makeText(this, names[q], Toast.LENGTH_SHORT).show();
}
And here is the logcat error (UPDATED LOGCAT REPORT):
06-06 21:21:07.267: E/AndroidRuntime(2538): FATAL EXCEPTION: main
06-06 21:21:07.267: E/AndroidRuntime(2538): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.brightdesign.truthordare/com.brightdesign.truthordare.Test}: android.content.res.Resources$NotFoundException: String resource ID #0x2
06-06 21:21:07.267: E/AndroidRuntime(2538): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-06 21:21:07.267: E/AndroidRuntime(2538): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-06 21:21:07.267: E/AndroidRuntime(2538): at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-06 21:21:07.267: E/AndroidRuntime(2538): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-06 21:21:07.267: E/AndroidRuntime(2538): at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 21:21:07.267: E/AndroidRuntime(2538): at android.os.Looper.loop(Looper.java:137)
06-06 21:21:07.267: E/AndroidRuntime(2538): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-06 21:21:07.267: E/AndroidRuntime(2538): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 21:21:07.267: E/AndroidRuntime(2538): at java.lang.reflect.Method.invoke(Method.java:511)
06-06 21:21:07.267: E/AndroidRuntime(2538): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-06 21:21:07.267: E/AndroidRuntime(2538): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-06 21:21:07.267: E/AndroidRuntime(2538): at dalvik.system.NativeStart.main(Native Method)
06-06 21:21:07.267: E/AndroidRuntime(2538): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
06-06 21:21:07.267: E/AndroidRuntime(2538): at android.content.res.Resources.getText(Resources.java:247)
06-06 21:21:07.267: E/AndroidRuntime(2538): at android.widget.Toast.makeText(Toast.java:260)
06-06 21:21:07.267: E/AndroidRuntime(2538): at com.brightdesign.truthordare.Test.onCreate(Test.java:16)
06-06 21:21:07.267: E/AndroidRuntime(2538): at android.app.Activity.performCreate(Activity.java:4465)
06-06 21:21:07.267: E/AndroidRuntime(2538): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-06 21:21:07.267: E/AndroidRuntime(2538): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-06 21:21:07.267: E/AndroidRuntime(2538): ... 11 more
This is your first Toast call:
Toast.makeText(this, number, Toast.LENGTH_SHORT).show();
Here, the second argument is an integer (number), so it thinks that is a string ID. Try replacing number with number+"".
So whenever I click the button on the startup page, it gives me a force close error. Here's the class for the main.xml layout file:
public class ForeverAloneActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnstrt = (Button) findViewById(R.id.toq1);
btnstrt.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent frstq = new Intent(v.getContext(), QuestionOne.class);
startActivityForResult(frstq, 0);
And this is what I believe is producing the error. This class is related to the page that that when pressing the button on the startup page, you are taken to:
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView (R.layout.frstq);
Button startQ2 = (Button) findViewById(R.id.toq2);
startQ2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent toQ2 = new Intent(v.getContext(), QuestionTwo.class);
final EditText number = (EditText) findViewById(R.id.editText1);
final Toast error = Toast.makeText(QuestionOne.this, "Please insert a value", Toast.LENGTH_SHORT);
if (number.getText().toString().equals("")) {error.show();
}else{
startActivityForResult(toQ2, 0);}
That if statement is there as on the next page, there is an EditText box. I tried to make it so that if there is nothing in the EditText box, it displays a toast message saying "Please insert a value". Until an integer is put into the EditText box, then the button will not work.
If someone can help, it will be much appreciated.
Logcat:
04-07 19:33:58.199: W/dalvikvm(362): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-07 19:33:58.221: E/AndroidRuntime(362): FATAL EXCEPTION: main
04-07 19:33:58.221: E/AndroidRuntime(362): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.kenning.foreveralone/com.kenning.foreveralone.QuestionOne}; have you declared this activity in your AndroidManifest.xml?
04-07 19:33:58.221: E/AndroidRuntime(362): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.app.Activity.startActivityForResult(Activity.java:2817)
04-07 19:33:58.221: E/AndroidRuntime(362): at com.kenning.foreveralone.ForeverAloneActivity$1.onClick(ForeverAloneActivity.java:22)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.view.View.performClick(View.java:2408)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.view.View$PerformClick.run(View.java:8816)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.os.Handler.handleCallback(Handler.java:587)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.os.Handler.dispatchMessage(Handler.java:92)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.os.Looper.loop(Looper.java:123)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-07 19:33:58.221: E/AndroidRuntime(362): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 19:33:58.221: E/AndroidRuntime(362): at java.lang.reflect.Method.invoke(Method.java:521)
04-07 19:33:58.221: E/AndroidRuntime(362): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-07 19:33:58.221: E/AndroidRuntime(362): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-07 19:33:58.221: E/AndroidRuntime(362): at dalvik.system.NativeStart.main(Native Method)
are you adding QuestionTwo in manifiest file ?
check your error - have you declared this activity in your AndroidManifest.xml?
Declare your activity in manifeast file. It seems you forgetted that
<activity android:name="QuestionTwo" />
also dont forget to include every activity in your manifeast file
Hey have you added the QuestionTwo class as an activity in menifest file. If after adding the
Intent toQ2 = new Intent(YourCurrentActivity.this,QuestionTwo.class);
then you must have not added the activity in menifest.B'coz it clearly shows in the log that Activity is not found. You have to declare the activity in menifest file.
Hope you'll get run your app.