I normally create android apps using either eclipse or NetBeans but when I try to create a messagebox on button press, Eclipse in this case won't import javax.swing
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="136dp"
android:text="Button" />
</RelativeLayout>
MainActivity.java:
package com.example.understanding;
import android.os.Bundle;
import android.app.Activity;
import javax.swing;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
private void button1_ButtonActionPerformed(View v) {
JOptionPane.showMessageDialog(null, "Enter Account Details");
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
}
javax.swing does not exist on Android and is not part of the Android SDK.
Related
As this is my first android app; i couldn't figure out what goes wrong.
Error
Error on line "caller.textView.setText(“You clicked the button!”);"
Multiple markers at this line
Syntax error, insert "AssignmentOperator Expression" to complete
Assignment
Syntax error, insert ";" to complete BlockStatements
Line breakpoint:MyOnClickListener [line: 12] - onClick(View)
setText cannot be resolved or is not a field
screenshot of error
MainActivity.java
package com.example.myfirstandroidapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button button;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new MyOnClickListener(this));
textView = (TextView) findViewById(R.id.textView1);
}
#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;
}
}
MyOnClickListener.java
package com.example.myfirstandroidapp;
import android.view.View;
import android.view.View.OnClickListener;
public class MyOnClickListener implements OnClickListener {
MainActivity caller;
public MyOnClickListener(MainActivity activity) {
this.caller = activity;
}
public void onClick(View view) {
caller.textView.setText(“You clicked the button!”);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="70dp"
android:text="Button" />
</RelativeLayout>
XML layout file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.gaurav.googlemap.HomeMap" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<fragment android:name="com.google.android.gms.maps.MapFragment"
android:id="#+id/map"
android:layout_height="match_parent"
android:layout_width="match_parent">
</fragment>
</RelativeLayout>
Java file
package com.beproject.ourway;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
public class HomeMap extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_map);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home_map, 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);
}
}
As I searched this question before also, I have done following things to solve
1.Imported following classes
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
2.Extended FragmentActivity class
Still I am getting this error in logcat
Error inflating class fragment
Is there any other way to solve this? Thanks.
Given that you have used 'support fragment' you need to replace MapFragment with SupportMapFragment. Doing this should fix it.
Use:
class="com.google.android.gms.maps.SupportMapFragment"
instead of :
android:name="com.google.android.gms.maps.MapFragment"
so your xml must be like:
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
In my android application I have three layout's activity and three tabs and below is my activity_1:
package com.example.testapp;
import java.io.ByteArrayOutputStream;
import android.support.v4.*;
import android.support.v7.app.ActionBarActivity;
import android.app.TabActivity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Base64;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
public class MainActivity extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt_display = (TextView)findViewById(R.id.txt_display);
btn_s = (Button)findViewById(R.id.btn_go);
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent_one;
intent_one = new Intent().setClass(this, MainActivity.class);
spec = tabHost.newTabSpec("tabOne");
spec.setContent(intent_one);
spec.setIndicator("Tab One");
tabHost.addTab(spec);
}
#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);
}
}
below is layout of this activity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</TabHost>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.testapp.MainActivity" >
<TextView
android:id="#+id/txt_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="96dp"
android:text="#string/hello_world" />
</RelativeLayout>
</RelativeLayout>
I am doing the same code in the other two activities, and I want to change layout's activity through tabs and i am using api level-16 and v7 but getting exception
The type TabActivity is deprecated
How can I resolve this issue, kindly suggest me waiting for reply.
Thanks
This is done by the view pager and fragment.
Here
is the link to develope the swipable tabs. you'll get a demo code for that.
According to the documentation
New applications should use Fragments instead of this class; to
continue to run on older devices, you can use the v4 support library
which provides a version of the Fragment API that is compatible down
to DONUT.
So use fragments instead.
I'm becoming mad because of this problem. I've searched a lot through the internet but no one is experiencing this problem like in my case.
What i'm doing is trying to debug a simple android app with my phone and Eclipse with ADT, composed by a TextView and a Button.
The button should write something in the TextView but actually I left the "Send" function empty.
Everytime i press that button, the app closes and in the Eclipse debug log there is an "IllegalStateException" error, related to a function called "ZygoteInit$MethodAndArgsCaller.run(), on line: 745
This is my code:
Main Activiy.java
package com.android_test.examples;
import com.example.android_test.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
protected void Send()
{
}
}
and activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/txt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<requestFocus />
<Button
android:id="#+id/btnok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="24dp"
android:onClick="Send()"
android:text="Invia" />
<EditText
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="104dp"
android:layout_toLeftOf="#+id/btnok"
android:ems="10" />
</RelativeLayout>
I'm really becoming mad because of this, please someone help me
Change protected void Send() {}
with
public void Send(View view) {}
and remove the brackets from the onClick property:
android:onClick="Send"
Change to
public void Send(View v)
{
}
and change
android:onClick="Send()"
to
android:onClick="Send"
http://developer.android.com/reference/android/view/View.html#attr_android:onClick
android:onClick
Name of the method in this View's context to invoke when the view is
clicked. This name must correspond to a public method that takes
exactly one parameter of type View. For instance, if you specify
android:onClick="sayHello", you must declare a public void
sayHello(View v) method of your context (typically, your Activity).
I just started programming a little password manager but every time I run it in the emulator, it crashes and I get the following error:
android.widget.TextView cannot be cast to android.widget.EditText
Is everyone seeing the mistake?
MainActivity.java:
package de.lennartschoch.passwordmanager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
Button login;
EditText masterpassfield;
String masterpass;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = (Button) findViewById(R.id.login);
masterpassfield = (EditText) findViewById(R.id.masterpass);
masterpass = "password";
login.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if(masterpassfield.getText().toString() == masterpass) {
Intent success = new Intent(view.getContext(), Passwords.class);
startActivityForResult(success, 0);
}
}});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<EditText
android:id="#+id/masterpass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignRight="#+id/textView1"
android:layout_centerVertical="true"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="#+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/masterpass"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="#string/login" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/masterpass"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:text="#string/masterpasstext"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Project -> Clean seems to be the only solution to me.
Trying cleaning your project a few times and then build it.
Try Project -> Clean and also check if there are any warnings in any .xml file. If there are any the R.class will not be generated correctly.