MAINMENU.CLASS
public class MainMenu extends Activity {
ImageButton playButton;
ImageButton soundButton;
SoundPlayers soundPlayers;
MediaPlayer firstMenusMusic;
MediaPlayer arenaMusic;
MediaPlayer buttonSound;
boolean now_sound;
OthersIO othersIO;
//MediaPlayer firstMenusMusic;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
playButton = (ImageButton) findViewById(R.id.play_button);
soundButton = (ImageButton) findViewById(R.id.sfx_sound);
playButtonEvent();
}
private void playButtonEvent() {
playButton.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MainMenu.this, UserConfig.class);
MainMenu.this.finish();
startActivity(intent);
}
});
}
}
USERCONFIG.CLASS
public class UserConfig extends Activity {
ImageButton userConfigBack;
ImageButton userConfigOK;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_config);
....
}
public void chooseCharacter() {
....
}
public void back() {
userConfigBack.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(UserConfig.this, MainMenu.class);
UserConfig.this.finish();
startActivity(intent);
}
});
}
public void OK() {
userConfigOK.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(UserConfig.this, EnemyConfig1.class);
UserConfig.this.finish();
createPlayerData();
startActivity(intent);
}
});
}
public void createPlayerData() {
....
}
}
This is the Android Manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ulartangga"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.ulartangga.MainMenu"
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=".UserConfig"
android:parentActivityName=".MainMenu" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainMenu" />
</activity>
<activity
android:name=".EnemyConfig1"
android:parentActivityName=".UserConfig"
>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".UserConfig" />
</activity>
<activity
android:name=".EnemyConfig2"
android:parentActivityName=".EnemyConfig1"
>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".EnemyConfig1" />
</activity>
<activity
android:name=".Arena"
android:parentActivityName=".EnemyConfig1"
>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".EnemyConfig1" />
</activity>
</application>
</manifest>
My problem is...
when I click play button on view from mainmenu.class. I can go to another page(go to view from userconfig.class). but when I click OK button in view from userconfig.class, I can't go to another page.
in USERCONFIG.CLASS change it to
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_config);
OK();
back();
}
call UserConfig.this.finish(); after startActivity()
Related
my program is not working ,
i have some kind of a problem with the surfaceviewexample class.
Unable to start activity java.lang.NullPointerException.
i am trying to push on the tut5 button which i made to start my surfaceview.
what is the reason for it ?
what am i doing wrong ?
SurfaceViewExample.java
public class SurfaceViewExample extends Activity implements View.OnTouchListener{
OurView v;
Bitmap ball;
float x,y;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
v=new OurView(this);
v=setOnTouchListener(this);
ball= BitmapFactory.decodeResource(getResources(),R.drawable.ball);
x=y=0;
setContentView(v);
}
private OurView setOnTouchListener(SurfaceViewExample surfaceViewExample) {
return null;
}
#Override
protected void onPause(){
super.onPause();
v.pause();
}
#Override
protected void onResume(){
super.onResume();
v.resume();
}
#Override
public boolean onTouch(View v , MotionEvent me) {
return false;
}
public class OurView extends SurfaceView implements Runnable {
Thread t=null;
SurfaceHolder holder;
boolean isItOK=false;
public OurView(Context context){
super(context);
holder=getHolder();
}
#Override
public void run()
{
while (isItOK==true)
{
if(!holder.getSurface().isValid()){
continue;
}
Canvas c=holder.lockCanvas();
c.drawARGB(255,150,150,10);
c.drawBitmap(ball,x,y,null);
holder.unlockCanvasAndPost(c);
}
}
public void pause(){
isItOK=false;
while(true){
try{
t.join();
}catch (InterruptedException e){
e.printStackTrace();
}
break;
}
t=null;
}
public void resume(){
isItOK=true;
t=new Thread(this);
t.start();
}
}
}
menu.java
public class menu extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView (com.example.youtubeproject.R.layout.activity_main);
final MediaPlayer buttonSound = MediaPlayer.create(menu.this,
com.example.youtubeproject.R.raw.button_click);
Button tut1= (Button)findViewById(com.example.youtubeproject.R.id.tutorial1);
Button tut2= (Button)findViewById(com.example.youtubeproject.R.id.tutorial2);
Button tut3= (Button)findViewById(com.example.youtubeproject.R.id.tutorial3);
Button tut4=(Button)findViewById(com.example.youtubeproject.R.id.tutorial4);
Button tut5=(Button)findViewById(com.example.youtubeproject.R.id.tutorial5);
tut1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonSound.start();
setContentView (com.example.youtubeproject.R.layout.tutorial1);
Intent TutorialIntent=new Intent("com.example.youtubeproject.TUTORIAL1");
startActivity(TutorialIntent);
//startActivity(new Intent("com.example.youtubeproject.TUTORIAL1"));
}
});
tut2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonSound.start();
startActivity(new Intent("com.example.youtubeproject.TUTORIALTWO"));
}
});
tut3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonSound.start();
setContentView (com.example.youtubeproject.R.layout.wallpapaer);
Intent TutorialIntent=new
Intent("com.example.youtubeproject.TUTORIALTHREE");
startActivity(TutorialIntent);
}
});
tut4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonSound.start();
Intent TutorialIntent=new
Intent("com.example.youtubeproject.TUTORIALFOUR");
startActivity(TutorialIntent);
//setContentView (com.example.youtubeproject.R.layout.wallpapaer);
//Intent TutorialIntent=new
Intent("com.example.youtubeproject.TUTORIALTHREE");
//startActivity(TutorialIntent);
}
});
tut5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonSound.start();
//startActivity(new Intent("com.example.youtubeproject.TUTORIALFIVE"));
Intent TutorialIntent=new
Intent("com.example.youtubeproject.TUTORIALFIVE");
startActivity(TutorialIntent);
//setContentView (com.example.youtubeproject.R.layout.wallpapaer);
//Intent TutorialIntent=new
Intent("com.example.youtubeproject.TUTORIALTHREE");
//startActivity(TutorialIntent);
}
});
}
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuInflater awesome=getMenuInflater();
awesome.inflate(com.example.youtubeproject.R.menu.main_menu,menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch(item .getItemId()){
case com.example.youtubeproject.R.id.menuSweet:
startActivity(new Intent("com.example.youtubeproject.SWEET"));
return true;
case com.example.youtubeproject.R.id.menuToast:
Toast andEggs=Toast.makeText(menu.this,"this is a toast
",Toast.LENGTH_LONG);
andEggs.show();
return true;
}
return false;
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.youtubeproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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>
<activity
android:name=".menu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.youtubeproject.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Tutorial1"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.youtubeproject.TUTORIAL1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".TutoralTwo"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.youtubeproject.TUTORIALTWO" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".TutorialThree"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.youtubeproject.TUTORIALTHREE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".SurfaceViewExample"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.youtubeproject.TUTORIALFIVE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".TutorialFour"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.youtubeproject.TUTORIALFOUR" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Sweet"
android:label="#string/app_name"
android:theme="#android:style/Theme.Dialog" >
<intent-filter>
<action android:name="com.example.youtubeproject.SWEET" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Declare you SurfaceViewExample activity into Menifest like that;
<activity
android:name="com.example.youtubeproject.SurfaceViewExample" >
</activity>
hy..i have a task to make my kitkat-nexus to act as a tag. I have ACS 122U as reader. i have read the program example in this site http://blog.opendatalab.de/hack/2013/11/07/android-host-card-emulation-with-acr122/. then i tryed the code on my own eclipse.
main activity :
public class MainActivity extends Activity implements OnMessageReceived, ReaderCallback {
private NfcAdapter nfcAdapter;
private ListView listView;
private IsoDepAdapter isoDepAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.listView);
isoDepAdapter = new IsoDepAdapter(getLayoutInflater());
listView.setAdapter(isoDepAdapter);
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
Log.i("end of onCreate-----","onCreate HCE");
}
#Override
public void onResume() {
super.onResume();
//nfcAdapter.enableReaderMode(this, this, NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK,
// null);
//nfcAdapter.disableReaderMode(this); //tambahan poipo
Log.i("onResume---", "onResume");
}
#Override
public void onPause() {
super.onPause();
nfcAdapter.disableReaderMode(this);
Log.i("onPause---", "onPause");
}
#Override
public void onTagDiscovered(Tag tag) {
IsoDep isoDep = IsoDep.get(tag);
IsoDepTransceiver transceiver = new IsoDepTransceiver(isoDep, this);
Thread thread = new Thread(transceiver);
Log.i("dibawah thread", "ontagdiscovered");
thread.start();
}
#Override
public void onMessage(final byte[] message) {
runOnUiThread(new Runnable() {
#Override
public void run() {
isoDepAdapter.addMessage(new String(message));
Log.i("didlmrun---", "onMessage");
}
});
Log.i("diluarrun---", "onMessage");
}
#Override
public void onError(Exception exception) {
onMessage(exception.getMessage().getBytes());
}
}
hostapduservice :
...
...
...
#Override
public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
if (selectAidApdu(apdu)) {
Log.i("HCEDEMO====", "Application selected====");
return getWelcomeMessage();
}
else {
Log.i("HCEDEMO======", "Received: =====" + new String(apdu));
return getNextMessage();
}
}
...
...
...
then in the manifest file :
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="FEATURE_NFC_HOST_CARD_EMULATION"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<service
android:name=".MyHostApduService"
android:exported="true"
android:permission="android.permission.BIND_NFC_SERVICE" >
<intent-filter>
<action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE" />
</intent-filter>
<meta-data
android:name="android.nfc.cardemulation.host_apdu_service"
android:resource="#xml/apduservice" />
</service>
<activity
android:name="de.grundid.hcedemo.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>
ok,,when i ran the above source code,, i saw my acs122u blinking continously when i tapped my nexus near to it. but i didn't see the log.i(....) from hostapdu service. In the eclipse log cat, there were just some log.i from main activity. what should i do to bind that hostapdu service to main activity, so my nexus can act as a tag...???
thanks in advance... :-)
Manifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
>
<activity
android:name="com.teamandy.MainActivity"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
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:theme="#*android:style/Theme.Black.NoTitleBar" android:name="com.teamandy.Choosetype" android:screenOrientation="portrait" />
<activity android:theme="#*android:style/Theme.Black.NoTitleBar" android:name="com.teamandy.frames1" android:screenOrientation="portrait" />
</application>
Main Activity:
this.startb=(Button)findViewById(R.id.btn_start);
this.startb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent strtint=new Intent(MainActivity.this, Choosetype.class);
//strtint.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
strtint.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
strtint.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
strtint.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(strtint);
finish();
}
});
Choosetype Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.choosefrm);
}
public void f1(View v ){
Choosetype.number=1;
this.mcontexts=this;
startActivity();
}
public void f2(View v){
Choosetype.number=2;
this.mcontexts=this;
startActivity();
.........................unti f20 etc. used
i have problem with sending Broadcast receive from one activity to other ..its not working my code is below..pls refer to it ..
sending class is:
public class SendBroadcast extends Activity {
public static String BROADCAST_ACTION = "com.unitedcoders.android.broadcasttest.SHOWTOAST";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sendBroadcast();
}
});
}
public void sendBroadcast(){
Intent broadcast = new Intent();
broadcast.setAction("com.unitedcoders.android.broadcasttest.SHOWTOAST");
sendBroadcast(broadcast);
}
}
and reciving class is :
public class ToastDisplay extends Activity {
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("asdasd","sdasdasd");
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "%%%%%%%%%%%%received", Toast.LENGTH_SHORT).show();
}
};
#Override
protected void onResume() {
IntentFilter filter = new IntentFilter();
filter.addAction(SendBroadcast.BROADCAST_ACTION);
registerReceiver(receiver, filter);
super.onResume();
}
#Override
protected void onPause() {
unregisterReceiver(receiver);
super.onPause();
}
}
Manifest file is ::::
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.unitedcoders.android.broadcasttest"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".SendBroadcast"
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=".ToastDisplay">
<intent-filter>
<action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST"> </action>
</intent-filter>
</activity>
</application>
</manifest>
you have not registered your Receiver in Manifest :registered as
<receiver android:name="receiver">
<intent-filter>
<action
android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST"/>
</intent-filter>
</receiver>
I am new in android and i would like to lock the phone. So I Read through Device Admin documentation. and have my code looking like this. But it doesnt seem to notice it on my manifest suggests if i have it defined in it which i do
my manifest.xml
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".MainLauncher"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=".Controller"android:label="#string/app_name">
</activity>
<activity android:name=".DeviceAdminSample$Controller"android:label="#string/activity_sample_device_admin">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
</activity>
<receiver android:name=".DeviceAdminSample"android:label="#string/sample_device_admin"android:description="#string/sample_device_admin_description"android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"android:resource="#xml/device_admin_sample" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
</manifest>
My Code
public class DeviceAdminSample extends DeviceAdminReceiver {
public static class Controller extends Activity {
static final int RESULT_ENABLE = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
mAM = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
mDeviceAdminSample = new ComponentName(Controller.this, DeviceAdminSample.class);
mEnableButton = (Button)this.findViewById(R.id.button1);
mEnableButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
mDeviceAdminSample);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"Additional text explaining why this needs to be added.");
startActivityForResult(intent, RESULT_ENABLE);
}
});
mDisableButton = (Button)this.findViewById(R.id.button2);
mDisableButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mDPM.removeActiveAdmin(mDeviceAdminSample);
updateButtonStates();
}
});
mForceLockButton = (Button)this.findViewById(R.id.button3);
mForceLockButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.i(" In","In the forc lock button");
if (mAM.isUserAMonkey()) {
AlertDialog.Builder builder = new AlertDialog.Builder(Controller.this);
builder.setMessage("You can't lock my screen because you are a monkey!");
builder.setPositiveButton("I admit defeat", null);
builder.show();
return;
}
boolean active = mDPM.isAdminActive(mDeviceAdminSample);
if (active) {
v mDPM.lockNow();
}
}
});
}
void updateButtonStates() {
boolean active = mDPM.isAdminActive(mDeviceAdminSample);
if (active) {
mForceLockButton.setEnabled(true);
} else {
mForceLockButton.setEnabled(false);
}
}
}
}
My Other Activity
package com.examples;
public class MainLauncher extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlauncher);
Button btnLock = (Button)this.findViewById(R.id.button22);
btnLock.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.reenableKeyguard();
}
});
Button btnDevice = (Button)this.findViewById(R.id.button11);
btnDevice.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
try{
Intent openScreen = new Intent();
openScreen.setClass(MainLauncher.this, DeviceAdminSample.class);
startActivity(openScreen);
}
catch(Exception e)
{Log.i("DeviceAdmin button",String.valueOf(e));}
}
});
}
}
Have you added a resource under "xml" folder called "device_admin_sample.xml"? It will have permission required for doing a "force lock" for Device administrator to work.