Android project problem - android

here
I uploaded my Android project made in Eclipse. The idea is that i have a service, which computes the sum of two random numbers. But when i press the OK Button, i don't see the result in that edit box... why? What i'm doing wrong? Please help
Thanks!
EDIT: The code:
//service class
package service;
import java.util.Random;
import com.android.AplicatieSuma;
import android.widget.EditText;
import android.widget.TextView;
public class ServiciuSuma
{
public ServiciuSuma() { }
public int CalculateSum()
{
Random generator=new Random();
int n=generator.nextInt();
int m=generator.nextInt();
return n+m;
}
}
The Application class:
package com.android;
import android.app.*;
import service.*;
public class ApplicationSum extends Application {
public ServiciuSuma service = new ServiciuSuma();
}
and the main Activity class:
package com.android;
import com.android.R;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class Activitate extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View btn_ok = findViewById(R.id.btn_ok);
btn_ok.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
CalculeazaSuma();
}
});
}
private void CalculeazaSuma()
{
AplicatieSuma appState = ((AplicatieSuma)this.getApplication());
EditText txt_amount = (EditText)findViewById(R.id.txt_amount);
txt_amount.setText(appState.service.CalculateSum());
//BindData();
}
}
So that edit text does not show the sum of the random generated numbers, by the service. What's wrong?
Thanks
EDIT:
the manifest xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android"
android:versionCode="1"
android:versionName="1.0.1">
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true" />
<application android:name="ApplicationSum" android:icon="#drawable/icon" android:label="#string/app_name" android:debuggable="true">
<activity android:label="#string/app_name" android:name=".Activitate">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="7" />
</manifest>

This fix will work:
txt_amount.setText(String.valueOf(appState.service.CalculateSum()));
Without that you pass integer and android thinks it's an id of a resource to display. You should really use DDMS to identify such problems.

Have you specified in manifest that you want ApplicationSum to be used as application class?
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:name="com.android.ApplicationSum">
...
</application>
</manifest>

Does this work?
public String CalculateSum()
{
Random generator=new Random();
int n=generator.nextInt();
int m=generator.nextInt();
String sum = Integer.toString(n+m) ;
Log.v("CalculateSum", "N+M = " + sum);
return sum;
}

Related

Android vibrator app force close

my app force closes I do not know why. All it should do is vibrating when it is in the foreground and stop if it is not but when I start it on my phone it force closes. I added lines about vibrator usage into the manifest by hand. Maybe this is the problem. I have to anything else with it to be valid for the app?
My manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kevinboonemidi"
android:versionCode="1"
android:versionName="1.0" >
<permission android:name="android.permission.VIBRATE" ></permission>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.haptic.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
My code:
package com.example.haptic_sof;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import com.example.kevinboonemidi.R;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
public class MainActivity extends Activity
{
protected Vibrator v = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
long pwmCycles[];
pwmCycles = new long[4];
v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
long[] pattern = { 0, 200, 10 };
v.vibrate(pattern, 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.main, menu);
return true;
}
protected void onPause(){
super.onPause();
v.cancel();
}
protected void onStop()
{
super.onStop();
v.cancel();
}
}
Your packages are a mess.
You define com.example.kevinboonemidi as your package in the manifest, declare your Activity to be in com.example.haptic and actually put your Activity in com.example.haptic_sof.
Change android:name="com.example.haptic.MainActivity" to android:name="com.example.haptic_sof.MainActivity" and your app should run, though you may need to end up using one package entirely at least to start with.

to get cdma dbm on android device

I had tried this one to show my evdo dbm.
it could run on my emulator but couldnt on my real device, why?
is there somethinh wrong ?
this my source code :
package com.cdbmsof2;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class CdBmSOF2MainActivity extends Activity implements OnClickListener {
TextView dateAndTimeLabel;
private Button closeButton;
private int signalDBM = 0;
public class GetParams extends PhoneStateListener
{
#Override
public void onSignalStrengthsChanged(SignalStrength signalStrength)
{
super.onSignalStrengthsChanged(signalStrength);
signalDBM = signalStrength.getEvdoDbm();
}
}
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_cd_bm_sof2_main);
GetParams listener = new GetParams();
TelephonyManager TelManager = ( TelephonyManager )getSystemService(Context.TELEPHONY_SERVICE);
TelManager.listen(listener ,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
Button btn=(Button)findViewById(R.id.start);
btn.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.start:
Toast.makeText(this, "CDMA signal strength is " + this.signalDBM , Toast.LENGTH_SHORT).show();
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_cd_bm_sof2_main, menu);
return true;
}
}
this my manifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cdbmsof2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".CdBmSOF2MainActivity"
android:label="#string/title_activity_cd_bm_sof2_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
</manifest>
any one would help me please? best regards.
You may want to change the MODIFY permission into a READ permission as you're not modifying phone state, but trying to read it:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
I tried your code but the value of the signalDBM will not change. It will stay the same even if there is no internet connection at all. I am trying to do something similar and your code was of good help for me but now I face that problem. Does that program changes the values for you? Please let me know..

I am getting ClassNotFoundException, in log cat? why?

For my project all my java classes are in one package - com.example.android.bitmapfun - and my manifest is:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.bitmapfun"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:description="#string/app_description"
android:hardwareAccelerated="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="com.example.android.bitmapfun.ImageGridActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.android.bitmapfun.ImageDetailActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.FullScreen" >
</activity>
</application>
</manifest>
and the Activities are:
package com.example.android.bitmapfun;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
public class ImageGridActivity extends FragmentActivity {
private static final String TAG = "ImageGridFragment";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getSupportFragmentManager().findFragmentByTag(TAG) == null) {
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(android.R.id.content, new ImageGridFragment(), TAG);
ft.commit();
}
}
}
ImageDeatilActivity.java:
package com.example.android.bitmapfun;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager.LayoutParams;
import android.widget.Toast;
import com.example.android.bitmapfun.R;
import com.example.android.bitmapfun.Images;
import com.example.android.bitmapfun.DiskLruCache;
import com.example.android.bitmapfun.ImageCache;
import com.example.android.bitmapfun.ImageFetcher;
import com.example.android.bitmapfun.ImageResizer;
import com.example.android.bitmapfun.ImageWorker;
import com.example.android.bitmapfun.Utils;
public class ImageDetailActivity extends FragmentActivity implements OnClickListener {
private static final String IMAGE_CACHE_DIR = "images";
public static final String EXTRA_IMAGE = "extra_image";
private ImagePagerAdapter mAdapter;
private ImageResizer mImageWorker;
private ViewPager mPager;
#SuppressLint("NewApi")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_detail_pager);
.....
}
LogCat: here
Why is the LogCat showing the ClassNotFoundException here? I tried but can't clear the error. Please any ideas to overcome this problem.
To use this FragMentActivity class, your application must specify API Level "11" or higher in its manifest and be compiled against a version of the Android library that supports an equal or higher API Level.
For what it is worth, I couldn't get any of the solutions to work when I imported the BitmapFun project into Eclipse. It was because of the ClassNotFoundException. I did notice that the project was missing the libs folder and the support library, but creating the libs folder and dropping the support library into it did not fix the problem. So, I created a new project in Eclipse, transferred the files over from the original imported project, and it worked on the first try. Victory.

Program force close

Here is my PlayerActivity:
package player.org;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.app.Activity;
import android.os.Bundle;
public class PlayerActivity extends Activity {
/** Called when the activity is first created. */
private MediaView media=new MediaView(this);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(media);
}
}
here is my MediaView:
package player.org;
import android.app.Activity;
import android.view.*;
import android.widget.ImageView;
import android.content.*;
import android.content.*;
import android.graphics.Color;
class MediaView extends View
{
public MediaView(Context context)
{
super(context);
setBackgroundColor(Color.WHITE);
}
}
and here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="player.org"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity
android:name=".MediaView"
android:label="#string/MediaView_title">
</activity>
<activity android:name=".PlayerActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
But when I Launch the app., It force closes.
Do you have any suggestions?
Thanks for your answers
You are writing this line private MediaView media=new MediaView(this);,before the Activity is initiated.So 'this' keyword is force closing your application
Please use like this
public class PlayerActivity extends Activity {
/** Called when the activity is first created. */
private MediaView media;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(media);
media=new MediaView(this);
}
}

Reading the tag UID of Mifare classic card

I am creating an app for NFC where my first objective is to get the tag uid from the mifare tag.
When i press the tag button my activvity goes to second activity where i should be getting the tagID.
I am getting error of resource lookup. I know i am doing some major mistakes but could not find it.
Request you to please help.
This is my Meanifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.chetan.nfc"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10"></uses-sdk>
<uses-feature android:required="true" android:name="android.hardware.nfc"></uses-feature>
<uses-permission android:name="android.permission.NFC"></uses-permission>
<application android:icon="#drawable/icon" android:label="#string/app_name" android:debuggable="true" android:enabled="true">
<activity android:name=".actOne"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="taginfo" android:label="#string/app_name" android:launchMode="standard">
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<meta-data android:resource="#xml/nfc_tech_filter" android:name="#string/nfc_tech_filter"></meta-data>
</activity>
</application>
</manifest>
This is my activity one:
package com.chetan.nfc;
import java.util.Date;
import android.app.Activity;
import android.app.LauncherActivity;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.tech.NdefFormatable;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class actOne extends Activity {
/** Called when the activity is first created. */
Button tag;
Intent i;
TextView tv;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tag=(Button)findViewById(R.id.Tag);
tv=(TextView)findViewById(R.id.textView1);
tag.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
i=new Intent(v.getContext(),taginfo.class);
startActivity(i);
//launchActivity();
}
});
}
}
This is my Activity 2
package com.chetan.nfc;
import java.io.IOException;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentFilter.MalformedMimeTypeException;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.MifareClassic;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
public class taginfo extends Activity{
TextView tv;
EditText tagIntent;
Log log;
IntentFilter mFilters[];
String mTechLists[][];
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.tagdata);
tagIntent=(EditText)findViewById(R.id.editText1);
tv=(TextView)findViewById(R.id.textView1);
tv.setText(getIntent().toString());
log.e("before get intetn", "");
readTag(getIntent());
}
public void readTag(Intent intent)
{
//tagIntent.setText(intent.getAction());
PendingIntent mPendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
NfcAdapter nfc_adapter=NfcAdapter.getDefaultAdapter(this);
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
Tag myTag = (Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] tagId = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
tagIntent.setText("");
tagIntent.setText(tagId.toString());
}
}
If the error is about "I am getting error of resource lookup", is not NFC related.
With this code you're getting the UID of the Tag correctly
.
.
.
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Log.d(TAG, "UID: " + bin2hex(tag.getId()));
.
.
.
//To display the UID
static String bin2hex(byte[] data) {
return String.format("%0" + (data.length * 2) + "X", new BigInteger(1,data));
}
If you are simulating a tag detection from your activity 1, then add the appropriate extras. Else let the android nfc service generate the intent for TECH_DISCOVERED, when it detects the actual tag.

Categories

Resources