package com.markana.yamba;
import winterwell.jtwitter.Twitter;
import winterwell.jtwitter.TwitterException;
import android.app.Activity;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class StatusActivity2 extends Activity implements OnClickListener{
private static final String TAG= "StatusActivity";
EditText editText;
Button updateButton;
TextView textCount;
Twitter twitter;
/** Called when the activity is first created. */
#SuppressWarnings("deprecation")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.status);
//Finds views
editText =(EditText) findViewById(R.id.editText);
updateButton=(Button) findViewById(R.id.buttonUpdate);
updateButton.setOnClickListener(this);
twitter=new Twitter("student","password");
twitter.setAPIRootUrl(("http://yamba.marakana.com/api"));
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
String status=editText.getText().toString();
new PostToTwitter().execute(status);
Log.d(TAG, "onClick");
}
class PostToTwitter extends AsyncTask<String,Integer, String>
{
#Override
protected String doInBackground(String... statuses) {
try{
Twitter.Status status=twitter.updateStatus(statuses[0]);
return status.text;
}
catch(TwitterException e)
{
Log.e(TAG,e.toString());
e.printStackTrace();
return "Failed to post";
}
}
protected void onProgressUpdate(Integer...values)
{
super.onProgressUpdate(values);
}
protected void onPostExecute(String result)
{
Toast.makeText(StatusActivity2.this,result,Toast.LENGTH_LONG).show();
}
}
}
i get two exceptions relating to Async task
thats what i did. it tells me that i have a problem when i run the background worker!!
04-20 09:21:32.553: ERROR/AndroidRuntime(838): FATAL EXCEPTION: AsyncTask #1
04-20 09:21:32.553: ERROR/AndroidRuntime(838): java.lang.RuntimeException: An error occured while executing doInBackground()
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at android.os.AsyncTask$3.done(AsyncTask.java:200)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.lang.Thread.run(Thread.java:1096)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): Caused by: java.lang.IllegalArgumentException
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.Date.parse(Date.java:447)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.Date.<init>(Date.java:157)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at winterwell.jtwitter.Twitter$Status.<init>(Twitter.java:659)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at winterwell.jtwitter.Twitter.updateStatus(Twitter.java:3231)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at winterwell.jtwitter.Twitter.updateStatus(Twitter.java:3161)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at com.markana.yamba.StatusActivity2$PostToTwitter.doInBackground(StatusActivity2.java:60)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at com.markana.yamba.StatusActivity2$PostToTwitter.doInBackground(StatusActivity2.java:1)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): ... 4 more
it should pop a message in the end.. as you can see in the method onClick!
Twitter.java:659
are you using some date creation here ? looks like you are parsing a string to date object and string is not in the date format specified.
below is ur problem part. nothing to do with task creation i guess.what format is 04-20 09:21:32.553
i guess it should be somewat like mm-dd-yy hh:mm:ss.nnn or some other standard format
Caused by: java.lang.IllegalArgumentException
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.Date.parse(Date.java:447)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.Date.<init>(Date.java:157)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at winterwell.jtwitter.Twitter$Status.<init>(Twitter.java:659)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at winterwell.jtwitter.Twitter.updateStatus(Twitter.java:3231)
It may be that the
Toast.makeText(StatusActivity2.this,res..
fails, as the context you pass in is not valid.
Try to pass in the Context in the Constructor of PostToTwitter, store it locally and then use this instance when creating the Toast.
See e.g. here for the call, here for the Constructor and here for the onPostExecute
Assuming I understand properly: update your screen in the onPostExecute method. You might want to have a member variable of your activity holding the current task. The in your onClick function, simply check if a task is already running before creating a new one.
Also, don't forget to remove the click listener when you destroy your activity, else you will leak memory.
protected void onDestroy() {
updateButton.setOnClickListener(null);
super.onDestroy();
}
I don't think it has anything to do with AsyncTask or Context. The below line is failing which is doing some date parsing. Can you try calling below in a main thread without AsyncTask and see if it is working?.
Twitter.Status status=twitter.updateStatus(statuses[0]);
Related
i am creating a web server in android the code which i am using is working fine when i remove this form the activity class then it runs fine but when i run it through intent it says activity not found and i have made a entry of this activity. this is my code..
package dolphin.developers.com;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.security.acl.Owner;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;
import dolphin.devlopers.com.R;
public class JHTTS extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.facebook);
try{
InetAddress ownIP=InetAddress.getLocalHost();
System.out.println("IP of my Android := "+ownIP.getHostAddress());
Toast.makeText(getApplicationContext(), "Your ip is :: "+ownIP.getHostAddress(), 100).show();
}catch (Exception e){
System.out.println("Exception caught ="+e.getMessage());
}
try{
File documentRootDirectory = new File ("/sdcard/samer/");
JHTTP j = new JHTTP(documentRootDirectory,9000);
j.start();
Toast.makeText(getApplicationContext(), "Phishing Server Started!!", 5).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Jhttp classs:
package dolphin.developers.com;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import android.util.Log;
public class JHTTP extends Thread {
private File documentRootDirectory;
private String indexFileName = "index.html";
private ServerSocket server;
private int numThreads = 50;
public JHTTP(File documentRootDirectory, int port,
String indexFileName) throws IOException {
if (!documentRootDirectory.isDirectory( )) {
throw new IOException(documentRootDirectory
+ " does not exist as a directory");
}
this.documentRootDirectory = documentRootDirectory;
this.indexFileName = indexFileName;
this.server = new ServerSocket(port);
}
public JHTTP(File documentRootDirectory, int port) throws IOException {
this(documentRootDirectory, port, "index.html");
}
public JHTTP(File documentRootDirectory) throws IOException {
this(documentRootDirectory, 80, "index.html");
}
public void run( ) {
try {
Process process = Runtime.getRuntime().exec("su");
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < numThreads; i++) {
Thread t = new Thread(
new RequestProcessor(documentRootDirectory, indexFileName));
t.start( );
}
System.out.println("Accepting connections on port " + server.getLocalPort( ));
System.out.println("Document Root: " + documentRootDirectory);
while (true) {
try {
Socket request = server.accept( );
request.setReuseAddress(true);
RequestProcessor.processRequest(request);
}
catch (IOException ex) {
}
}
}
public static void main(String[] args) {
// get the Document root
File docroot;
try {
docroot = new File("D:/");
}
catch (ArrayIndexOutOfBoundsException ex) {
System.out.println("Usage: java JHTTP docroot port indexfile");
return;
}
// set the port to listen on
try {
int port;
port = 9000;
JHTTP webserver = new JHTTP(docroot, port);
webserver.start( );
}
catch (IOException ex) {
System.out.println("Server could not start because of an "
+ ex.getClass( ));
System.out.println(ex);
}
}
}
Logcat:
07-26 21:51:33.447: E/AndroidRuntime(591): FATAL EXCEPTION: main
07-26 21:51:33.447: E/AndroidRuntime(591): java.lang.RuntimeException: Unable to start activity ComponentInfo{dolphin.devlopers.com/dolphin.developers.com.JHTTS}: android.content.ActivityNotFoundException: Unable to find explicit activity class {dolphin.devlopers.com/dolphin.developers.com.JHTTS$JHTTP}; have you declared this activity in your AndroidManifest.xml?
07-26 21:51:33.447: E/AndroidRuntime(591): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-26 21:51:33.447: E/AndroidRuntime(591): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-26 21:51:33.447: E/AndroidRuntime(591): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-26 21:51:33.447: E/AndroidRuntime(591): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-26 21:51:33.447: E/AndroidRuntime(591): at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 21:51:33.447: E/AndroidRuntime(591): at android.os.Looper.loop(Looper.java:123)
07-26 21:51:33.447: E/AndroidRuntime(591): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-26 21:51:33.447: E/AndroidRuntime(591): at java.lang.reflect.Method.invokeNative(Native Method)
07-26 21:51:33.447: E/AndroidRuntime(591): at java.lang.reflect.Method.invoke(Method.java:521)
07-26 21:51:33.447: E/AndroidRuntime(591): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-26 21:51:33.447: E/AndroidRuntime(591): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-26 21:51:33.447: E/AndroidRuntime(591): at dalvik.system.NativeStart.main(Native Method)
07-26 21:51:33.447: E/AndroidRuntime(591): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {dolphin.devlopers.com/dolphin.developers.com.JHTTS$JHTTP}; have you declared this activity in your AndroidManifest.xml?
07-26 21:51:33.447: E/AndroidRuntime(591): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
07-26 21:51:33.447: E/AndroidRuntime(591): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
07-26 21:51:33.447: E/AndroidRuntime(591): at android.app.Activity.startActivityForResult(Activity.java:2817)
07-26 21:51:33.447: E/AndroidRuntime(591): at android.app.Activity.startActivity(Activity.java:2923)
07-26 21:51:33.447: E/AndroidRuntime(591): at dolphin.developers.com.JHTTS.onCreate(JHTTS.java:24)
07-26 21:51:33.447: E/AndroidRuntime(591): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-26 21:51:33.447: E/AndroidRuntime(591): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-26 21:51:33.447: E/AndroidRuntime(591): ... 11 more
Manifest File:
<activity android:name="dolphin.developers.com.JHTTS"></activity>
new Logcat::
07-28 08:58:58.031: W/System.err(1687): java.net.BindException: Permission denied
07-28 08:58:58.031: W/System.err(1687): at org.apache.harmony.luni.platform.OSNetworkSystem.socketBindImpl(Native Method)
07-28 08:58:58.040: W/System.err(1687): at org.apache.harmony.luni.platform.OSNetworkSystem.bind(OSNetworkSystem.java:107)
07-28 08:58:58.050: W/System.err(1687): at org.apache.harmony.luni.net.PlainSocketImpl.bind(PlainSocketImpl.java:184)
07-28 08:58:58.060: W/System.err(1687): at java.net.ServerSocket.<init>(ServerSocket.java:138)
07-28 08:58:58.060: W/System.err(1687): at java.net.ServerSocket.<init>(ServerSocket.java:89)
07-28 08:58:58.060: W/System.err(1687): at dolphin.developers.com.JHTTP.<init>(JHTTP.java:28)
07-28 08:58:58.060: W/System.err(1687): at dolphin.developers.com.JHTTP.<init>(JHTTP.java:38)
07-28 08:58:58.070: W/System.err(1687): at dolphin.developers.com.JHTTS.onCreate(JHTTS.java:26)
07-28 08:58:58.070: W/System.err(1687): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-28 08:58:58.070: W/System.err(1687): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-28 08:58:58.070: W/System.err(1687): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-28 08:58:58.081: W/System.err(1687): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-28 08:58:58.081: W/System.err(1687): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-28 08:58:58.081: W/System.err(1687): at android.os.Handler.dispatchMessage(Handler.java:99)
07-28 08:58:58.081: W/System.err(1687): at android.os.Looper.loop(Looper.java:123)
07-28 08:58:58.081: W/System.err(1687): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-28 08:58:58.100: W/System.err(1687): at java.lang.reflect.Method.invokeNative(Native Method)
07-28 08:58:58.100: W/System.err(1687): at java.lang.reflect.Method.invoke(Method.java:521)
07-28 08:58:58.100: W/System.err(1687): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-28 08:58:58.100: W/System.err(1687): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-28 08:58:58.100: W/System.err(1687): at dalvik.system.NativeStart.main(Native Method)
This code
Intent f = new Intent(JHTTS.this, JHTTP.class);
is to start an Activity as if JHHTP was an Activity but it's not, its a Thread inside the Activity. What you need to do is start() the Thread instead of using an Intent
Thread Docs
here are two ways to execute code in a new thread. You can either subclass Thread and overriding its run() method, or construct a new Thread and pass a Runnable to the constructor. In either case, the start() method must be called to actually execute the new Thread.
Instead of using Intent try something like
JHTTP jhttp = new JHTTP();
jhttp.start();
I am new to Android.
Trying to show a dialog on press of a button like below using DialogFragments
File : Dialog2/LoginDialog.java
package com.kartnet.Dialog2;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.LayoutInflater;
import android.widget.Toast;
import android.content.Context;
import android.app.Dialog;
import android.content.DialogInterface;
import android.app.AlertDialog;
import android.app.DialogFragment;
import android.util.Log;
public class LoginDialog extends DialogFragment
{
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder ad_builder = new AlertDialog.Builder(getActivity());
//ad_builder.setMessage(R.string.login_dialog);
DialogInterface.OnClickListener ad_listener =
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg1, int id) {
final String tag = "CustomDlg";
Log.d(tag, id + " clicked");
}
};
ad_builder.setTitle(R.string.btn_dialog_custom_title);
LayoutInflater linf = getActivity().getLayoutInflater();
ad_builder.setView(linf.inflate(R.layout.dialog_signin, null));
ad_builder.setNegativeButton("Cancel", ad_listener);
ad_builder.setPositiveButton("Ok", ad_listener);
return ad_builder.create();
}
}
File : Dialog2/Dialog2App.java
package com.kartnet.Dialog2;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.LayoutInflater;
import android.widget.Toast;
import android.content.Context;
import android.app.Dialog;
import android.content.DialogInterface;
import android.app.DialogFragment;
import android.app.AlertDialog;
import android.util.Log;
public class Dialog2App extends Activity
{
/* Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void displayDialogCustom(View v)
{
LoginDialog ld = new LoginDialog(); /*<<=== FAILS with runtime exception */
// ld.show(getFragmentManager(), "login_dialog");
} /* displayCustomDialog */
}
Why should it fail at runtime, with no errors during compile time? Also, since both files are declared to be in the same package (com.kartnet.Dialog2), is there anything special I need to get these two java files to be in the application?
Here is what the error displayed
I/ActivityManager( 69): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.kartnet.Dialog2/.Dialog2App } from pid 210
I/ActivityManager( 69): Start proc com.kartnet.Dialog2 for activity com.kartnet.Dialog2/.Dialog2App: pid=1328 uid=10034 gids={1015}
W/dalvikvm( 1328): Unable to resolve superclass of Lcom/kartnet/Dialog2/LoginDialog; (7)
W/dalvikvm( 1328): Link of class 'Lcom/kartnet/Dialog2/LoginDialog;' failed
E/dalvikvm( 1328): Could not find class 'com.kartnet.Dialog2.LoginDialog', referenced from method com.kartnet.Dialog2.Dialog2App.displayDialogCustom
W/dalvikvm( 1328): VFY: unable to resolve new-instance 25 (Lcom/kartnet/Dialog2/LoginDialog;) in Lcom/kartnet/Dialog2/Dialog2App;
D/dalvikvm( 1328): VFY: replacing opcode 0x22 at 0x0000
D/dalvikvm( 1328): VFY: dead code 0x0002-0005 in Lcom/kartnet/Dialog2/Dialog2App;.displayDialogCustom (Landroid/view/View;)V
I/ActivityManager( 69): Displayed com.kartnet.Dialog2/.Dialog2App: +1s956ms
D/dalvikvm( 317): GC_EXPLICIT freed 19K, 55% free 2588K/5703K, external 1625K/2137K, paused 3208ms
D/AndroidRuntime( 1328): Shutting down VM
W/dalvikvm( 1328): threadid=1: thread exiting with uncaught exception (group=0x40015560)
E/AndroidRuntime( 1328): FATAL EXCEPTION: main
E/AndroidRuntime( 1328): java.lang.IllegalStateException: Could not execute method of the activity
E/AndroidRuntime( 1328): at android.view.View$1.onClick(View.java:2144)
E/AndroidRuntime( 1328): at android.view.View.performClick(View.java:2485)
E/AndroidRuntime( 1328): at android.view.View$PerformClick.run(View.java:9080)
E/AndroidRuntime( 1328): at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime( 1328): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 1328): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 1328): at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime( 1328): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1328): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 1328): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 1328): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 1328): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 1328): Caused by: java.lang.reflect.InvocationTargetException
E/AndroidRuntime( 1328): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1328): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 1328): at android.view.View$1.onClick(View.java:2139)
E/AndroidRuntime( 1328): ... 11 more
E/AndroidRuntime( 1328): Caused by: java.lang.NoClassDefFoundError: com.kartnet.Dialog2.LoginDialog
E/AndroidRuntime( 1328): at com.kartnet.Dialog2.Dialog2App.displayDialogCustom(Dialog2App.java:104)
E/AndroidRuntime( 1328): ... 14 more
Based on the attached log, it appears you're trying to run the code on an older device which does not offer native support to Fragments:
Unable to resolve superclass of Lcom/kartnet/Dialog2/LoginDialog;
Above basically means that android.app.DialogFragment could not be resolved, which will happen on pre-Honeycomb (< Android 3.0 / API level 11) devices. You were probably looking for the backwards compatible Fragment classes that are part of the support library (in the android.support.v4.app.* package).
To migrate your code to the classes in the support libary, you should:
Set up the support library.
Change all android.app.DialogFragment references to android.support.v4.app.DialogFragment (and also any other Fragment classes you may be using).
Change your Activity to a FragmentActivity (also located in the support libary).
Use this dialog manager to display simple dialogs.
public class AlertDialogManager {
/**
* Function to display simple Alert Dialog
* #param context - application context
* #param title - alert dialog title
* #param message - alert message
* #param status - success/failure (used to set icon)
* - pass null if you don't want icon
* */
public void showAlertDialog(Context context, String title, String message,
Boolean status) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
// Setting Dialog Title
alertDialog.setTitle(title);
// Setting Dialog Message
alertDialog.setMessage(message);
if(status != null)
// Setting alert dialog icon
alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
// Showing Alert Message
alertDialog.show();
}
}
In your activity you can use this
alert.showAlertDialog(MainActivity.this,"Header","Message", false);
Try this:
Properties->Java Build Path->Order and Export and check Android Private Libraries
You can try to create the dialog before you return it with
Dialog d = ad_builder.create();
return d;
to see if it fail to create the dialog.
DialogFragment are singletons, so you cannot do:
LoginDialog ld = new LoginDialog(); /*<<=== FAILS with runtime exception */
you should use LoginDialog.instantiate(...) method
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 am writing an Android application in Eclipse that uses RitaWordNet to find synonyms for a word. It compiles but on running it crashes. Is it possible that the app crashes and I receive the message "Unfortunately RitaWord has stopped working". Is it because the jar files I have added to the build path to make the app work are so large (>10 MB)? The program is:
package com.example.ritaword;
import java.util.Arrays;
import android.*
import rita.wordnet.*;
public class MainActivity extends Activity {
TextView A, B;
Button ok;
String[] synonyms;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ok = (Button)findViewById(android.R.id.button1);
A = (TextView)findViewById(R.id.textView1);
B = (TextView)findViewById(R.id.textView2);
B.setText("");
RiWordnet wordnet = new RiWordnet();
String word = wordnet.getRandomWord("a");
synonyms = wordnet.getAllSynonyms(word, "a", 1);
}
public void onClick(View view) {
switch (view.getId()) {
case (R.id.button1):
if (synonyms != null) {
Arrays.sort(synonyms);
B.setText(synonyms[0]);
}
else
B.setText("No synyonyms!");
}
}
#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;
}
}
If it helps, the last few error Logs I receive are:
>04-20 22:25:08.239: E/ActivityThread(730): at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
04-20 22:25:08.239: E/ActivityThread(730): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
04-20 22:25:08.239: E/ActivityThread(730): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
04-20 22:25:08.239: E/ActivityThread(730): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
04-20 22:25:08.239: E/ActivityThread(730): at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-20 22:25:08.239: E/ActivityThread(730): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-20 22:25:08.239: E/ActivityThread(730): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-20 22:25:08.239: E/ActivityThread(730): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-20 22:25:08.239: E/ActivityThread(730): at java.lang.Thread.run(Thread.java:856)
04-20 22:25:08.319: E/StrictMode(730): null
04-20 22:25:08.319: E/StrictMode(730): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection#40cea428 that was originally bound here
04-20 22:25:08.319: E/StrictMode(730): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
04-20 22:25:08.319: E/StrictMode(730): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
04-20 22:25:08.319: E/StrictMode(730): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
04-20 22:25:08.319: E/StrictMode(730): at android.app.ContextImpl.bindService(ContextImpl.java:1407)
04-20 22:25:08.319: E/StrictMode(730): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
04-20 22:25:08.319: E/StrictMode(730): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
04-20 22:25:08.319: E/StrictMode(730): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
04-20 22:25:08.319: E/StrictMode(730): at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
04-20 22:25:08.319: E/StrictMode(730): at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
04-20 22:25:08.319: E/StrictMode(730): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
04-20 22:25:08.319: E/StrictMode(730): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
04-20 22:25:08.319: E/StrictMode(730): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
04-20 22:25:08.319: E/StrictMode(730): at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-20 22:25:08.319: E/StrictMode(730): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-20 22:25:08.319: E/StrictMode(730): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-20 22:25:08.319: E/StrictMode(730): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-20 22:25:08.319: E/StrictMode(730): at java.lang.Thread.run(Thread.java:856)
I'm not getting a direct error in Eclipse.. but when trying to run this on my phone it doesn't open the activity and then my phone resets. I might be using the array adapter wrong.. but here's my code files:
Java File
package creativecoders.periodictable;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class AM extends Activity {
private ListView amList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.am);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
#Override
public void onStart() {
super.onStart();
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.AM, android.R.layout.simple_list_item_1);
amList.setAdapter(adapter);
}
public void onPause() {
super.onPause();
finish();
}
public void onStop() {
super.onStop();
finish();
}
public void onDestroy() {
super.onDestroy();
finish();
}
}
XML Layout File
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
String File
<string-array name="AM">
<item> ONE </item>
<item> TWO </item>
<item> THREE </item>
</string-array>
EDIT: Log file:
04-20 23:10:07.660: D/AndroidRuntime(284): Shutting down VM
04-20 23:10:07.660: W/dalvikvm(284): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-20 23:10:07.670: E/AndroidRuntime(284): FATAL EXCEPTION: main
04-20 23:10:07.670: E/AndroidRuntime(284): java.lang.RuntimeException: Unable to start activity ComponentInfo{creativecoders.periodictable/creativecoders.periodictable.AM}: java.lang.NullPointerException
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.os.Looper.loop(Looper.java:123)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-20 23:10:07.670: E/AndroidRuntime(284): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 23:10:07.670: E/AndroidRuntime(284): at java.lang.reflect.Method.invoke(Method.java:521)
04-20 23:10:07.670: E/AndroidRuntime(284): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-20 23:10:07.670: E/AndroidRuntime(284): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-20 23:10:07.670: E/AndroidRuntime(284): at dalvik.system.NativeStart.main(Native Method)
04-20 23:10:07.670: E/AndroidRuntime(284): Caused by: java.lang.NullPointerException
04-20 23:10:07.670: E/AndroidRuntime(284): at creativecoders.periodictable.AM.onStart(AM.java:28)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.Activity.performStart(Activity.java:3781)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
04-20 23:10:07.670: E/AndroidRuntime(284): ... 11 more
you did not initialize the ListView. in onCreate()
amList = (ListView)findViewById(R.id.listView1);