I want to call MyView.circle() method when I click on menu Circle but when I'm clicking on menu it is throwing a NullPointerException, which means that the object is not initialized as I expected.
Here is my code. Where am I going wrong?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0,1,1,"Circle");
menu.add(0,1,2,"Rect");
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId())
{
case 1:
MyView.circle();
break;
}
return super.onOptionsItemSelected(item);
}
public static class MyView extends View {
private Context context;
static Canvas can=null;
public MyView(Context c) {
super(c);
context=c;
}
#Override
protected void onDraw(Canvas canvas) {
mPaint=new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.BLUE);
mPaint.setAntiAlias(true);
//circle(canvas);
}
public static void circle() {
// TODO Auto-generated method stub
can.drawCircle(50, 50, 50, mPaint);
}
}
}
public void static circle(c) {
needs to be changed to
public void static circle(Canvas c) {
Might I recommend spending some time learning Java before writing Android apps?
Related
I tried placing the interface in my Activity, my fragment and my FragmentStatePagerAdapter. But in all of them the code never got there. this is what my FragmentStatePagerAdapter looks like since this is my latest try.
FragmentStatePagerAdapter :
public static class AppSectionsPagerAdapter extends FragmentStatePagerAdapter implements OnPageChangeListener
{
......
......
#Override
public void onPageScrollStateChanged(int arg0)
{
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
// TODO Auto-generated method stub
}
#Override
public void onPageSelected(int arg0)
{
// TODO Auto-generated method stub
Log.w("onpage", "onpage executed");
currentSelectedFragmentPosition = arg0;
}
public int getCurrentSelectedFragmentPosition()
{
return currentSelectedFragmentPosition;
}
}
Fragment would look like this :
public class SectionFragment extends ListFragment implements OnPageChangeListener
{
......
......
#Override
public void onPageScrollStateChanged(int arg0)
{
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
// TODO Auto-generated method stub
}
#Override
public void onPageSelected(int arg0)
{
// TODO Auto-generated method stub
Log.w("onpage", "onpage executed");
currentSelectedFragmentPosition = arg0;
}
public int getCurrentSelectedFragmentPosition()
{
return currentSelectedFragmentPosition;
}
}
But both didnt work, it would be most usefull to me to use it in a fragment but if that is not possible then the activity or statepager will work too
In AppSectionsPagerAdapter constructor: add this
mViewPager.setOnPageChangeListener(this);
(this assume the ViewPager is an instance variable named mViewPager)
Or, as an alternative in the MainActivity.onCreate() :
mViewPager.setOnPageChangeListener(mAppSectionPageAdapter);
(this assume the AppSectionPageAdapter is in a variable named mAppSectionPageAdapter)
I have 3 classes, class GameThread that extend thread, class Main that extend Activity, class GameView that extend surfaceview implement surfaceHolder.onCallback:
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("oncreate","ONcreate");
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.gameloopxml);
}
#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;
}
}
Class GameThread:
public class GameThread extends Thread {
GameView gameview;
SurfaceHolder surfaceHolder;
public boolean running;
public GameThread(SurfaceHolder surfaceHolder,GameView gameview){
this.gameview=gameview;
running=false;
this.surfaceHolder=surfaceHolder;
}
#Override
public void run() {
Canvas c;
//super.run();
while(running){
c=null;
try{
c=surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
if(c!=null){
c.drawColor(Color.BLACK);
gameview.draw(c);
gameview.update();
}
}
}finally{
if(c!=null){
surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
Class GameView:
public class GameView extends SurfaceView implements SurfaceHolder.Callback{
GameThread gamethread;
Paint p=new Paint();
int x,y;
Bitmap bitmap;
public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
Log.i("surface", "constructor");
bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
//super(context);
p.setColor(Color.RED);
//add the callback this in order to intercept events
getHolder().addCallback(this);
//create the gameloop
//make it foccusable so it can handle events
setFocusable(true);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Log.i("surface", "changed");
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
Log.i("surface", "created");
gamethread=new GameThread(getHolder(),this);
gamethread.running=true;
gamethread.start();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.i("surface", "destroyed");
boolean tryagain=false;
while(tryagain){
try {
gamethread.join();//destroy the thread
tryagain=false;
gamethread.running=false;
} catch (InterruptedException e) {
//try again shutting down the thread
}
}
}
public void draw(Canvas c) {
c.drawRect(x, y, x+80, y+80, p);
}
public void update(){
x++;
y++;
}
}
Everything works fine in my code except that whenever I press the back or home key I am getting a Nullpointer exception in the logcat.
How do I get values from a SeekBar?
I have a code of a class that has three SeekBars (PRICEbar)
I want to pass the values of these SeekBars to next Activity (screen)
as Intents
I know how toimplement OnClickListener but how can I extract the
values from the SeekBars?
ex: if PRICEbar is pointing to value 10 ... I need to get the value of 10
Filters.java
public class Filters extends Activity implements OnSeekBarChangeListener{
// declare text objects variables
private SeekBar PRICEbar;
private TextView PRICEtextProgress,DISTANCEtextProgress, RATINGtextProgress;
Button back;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// load the layout
setContentView(R.layout.filters);
PRICEbar = (SeekBar)findViewById(R.id.PRICEseekBarID); // make seekbar object
PRICEbar.setOnSeekBarChangeListener(this);
PRICEbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
PRICEtextProgress = (TextView)findViewById(R.id.PRICEtextViewProgressID);
PRICEtextProgress.setText("Price:: Rs "+progress);
seekBar.setMax(100);
}
});
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (seekBar == PRICEbar)
PRICEtextProgress.setText("Price:: Rs "+progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
The SeekBar class is a subclass of ProgressBar, which contains a getProgress() method.
Calling PRICEbar.getProgress() will return the value you are looking for.
I have one imageview, if i touch the imageview it to do some action else if i double click that imageview need to do some action.
How it is possible.?
First create a GestureDetector and a listener. Then bind it to your class that extends GestureDetector.SimpleOnGestureListener.
private GestureDetector detector;
private ImageView mImageView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
detector = new GestureDetector(this, new MyGesturesListener());
// TODO find your image view
mImageView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
detector.onTouchEvent(event);
return true;
}
});
}
Then you can implement the methods of different gestures:
class MyGesturesListener extends GestureDetector.SimpleOnGestureListener{
#Override
public boolean onSingleTapUp(MotionEvent ev) {
// TODO handle single tap
return true;
}
#Override
public boolean onDoubleTap(MotionEvent ev) {
// TODO handle double tap
return true;
}
}
i wish to implement the GestureDetector using service instead of activity. the code works on activity but not on service. In Service i am not able to #Override the onTouchEvent(MotionEvent e) method - is this the cause of the problem?
public class GestureService extends Service implements OnGestureListener {
private GestureDetector gestureScanner;
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(getBaseContext(),"onCreate", Toast.LENGTH_SHORT).show();
gestureScanner = new GestureDetector(this);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getBaseContext(),"onStartCommand", Toast.LENGTH_SHORT).show();
return START_STICKY;
}
public boolean onTouchEvent(MotionEvent me) {
return gestureScanner.onTouchEvent(me);
}
#Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(),"onDown", Toast.LENGTH_SHORT).show();
return false;
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
#Override
public boolean onSingleTapUp(MotionEvent e) {
Toast.makeText(getBaseContext(),"onSingleTapUp", Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
return false;
}
}
i started the service in an activity class using startService(new Intent(this,GestureService.class));
I'm guessing you want to make it a Service so that you can use it for all applications. But from reading this article, it's really not the way it works: gestures are application specific.