when testing my application in the emulator(I'm using Eclipse) it shows me "The application Counter(com.ian.counter) has stopped unexpectedly. Please try again." when running it. I've searched and searched for an answer but I've found none. Anyone know the problem?
Code:
package com.ian.counter;
import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
import android.widget.Toast;
public class CounterActivity extends Activity {
/** Called when the activity is first created. */
TextView textView1 = (TextView) this.findViewById(R.id.textView1);
int count = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void Count(){
count ++;
textView1.setText(Integer.toString(count));
}
}
Logcat:
12-28 16:59:20.615: D/AndroidRuntime(353): Shutting down VM
12-28 16:59:20.686: W/dalvikvm(353): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-28 16:59:20.756: E/AndroidRuntime(353): FATAL EXCEPTION: main
12-28 16:59:20.756: E/AndroidRuntime(353): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ian.counter/com.ian.counter.CounterActivity}: java.lang.NullPointerException
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.os.Handler.dispatchMessage(Handler.java:99)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.os.Looper.loop(Looper.java:123)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-28 16:59:20.756: E/AndroidRuntime(353): at java.lang.reflect.Method.invokeNative(Native Method)
12-28 16:59:20.756: E/AndroidRuntime(353): at java.lang.reflect.Method.invoke(Method.java:507)
12-28 16:59:20.756: E/AndroidRuntime(353): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-28 16:59:20.756: E/AndroidRuntime(353): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-28 16:59:20.756: E/AndroidRuntime(353): at dalvik.system.NativeStart.main(Native Method)
12-28 16:59:20.756: E/AndroidRuntime(353): Caused by: java.lang.NullPointerException
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.Activity.findViewById(Activity.java:1647)
12-28 16:59:20.756: E/AndroidRuntime(353): at com.ian.counter.CounterActivity.<init>(CounterActivity.java:10)
12-28 16:59:20.756: E/AndroidRuntime(353): at java.lang.Class.newInstanceImpl(Native Method)
12-28 16:59:20.756: E/AndroidRuntime(353): at java.lang.Class.newInstance(Class.java:1409)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
Any help is appreciated.
You're trying to set the textView1 variable before the layout has been set. You need to define it as a variable and then assign it in onCreate() after you've called setContentView(). Like this:
package com.ian.counter;
import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
import android.widget.Toast;
public class CounterActivity extends Activity {
/** Called when the activity is first created. */
TextView textView1;
int count = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView1 = (TextView) this.findViewById(R.id.textView1);
}
public void Count(){
count ++;
textView1.setText(Integer.toString(count));
}
}
Just copy and paste the above, that'll work just fine.
You should put this line
textView1 = (TextView) this.findViewById(R.id.textView1);
after
setContentView(R.layout.main);
and you just declare TextView in the begining
TextView textView1;
dont try to use findViewById(R.id.textView1); while declaraing instance variable as view does not exist at that point of time.
This is how your code should look like:
TextView textView1 = null;
int count = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textView1 = (TextView) findViewById(R.id.textView1);
}
public void Count(){
count ++;
textView1.setText(Integer.toString(count));
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hi hoping someone can help me with a error i'm getting when running my code. What want is when the user gets game over the score from the main class is taken to the gameover class to display the final score. without the intent the game over screen loads fine but when I add the intent upon gameover running the game the app crashes Here is my code where score is the int:
main class intent:
Intent myIntent = new Intent(MainActivity.this,gameover.class);
myIntent.putExtra("number", score);
MainActivity.this.startActivity(myIntent);
gameover class:
EndScore = (TextView) findViewById(R.id.show);
int numScore;
numScore = getIntent().getExtras().getInt("number");
String s = String.valueOf( numScore );
EndScore.setText(s);
I have been looking around the forums but can't see what I am doing wrong.
Log is as follows:
12-28 02:36:37.760: I/Adreno200-EGLSUB(17096): <ConfigWindowMatch:2081>: Format RGBA_8888.
12-28 02:36:37.770: D/memalloc(17096): /dev/pmem: Mapped buffer base:0x5189a000 size:4866048 offset:4251648 fd:68
12-28 02:36:37.860: D/memalloc(17096): /dev/pmem: Mapped buffer base:0x51e6e000 size:2949120 offset:2334720 fd:71
12-28 02:36:38.870: W/dalvikvm(17096): threadid=1: thread exiting with uncaught exception (group=0x40b0a9f0)
12-28 02:36:38.880: E/AndroidRuntime(17096): FATAL EXCEPTION: main
12-28 02:36:38.880: E/AndroidRuntime(17096): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.phil3992.colourguess/com.phil3992.colourguess.gameover}: java.lang.NullPointerException
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.app.ActivityThread.access$600(ActivityThread.java:123)
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.os.Handler.dispatchMessage(Handler.java:99)
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.os.Looper.loop(Looper.java:137)
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.app.ActivityThread.main(ActivityThread.java:4424)
12-28 02:36:38.880: E/AndroidRuntime(17096): at java.lang.reflect.Method.invokeNative(Native Method)
12-28 02:36:38.880: E/AndroidRuntime(17096): at java.lang.reflect.Method.invoke(Method.java:511)
12-28 02:36:38.880: E/AndroidRuntime(17096): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
12-28 02:36:38.880: E/AndroidRuntime(17096): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
12-28 02:36:38.880: E/AndroidRuntime(17096): at dalvik.system.NativeStart.main(Native Method)
12-28 02:36:38.880: E/AndroidRuntime(17096): Caused by: java.lang.NullPointerException
12-28 02:36:38.880: E/AndroidRuntime(17096): at com.phil3992.colourguess.gameover.onCreate(gameover.java:22)
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.app.Activity.performCreate(Activity.java:4470)
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
12-28 02:36:38.880: E/AndroidRuntime(17096): ... 11 more
The int is increment when the user scores a point then on game over it should send and be shown in the textview
Update whole gameover class:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class gameover extends Activity {
Button re_run;
TextView EndScore;
#Override
public void onCreate(Bundle savedInstanceState) {
re_run = (Button) findViewById(R.id.retry);
super.onCreate(savedInstanceState);
setContentView(R.layout.gameover);
EndScore = (TextView) findViewById(R.id.show);
int numScore;
numScore = getIntent().getExtras().getInt("number");
String s = String.valueOf( numScore );
EndScore.setText(s);
}
#SuppressWarnings("unused")
private void setButtonOnClickListeners(){
re_run.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
}
});
}
#Override
public void onBackPressed() {
// Do nothing
}
}
Put this line-
re_run = (Button) findViewById(R.id.retry);
Below
setContentView().
It looks as though the TextView EndScore (if you could in the future use lower case letters for variables) could not be found within the R.layout.gameover layout.
Be sure that the TextView is actually in that XML file.
Be sure that it is a TextView and not something else, like an EditText or something similar (although I think that would throw a different error).
EndScore is null according to the LogCat.
I have two framelayout in my main.xml file. I add framelayouts to the class that extends Fragment. my main class extends FragmentActivity and this is Oncreate method of it:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FragmentManager fm =getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
Fragment f=new Freg1();
Fragment f2=new Freg1();
ft.add(R.id.frame1, f);
ft.add(R.id.frame2, f2);
ft.commit();
tf=Typeface.createFromAsset(this.getAssets(),"font/Byekan.ttf" );
tv1=(TextView) findViewById(R.id.textView1);
tv1.setTypeface(tf);
Log.i(TAG,"1");
lv1=(ListView) findViewById(R.id.listView1);
lv2=(ListView) findViewById(R.id.listView2);
Log.i(TAG,"2");
List<String> stringList = new ArrayList<String>(Arrays.asList(s1));
Log.i(TAG,"3");
ListAdapter listAdapter = new CustomListAdapter(MainActivity.this , R.layout.custom_list ,stringList);
Log.i(TAG,"4");
lv1.setAdapter(listAdapter);
Log.i(TAG,"5");
lv2.setAdapter(listAdapter);
Log.i(TAG,"6");
}
when i run the codes, it crashed after LOG no4. that mean setAdapter() method do not work. how can i resolve this problem?
this is my logcat resource:
Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x40a13300)
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.taxitabriz/com.example.taxitabriz.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.taxitabriz.MainActivity.onCreate(MainActivity.java:55)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
... 11 more
thank for you that help me to resolve problem.
Your ListView lv1 is null as you can see here:
java.lang.RuntimeException: Unable to start activity ComponentInfo{...}: java.lang.NullPointerException
....
com.example.taxitabriz.MainActivity.onCreate(MainActivity.java:55)
The line 55 of MainActivity should be this call: lv1.setAdapter(listAdapter);
Make sure that your listView1 is included within the layout and initialized correctly prior to trying to set an Adapter to it.
i have made 2 classes YehActivity.java and h.java. On running the application i am getting an error ,Application has stopped unexpectedly.Here is the code
public class YehActivity extends Activity {
public static final int r=1;
Button b;
TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(YehActivity.this,he.class);
//startActivity(i);
startActivityForResult(i, r);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode==r && resultCode==RESULT_OK){
String h=data.getStringExtra("a");
tv.setText(h);
}
}
}
where to check for null.
this is the second file
public class he extends Activity{
Button b;
EditText et;
Intent i=getIntent();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.h);
b=(Button) findViewById(R.id.button12);
et=(EditText) findViewById(R.id.editText1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String value=et.getText().toString().trim();
i.putExtra("a", value);
he.this.setResult(RESULT_OK, i);
finish();
}
});
}
}
and the log file is
02-11 23:31:46.408: I/Process(302): Sending signal. PID: 302 SIG: 9
02-11 23:45:04.778: D/AndroidRuntime(357): Shutting down VM
02-11 23:45:04.778: W/dalvikvm(357): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-11 23:45:04.798: E/AndroidRuntime(357): FATAL EXCEPTION: main
02-11 23:45:04.798: E/AndroidRuntime(357): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ye/com.ye.YehActivity}: java.lang.NullPointerException
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.os.Looper.loop(Looper.java:123)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-11 23:45:04.798: E/AndroidRuntime(357): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 23:45:04.798: E/AndroidRuntime(357): at java.lang.reflect.Method.invoke(Method.java:507)
02-11 23:45:04.798: E/AndroidRuntime(357): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-11 23:45:04.798: E/AndroidRuntime(357): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-11 23:45:04.798: E/AndroidRuntime(357): at dalvik.system.NativeStart.main(Native Method)
02-11 23:45:04.798: E/AndroidRuntime(357): Caused by: java.lang.NullPointerException
02-11 23:45:04.798: E/AndroidRuntime(357): at com.ye.YehActivity.onCreate(YehActivity.java:23)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-11 23:45:04.798: E/AndroidRuntime(357): ... 11 more
02-11 23:45:11.158: I/Process(357): Sending signal. PID: 357 SIG: 9
02-11 23:45:22.708: D/AndroidRuntime(374): Shutting down VM
02-11 23:45:22.708: W/dalvikvm(374): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-11 23:45:22.728: E/AndroidRuntime(374): FATAL EXCEPTION: main
02-11 23:45:22.728: E/AndroidRuntime(374): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ye/com.ye.YehActivity}: java.lang.NullPointerException
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.os.Looper.loop(Looper.java:123)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-11 23:45:22.728: E/AndroidRuntime(374): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 23:45:22.728: E/AndroidRuntime(374): at java.lang.reflect.Method.invoke(Method.java:507)
02-11 23:45:22.728: E/AndroidRuntime(374): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-11 23:45:22.728: E/AndroidRuntime(374): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-11 23:45:22.728: E/AndroidRuntime(374): at dalvik.system.NativeStart.main(Native Method)
02-11 23:45:22.728: E/AndroidRuntime(374): Caused by: java.lang.NullPointerException
02-11 23:45:22.728: E/AndroidRuntime(374): at com.ye.YehActivity.onCreate(YehActivity.java:23)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-11 23:45:22.728: E/AndroidRuntime(374): ... 11 more
02-11 23:45:25.497: I/Process(374): Sending signal. PID: 374 SIG: 9
In your
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(YehActivity.this,he.class);
//startActivity(i);
startActivityForResult(i, r);
}
});
}
onCreate() method you attempt to use b, but you never initialize it (I'm assuming its declared as a global variable). This means that you will run into a NullPointerException when you try to call setOnClickListener().
In your code in OnCreate() you have to declare b as button and then apply listener to that.
b=(Button) findViewById(R.id.button12);
Also check your data is null or not. If it is null than handle it properly.
Then your code runs fine.
I'm trying to upload the file to a server. What is the way for uploading a file to a server through FTP?
i wrote this class:
serverconnect.java:
import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.net.SocketClient;
import org.apache.commons.net.ftp.FTPClient;
public class serverconnection
{
public FTPClient connectftp()
{
FTPClient ftp = null;
try {
ftp.connect("ftp://ftp.drivehq.com/");
ftp.login("zule", "*****");
// ftp.changeWorkingDirectory("/public");
// ftp.makeDirectory("200");
} catch (SocketException en) {
// TODO Auto-generated catch block
en.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ftp;
}
}
and this is the mainActivity ( only the relevant code):
import android.view.View;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;
import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
public class MainActivity extends Activity implements OnClickListener {
Button scan;
String contents;
String format;
TextView contentstext;
TextView formattext;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//----------------------FTP-------------
serverconnection ftpconnect =new serverconnection();
FTPClient ftp=ftpconnect.connectftp();
scan=(Button)findViewById(R.id.scanbutton);
.....
and when i install the app on my phone i get an error: "unfortanly your app must stopp..."
the new code:
public class MainActivity extends Activity implements OnClickListener {
Button scan;
String contents;
String format;
TextView contentstext;
TextView formattext;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//----------------------FTP-------------
//serverconnection ftpconnect =new serverconnection();
//SimpleFTP ftp=ftpconnect.connectftp();
SimpleFTP ftp = new SimpleFTP();
try {
ftp.connect("market.bugs3.com", 21, "u884282808", "lionetwork1");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
the new logcat:
09-16 13:43:31.131: E/AndroidRuntime(1203): FATAL EXCEPTION: main
09-16 13:43:31.131: E/AndroidRuntime(1203): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.market/com.example.market.MainActivity}: android.os.NetworkOnMainThreadException
09-16 13:43:31.131: E/AndroidRuntime(1203): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-16 13:43:31.131: E/AndroidRuntime(1203): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-16 13:43:31.131: E/AndroidRuntime(1203): at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-16 13:43:31.131: E/AndroidRuntime(1203): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-16 13:43:31.131: E/AndroidRuntime(1203): at android.os.Handler.dispatchMessage(Handler.java:99)
09-16 13:43:31.131: E/AndroidRuntime(1203): at android.os.Looper.loop(Looper.java:137)
09-16 13:43:31.131: E/AndroidRuntime(1203): at android.app.ActivityThread.main(ActivityThread.java:4424)
09-16 13:43:31.131: E/AndroidRuntime(1203): at java.lang.reflect.Method.invokeNative(Native Method)
09-16 13:43:31.131: E/AndroidRuntime(1203): at java.lang.reflect.Method.invoke(Method.java:511)
09-16 13:43:31.131: E/AndroidRuntime(1203): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-16 13:43:31.131: E/AndroidRuntime(1203): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-16 13:43:31.131: E/AndroidRuntime(1203): at dalvik.system.NativeStart.main(Native Method)
09-16 13:43:31.131: E/AndroidRuntime(1203): Caused by: android.os.NetworkOnMainThreadException
09-16 13:43:31.131: E/AndroidRuntime(1203): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
09-16 13:43:31.131: E/AndroidRuntime(1203): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
09-16 13:43:31.131: E/AndroidRuntime(1203): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
09-16 13:43:31.131: E/AndroidRuntime(1203): at java.net.InetAddress.getAllByName(InetAddress.java:220)
09-16 13:43:31.131: E/AndroidRuntime(1203): at java.net.Socket.tryAllAddresses(Socket.java:108)
09-16 13:43:31.131: E/AndroidRuntime(1203): at java.net.Socket.<init>(Socket.java:177)
09-16 13:43:31.131: E/AndroidRuntime(1203): at java.net.Socket.<init>(Socket.java:149)
09-16 13:43:31.131: E/AndroidRuntime(1203): at org.jibble.simpleftp.SimpleFTP.connect(SimpleFTP.java:68)
09-16 13:43:31.131: E/AndroidRuntime(1203): at com.example.market.MainActivity.onCreate(MainActivity.java:31)
09-16 13:43:31.131: E/AndroidRuntime(1203): at android.app.Activity.performCreate(Activity.java:4465)
09-16 13:43:31.131: E/AndroidRuntime(1203): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-16 13:43:31.131: E/AndroidRuntime(1203): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-16 13:43:31.131: E/AndroidRuntime(1203): ... 11 more
Note: AsyncTask class was deprecated in API level 30. Please use java.util.concurrent instead.
The problem is the fact that you're trying to make a network call on your main thread. Which is not allowed on Android 3.0 or higher.
You should solve this by calling the FTP server on a different thread. A good way to do this is to use AsyncTask:
private class FtpTask extends AsyncTask<Void, Void, FTPClient> {
protected FTPClient doInBackground(Void... args) {
serverconnection ftpconnect =new serverconnection();
FTPClient ftp=ftpconnect.connectftp();
return ftp;
}
protected void onPostExecute(FTPClient result) {
Log.v("FTPTask","FTP connection complete");
ftpClient = result;
//Where ftpClient is a instance variable in the main activity
}
}
Then you can run this background thread using the following code in the main thread:
new FtpTask().execute();
EDIT:
If you want to pass parameters between the different methods, you can change the
AsyncTask<Void, Void, Void> superclass initialization.
For example AsyncTask<String, Double, Integer> will make it possible to pass a String variable to the doInBackground method, keep track of progress using a double and use a integer as the result type(the result type is the return type of doInBackground, which will be sent to onPostExecute as a parameter).
I have this code for an expandable list, I want to have a checkbox in the childgroups of the list view, and check if one of the checkboxes is checked.
The problem is that when I check if the checkbox is checked I get a NULL Pointer Exception.
Can you please tell me whats wrong?
here's my code - I've edited the code to inflate a view of the child_row.xml that holds that checkbox but I still get that null pointer, what am I doing wrong?!?!
package send.Shift;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
public class Shifts extends ExpandableListActivity implements
OnCheckedChangeListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.list);
SimpleExpandableListAdapter expListAdapter = new SimpleExpandableListAdapter(
this, createGroupList(), R.layout.group_row,
new String[] { "Group Item" }, new int[] { R.id.row_name },
createChildList(), R.layout.child_row,
new String[] { "Sub Item" }, new int[] { R.id.grp_child });
getExpandableListView().setGroupIndicator(
getResources().getDrawable(R.drawable.expander_group));
ExpandableListView EX = (ExpandableListView) findViewById(android.R.id.list);
EX.setAdapter(expListAdapter);
final CheckBox childBox = (CheckBox) findViewById(R.id.childBOX);
final TextView choosenGroup = (TextView) findViewById(R.id.choosen);
LayoutInflater inflater = LayoutInflater.from(Shifts.this);
View view2 = inflater.inflate(R.layout.child_row, null);
childBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(childBox.isChecked() == true){
choosenGroup.setText("Shift Set");
}
}
});
}
Here is some of the Logcat log:
12-23 07:38:00.644: W/dalvikvm(880): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-23 07:38:00.654: E/AndroidRuntime(880): FATAL EXCEPTION: main
12-23 07:38:00.654: E/AndroidRuntime(880): java.lang.RuntimeException: Unable to start activity ComponentInfo{send.Shift/send.Shift.Shifts}: java.lang.NullPointerException
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.os.Handler.dispatchMessage(Handler.java:99)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.os.Looper.loop(Looper.java:123)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-23 07:38:00.654: E/AndroidRuntime(880): at java.lang.reflect.Method.invokeNative(Native Method)
12-23 07:38:00.654: E/AndroidRuntime(880): at java.lang.reflect.Method.invoke(Method.java:507)
12-23 07:38:00.654: E/AndroidRuntime(880): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-23 07:38:00.654: E/AndroidRuntime(880): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-23 07:38:00.654: E/AndroidRuntime(880): at dalvik.system.NativeStart.main(Native Method)
12-23 07:38:00.654: E/AndroidRuntime(880): Caused by: java.lang.NullPointerException
12-23 07:38:00.654: E/AndroidRuntime(880): at send.Shift.Shifts.onCreate(Shifts.java:41)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-23 07:38:00.654: E/AndroidRuntime(880): ... 11 more
If you get a nullpointer on a certain line, something on that line is null. If it is the if(childBox.isChecked() line, probably childBox is null. Hard to say without the stack, but most probable cause is the line where you retrieve that checkbox.
final CheckBox childBox = (CheckBox) findViewById(R.id.childBOX);
Might be returning null, and this could be for several reasons. It could be your id is childBox instead of childBOX. Or that it is not in R.layout.list.
The best thing you can do is start debugging. What line is the error. Find the object that is null. Find out why it is null and if that is expected or not.
Instead of checking the childBox.isChecked() you can use the isChecked value. So your code would look like this:
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked == true){
choosenGroup.setText("Shift Set");
}
}
Check your layout list.xml I think this layout may is missing android:id="#+id/childBOX" for Check Box or android:id="#+id/choosen" for TextView
Check the folowin sample
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/childBOX"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/choosen"/>