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);
}
}
Related
it has been a week to try to solve this problem...
I keep trying to put interstitial ad to my application.
And when testing with test-unit-id works fine on my application.
BUT! When changing to my real-unit-id, Ad don't be loaded in my application at all..
i don't know why this is happening.
so confused.
I used the same sample code in this tutorial, below link.
https://developers.google.com/admob/android/interstitial
and i uploaded the screenshots in my admob -id.
and i will give my codes in my apps. please let me know what is the problem. thanks in advance :)
[enter image description here][1]
[enter image description here][2]
below is my application codes.
1.mainactivity.java
package admobtest.com.theaterwin.theateradmobtest;
import android.app.AlertDialog;
import android.app.Application;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutionException;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
public class MainActivity extends AppCompatActivity {
private static final long GAME_LENGTH_MILLISECONDS = 3000;
private InterstitialAd interstitialAd;
private CountDownTimer countDownTimer;
private Button retryButton;
private boolean gameIsInProgress;
private long timerMilliseconds;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize the Mobile Ads SDK.
MobileAds.initialize(this, "ca-app-pub-4942856506050335~1501960845");
// Create the InterstitialAd and set the adUnitId.
interstitialAd = new InterstitialAd(this);
// Defined in res/values/strings.xml
interstitialAd.setAdUnitId(getString(R.string.ad_unit_id));
interstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
startGame();
}
});
// Create the "retry" button, which tries to show an interstitial between game plays.
retryButton = findViewById(R.id.retry_button);
retryButton.setVisibility(View.INVISIBLE);
retryButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showInterstitial();
}
});
startGame();
}
private void createTimer(final long milliseconds) {
// Create the game timer, which counts down to the end of the level
// and shows the "retry" button.
if (countDownTimer != null) {
countDownTimer.cancel();
}
final TextView textView = findViewById(R.id.timer);
countDownTimer = new CountDownTimer(milliseconds, 50) {
#Override
public void onTick(long millisUnitFinished) {
timerMilliseconds = millisUnitFinished;
textView.setText("seconds remaining: " + ((millisUnitFinished / 1000) + 1));
}
#Override
public void onFinish() {
gameIsInProgress = false;
textView.setText("done!");
retryButton.setVisibility(View.VISIBLE);
}
};
}
#Override
public void onResume() {
// Start or resume the game.
super.onResume();
if (gameIsInProgress) {
resumeGame(timerMilliseconds);
}
}
#Override
public void onPause() {
// Cancel the timer if the game is paused.
countDownTimer.cancel();
super.onPause();
}
private void showInterstitial() {
// Show the ad if it's ready. Otherwise toast and restart the game.
if (interstitialAd != null && interstitialAd.isLoaded()) {
interstitialAd.show();
} else {
Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show();
startGame();
}
}
private void startGame() {
// Request a new ad if one isn't already loaded, hide the button, and kick off the timer.
if (!interstitialAd.isLoading() && !interstitialAd.isLoaded()) {
AdRequest adRequest = new AdRequest.Builder().build();
interstitialAd.loadAd(adRequest);
}
retryButton.setVisibility(View.INVISIBLE);
resumeGame(GAME_LENGTH_MILLISECONDS);
}
private void resumeGame(long milliseconds) {
// Create a new timer for the correct length and start it.
gameIsInProgress = true;
timerMilliseconds = milliseconds;
createTimer(milliseconds);
countDownTimer.start();
}
}
2. Androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="admobtest.com.theaterwin.theateradmobtest">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-4942856506050335~1501960845"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="#android:style/Theme.Translucent" />
</application>
</manifest>
3. vales - strings.xml
<resources>
<string name="app_name">TheaterAdmobTest</string>
<string name="action_settings">Settings</string>
<string name="impossible_game">Impossible Game</string>
<!--Replace with your own ad unit id. -->
<!--<string name="ad_unit_id">ca-app-pub-3940256099942544/1033173712</string>-->
<!--this is actual abunit_id 입니다 . -->.
<string name="ad_unit_id">ca-app-pub-4942856506050335/8049265316</string>
</resources>
If you have not uploaded your app to google store, the real ads will not show. I've met this case.
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.
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.
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.
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;
}