Running an android application [closed] - android-emulator

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 8 years ago.
Improve this question
I am having trouble when I am trying to run my simple android applications On my system
I tested for some other applications which were not created on my system and those worked well but when I edit those on my system then also these applications wont work. I am getting many errors
09-26 07:23:55.458: E/AndroidRuntime(1341): ... 11 more
at com.calculation.MainActivity.<init>(MainActivity.java:19)
at java.lang.Class.newInstanceImpl(Native Method)
: at java.lang.Class.newInstance(Class.java:1130)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
Why am I getting this
Please Help me
package com.calculation;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.AlertDialog;
import android.content.DialogInterface;
public class MainActivity extends Activity {
EditText value1,value2;
Button add;
TextView display;
int num1=0, num2=0, total=0;
final AlertDialog alertdialog= new AlertDialog.Builder(MainActivity.this).create();
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
value1=(EditText)findViewById(R.id.input1);
value2=(EditText)findViewById(R.id.input2);
add=(Button)findViewById(R.id.btnadd);
display=(TextView)findViewById(R.id.result);
if(value1==null)
{
alertdialog.setTitle("AlertDialog");
alertdialog.setMessage("Please Enter a Valid Input");
alertdialog.setIcon(R.drawable.ic_launcher);
alertdialog.setButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "You Clicked OK" , Toast.LENGTH_LONG).show();
}
});
}
else if(value2==null)
{
alertdialog.setTitle("AlertDialog");
alertdialog.setMessage("Please Enter a Valid Input");
alertdialog.setIcon(R.drawable.ic_launcher);
alertdialog.setButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "You Clicked OK" , Toast.LENGTH_LONG).show();
}
});
}
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
num1=Integer.parseInt(value1.getText().toString());
num2=Integer.parseInt(value2.getText().toString());
total=num1+num2;
display.setText("ADDITION IS"+total);
}
});
}
}

The Problem was of api levels the apps which were tested were created many years back in different api level and environment so the alert dialog error was coming
alert dialog can be in this way
private void showinvalidemail() {
// TODO Auto-generated method stub
AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(this);
dialogbuilder.setTitle("Invalid Email");
dialogbuilder.setMessage("Please Enter Valid Email");
dialogbuilder.setIcon(R.drawable.mexit);
dialogbuilder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
// TODOAuto-generated method stub
Toast.makeText(getApplicationContext(),
"You Clicked OK", Toast.LENGTH_LONG).show();
}
});
AlertDialog alertdialog = dialogbuilder.create();
alertdialog.show();
}

Related

Android Check for Wireless connection upon Returning to Activity from Settings

My Android application is mainly a Webview (labeles WebViewActivity) that navigates a mobile website. I have a connection detector that opens another activity on every URL load if there is no wireless connection. From this next activity there is a button that opens up the Wireless Settings. Upon pressing the back button I would like for the second activity (labeled MainActivity) to refresh itself and continue back to whichever page my Webview had loaded. What do I need to change in the MainActivity.java to execute that?
My MainActivity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
// flag for Internet connection status
Boolean isInternetPresent = false;
// Connection detector class
ConnectionDetector cd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
}
#Override
protected void onRestart(){
if(checkConnection()){
Intent intent= new Intent(this, WebViewActivity.class);
startActivity(intent);
}
}
#Override
// Detect when the back button is pressed
public void onBackPressed() {
super.onBackPressed();
}
public void openSettings(View view){
Intent intent= new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
startActivity(intent);
}
public boolean checkConnection(){
// creating connection detector class instance
cd = new ConnectionDetector(getApplicationContext());
//Get Internet Status
isInternetPresent = cd.isConnectingToInternet();
if(!isInternetPresent)
return false;
return true;
}
}
My WebviewActivity:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewActivity extends Activity {
private WebView mWebView;
// flag for Internet connection status
Boolean isInternetPresent = false;
// Connection detector class
ConnectionDetector cd;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// TODO Auto-generated method stub
if (checkConnection()) {
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
mWebView.loadUrl("http://my.fellowshipnwa.org/?publicapp");
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new myWebClient());
} else {
openSplash();
}
}
#Override
// Detect when the back button is pressed
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
// Let the system handle the back button
new AlertDialog.Builder(this)
.setTitle("Exit myFellowship App?")
.setMessage("Are you sure you want to exit?")
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
WebViewActivity.super.onBackPressed();
}
}).create().show();
}
}
public class myWebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(!checkConnection()){
openSplash();
return true;
}else{
if( url.startsWith("tel:")){
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
return true;
}
else if( url.startsWith("mailto:")){
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(url));
startActivity(intent);
return true;
}
}
return false;
}
}
public void openSplash(){
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
public boolean checkConnection(){
// creating connection detector class instance
cd = new ConnectionDetector(getApplicationContext());
//Get Internet Status
isInternetPresent = cd.isConnectingToInternet();
if(!isInternetPresent)
return false;
return true;
}
}
Any help is Greatly Appreciated! Even if it means telling me I goofed up on creating this question.
Thank you friends.
I played with it for a while and used a few other examples to piece together something that is working. One small hitch is if the user turns on a connection, but it is not quite connected when they return to the app it will stay on the ConnectorActivity. So I intercept the back button to check for a connection. If present, it will finish the current activity. Then, upon returning to the webview it will reload.
ConnectorActivity.java:
#Override
protected void onRestart(){
super.onRestart();
if(checkConnection()){
finish();
}
}
#Override
// Detect when the back button is pressed
public void onBackPressed() {
if(checkConnection()){
super.onBackPressed();
}
}
Piece from WebViewActivity.java:
#Override
protected void onRestart(){
super.onRestart();
mWebView.reload();
}

When click button in menu it opens xml file

i am trying to make a menu in my app. when i click the menu button i made called aboutUs it is supposed to open an XML file that explains what this app is about. Except when i run the app and click on the menu button the app just force closes. heres my mainactivity.java
package com.JordanZimmittiDevelopers.BlazeCustomerService1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.JordanZimmittiDevelopers.BlazeCustomerService.R;
public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mail = (Button)findViewById(R.id.button1);
mail.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.button1:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
String[] recipients = new String[]{"jordanzimmitti#gmail.com", "",};
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "I Have A Question Or Probelm:");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "My question or problem is:");
emailIntent.setType("text/plain");
startActivity(Intent.createChooser(emailIntent, "Click Your Defult E-mail To Send Your Message:"));
finish();
break;
}
}
Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.aboutUs:
Intent i = new Intent("com.JordanZimmittiDevelopers.BlazeCustomerService.AboutThisApp");
startActivity(i);
}
return false;
}
}
Make an activity that explain the application then override onCreateOptionsMenu() like this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add("Help").setIcon(R.drawable.HelpButton).setIntent(new Intent(this,HelpActivity.class));
return super.onCreateOptionsMenu(menu);
}

Admob crashes my app Android (Eclipse)

I have been following a tutorial online on how to build android apps, everything went fine until today, I have been trying admob, first I followed the tutorial, my app crashed java and xml way of adding admob. Then I followed the instructions on admob.com same thing. Does anyone have an idea how to fix that? I am brand new to OOP (Java/Android).
Here is the error log:
03-01 20:02:06.910: E/AndroidRuntime(4670): <-- all lines started with that.
FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: com.google.ads.AdView
at com.example.myfirstapp.Data.onCreate(Data.java:31)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
at android.app.ActivityThread.access$1500(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3835)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Data.java:
package com.example.myfirstapp;
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.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
public class Data extends Activity implements OnClickListener {
private AdView ad;
Button start, startFor;
EditText sendET;
TextView gotAnswer;
RelativeLayout rl;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.get);
initialize();
ad = new AdView(this, AdSize.BANNER, "a151303d6f29c9c");
rl = (RelativeLayout) findViewById(R.id.relLayout);
rl.addView(ad);
ad.loadAd(new AdRequest());
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
if (ad != null) {
ad.destroy();
}
super.onDestroy();
}
private void initialize() {
// TODO Auto-generated method stub
start = (Button) findViewById(R.id.bSA);
startFor = (Button) findViewById(R.id.bSAFR);
sendET = (EditText) findViewById(R.id.etSend);
gotAnswer = (TextView) findViewById(R.id.tvGot);
start.setOnClickListener(this);
startFor.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.bSA:
String bread = sendET.getText().toString();
Bundle basket = new Bundle();
basket.putString("key", bread);
Intent a = new Intent(Data.this, OpenedClass.class);
a.putExtras(basket);
startActivity(a);
break;
case R.id.bSAFR:
Intent i = new Intent(Data.this, OpenedClass.class);
startActivityForResult(i, 0);
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle basket = data.getExtras();
String s = basket.getString("answer");
gotAnswer.setText(s);
}
}
}
My guess is that your Android project hasn't been configured correctly as the AdView class seems to be present at compile time but not at run time which indicates that the project does have a reference to the Admob library but the packaged app (the apk) doesn't.
Now because your question doesn't include the information on what development environment you are using, there's no concrete instruction I could give you to remedy the problem.
Of course it would also help if you could post the Data.java code, at least the onCreate() method to see what you are doing at line 31.

How to add an GUI component like a Button in a Editor?

I'm a beginner in RCP just started building RCP application today.I want to a GUI component like a Button ,comboBox,Checkbox in a Editor .I've managed to add a editor in Extensions and create a class for it.I have written the code to create a label in creatPartControl but it does not work..I get a black window.Should I add the editor in perspective like this
layout.addStandaloneView(Editor.id, true, IPageLayout.TOP,0.7f,
layout.getEditorArea());
layout.addStandaloneView(View.ID, true, IPageLayout.BOTTOM,0.4f,
layout.getEditorArea());
Please help me resolve this issue.If possible please give an eg on how to add a editor and create a label and a button in it.
Thank you for your help in advance
code in my Editor.java content in createPartControl()
parent.setLayout(new GridLayout());
Button b=new Button(parent,SWT.TOGGLE);
b.setText("Hello ");
Label label1 = new Label(parent, SWT.NONE);
label1.setText("First Name");
package com.hello;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;
public class Editor extends EditorPart {
public static final String ID = "TestApplication.editor3";
public Editor() {
// TODO Auto-generated constructor stub
}
#Override
public void doSave(IProgressMonitor monitor) {
// TODO Auto-generated method stub
}
#Override
public void doSaveAs() {
// TODO Auto-generated method stub
}
#Override
public void init(IEditorSite site, IEditorInput input)
throws PartInitException {
// TODO Auto-generated method stub
}
#Override
public boolean isDirty() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isSaveAsAllowed() {
// TODO Auto-generated method stub
return false;
}
#Override
public void createPartControl(Composite parent) {
Label label = new Label(parent, SWT.NONE);
label.setText("sssssss");
}
#Override
public void setFocus() {
// TODO Auto-generated method stub
}
}
You are not correctly initializing the editor and it causes problems when opening the editor. Fill up your init() method like below and see if this helps:
#Override
public void init(IEditorSite site, IEditorInput input)
throws PartInitException {
setSite(site);
setInput(input);
}
It's been a few years since I worked on an Eclipse editor. Here's a screen capture of the editor so you can see I did more than add a Button.
I extended the Viewer class to create the GUI of the editor.
I extended the EditorPart class to create the functionality of the editor.
Because of the kind of editor I was building, I had to create my own version of Canvas and my own version of IDocument.

Is it possible to add a ActionEvent Handler to a javaFX Textfield using addEventHandler method?

I tried to add action handlers to text field using the addEventHandler() but seem not to be working. Is it even possible o is it a bug? If I try the same with Button control everything is fine.
Here is the sample code:
package com.teste;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class EventHandlerTest extends Application {
#Override
public void start(Stage stage) throws Exception {
// TODO Auto-generated method stub
stage.setTitle("Custom JavaFX Event");
TextField btn = new TextField();
btn.setId("Fire Button");
btn.setText("Fire MyEvent'");
btn.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
if (event.getEventType().equals(ActionEvent.ACTION)) {
System.out.println("ActionEvent 2");
}
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
stage.setScene(new Scene(root, 300, 250));
stage.show();
}
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
launch(args);
}
}
Documentation says it should be possible, but can't find anything else. Any ideas?
If you use setOnAction rather than addEventHandler, then you will be able to capture the ActionEvent for the TextField.
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent event) {
System.out.println("ActionEvent: " + event);
}
});
I don't know why the addEventHandler code does not capture the event. I also tried addEventFilter as well and that did not work for me either (JavaFX 2.2).
Note that a TextField will generate an ActionEvent when you press the Enter key on the TextField.
In java 8 now is possible
btn.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("ActionEvent: " + event);
}
});
//Lambda Notation
btn.addEventHandler(ActionEvent.ACTION, event -> {
System.out.println("ActionEvent: " + event);
});