It shows an error in "startActivity(i);".
Error: startActivity (android.content.Intent) in Activity cannot be
applied.
Here's code:
public class LoadingScreen extends AppCompatActivity {
private static int SplashInterval = 2000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loading_screen);
new Handler().postDelayed(new Runnable(){
#Override
public void run(){
//TODO Auto-generated method stub
Intent i = new Intent(LoadingScreen.this, MainActivity.class);
startActivity(i);
this.finish();
}
private void finish(){
//TODO Auto-generated method stub
}
},SplashInterval);
};
}
Try this,
public class LoadingScreen extends Activity {
private static int SplashInterval = 2000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loading_screen);
new Handler().postDelayed(new Runnable(){
#Override
public void run(){
//TODO Auto-generated method stub
Intent i = new Intent(LoadingScreen.this, MainActivity.class);
startActivity(i);
finish();
}
},SplashInterval);
};
}
Remove this line
this.finish();
which is written inside handler
rather do LoadingScreen.this.finish();
to achieve closing of this activity;
I have a method that takes lat/lng from mysql and puts the markers on the map.
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
myMethod();
}
});
Everything in this way works.
But when i try to put this method with ProgressDialog markers do not appear. After push the button i see only "loading..." and "Done".
This is a way that does not work:
I
pg = new ProgressDialog(this);
pg.setMessage("wait...");
II
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
pg.show();
new BackgroundJob().execute();
Toast.makeText(MapsActivity.this, "loading...", Toast.LENGTH_SHORT).show();
}
});
III
private class BackgroundJob extends AsyncTask<Void, Void, Void>
{
#Override
protected Void doInBackground(Void... params) {
myMethod();
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
Toast.makeText(MapsActivity.this, "Done", Toast.LENGTH_SHORT).show();
pg.cancel();
}
}
what is wrong here?
I am getting java.lang.NullPointerException for this method. I have check the adapter and lists are just fine and the list is ofcourse under fragment_phonelist xml file.
private void showCustomDialog() {
final Dialog dialog = new Dialog(SetupProfile.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.fragment_phonelist);
Button done = (Button)dialog.findViewById(R.id.btn_done);
Button canCel = (Button)dialog.findViewById(R.id.btn_cancel);
ListView PhoneListView = (ListView)findViewById(R.id.list_phone);
MyCustomAdapter tst = new MyCustomAdapter(this,ContactName,ContactNumb);
PhoneListView.setAdapter(tst);
done.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
canCel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
dialog.show();
}
Please any help..
This line
ListView PhoneListView = (ListView)findViewById(R.id.list_phone);
Shouldn't that be like this:
ListView PhoneListView = (ListView)dialog.findViewById(R.id.list_phone);
I have couple of activities. On the mainActivity I have 4 buttons each leads to another activity.
On three of the activities I activate a thread which draws in a canvas and in each of them I have a button to return to the mainActivity. I have tried using:
Intent i = new Intent(getApplicationContext(), MainActivity.class);
// i tried also (theClassName.this,MainActivity.Class)
startActivity(i);
When I clicked on the return button, I did return to the mainActivity but the app stopped responding (it alerted me that the app isn't responding and I had to choose between waiting or exiting the app. Waiting doesn't help).
When I used
finish();
instead,
And clicked on the return button, I returned to the home screen of the device (went out of the app) but didn't exit it - I could still go back to the app using the task manager, and when I did it put me back in the activity I entered and not the mainActivity.
As I said I have 4 buttons on the mainActivity and when I use the fourth one it goes to another activity that doesn't activate a thread, it only has listView, and when I return from it to the mainActivity it does work.
How can I fix it? Thanks for the help
*when the problem occurs there is no error stack trace.
*if you need to see any code tell and I'll be happy to post it here
The code:
MainActivity:
public class MainActivity extends Activity {
Button classic;
Button vsPlayer;
Button vsComp;
// Button settings;
Button scores;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
classic=(Button) findViewById(R.id.Classic);
classic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(MainActivity.this,ClassicActivity.class);
startActivity(i);
}
});
vsPlayer=(Button) findViewById(R.id.PlayerVSPlayer);
vsPlayer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(MainActivity.this,VsPlayerActivity.class);
startActivity(i);
}
});
vsComp=(Button) findViewById(R.id.PlayerVSComp);
vsComp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(MainActivity.this,VsCompActivity.class);
startActivity(i);
}
});
/* settings=(Button) findViewById(R.id.Settings);
settings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(MainActivity.this,SettingsActivity.class);
startActivity(i);
}
});
*/
scores=(Button) findViewById(R.id.Scores);
scores.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(MainActivity.this,ScoresActivity.class);
startActivity(i);
}
});
}//onCreate
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mains, menu);
return true;
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
MainActivity.this.finish();
}
}
-=------ some other activity:
public class ClassicActivity extends Activity {
Classic g;
ImageView left;
ImageView right;
ImageView up;
ImageView down;
TextView pause;
TextView back;
TextView scoretv;
LinearLayout surface;
LinearLayout screen;
LinearLayout stoped;
LinearLayout gameover;
ScoreDataSource sds;
Button no;
Button yes;
EditText name;
Button continueb;
Button leave;
//protected static TextView MyScore;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*requestWindowFeature(Window.FEATURE_NO_TITLE);
v= new GameView1(this);
setContentView(v);
*/
setContentView(R.layout.activity_classic);
sds=new ScoreDataSource(this);
gameover=(LinearLayout) findViewById(R.id.gameover);
screen = (LinearLayout)findViewById(R.id.screen);
surface = (LinearLayout)findViewById(R.id.surface);
g= new Classic(this, surface);
surface.addView(g);
//MyScore=(TextView) findViewById(R.id.MyScore);
//MyScore.setText("My score:"+gv.getPoints());
left=(ImageView) findViewById(R.id.left);
right=(ImageView) findViewById(R.id.right);
up=(ImageView) findViewById(R.id.up);
down=(ImageView) findViewById(R.id.down);
left.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(g.snake.getDir()!="LEFT"&&g.snake.getDir()!="RIGHT"){
g.snake.Left();
}
}
});
right.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(g.snake.getDir()!="RIGHT"&&g.snake.getDir()!="LEFT"){
g.snake.Right();
}
}
});
up.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(g.snake.getDir()!="UP"&&g.snake.getDir()!="DOWN"){
g.snake.Up();
}
}
});
down.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(g.snake.getDir()!="DOWN"&&g.snake.getDir()!="UP"){
g.snake.Down();
}
}
});
pause=(TextView) findViewById(R.id.Pause);
back=(TextView) findViewById(R.id.Back);
stoped=(LinearLayout) findViewById(R.id.stoped);
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(g.getRunning()){
pause.setText("continue");
g.setRunning(false);
}
else{
pause.setText("pause");
g.setRunning(true);
new Thread(g.snakethread).start();
}
}
});
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
g.setRunning(false);
stoped.setVisibility(View.VISIBLE);
}
});
no=(Button) findViewById(R.id.no);
yes=(Button) findViewById(R.id.yes);
continueb=(Button) findViewById(R.id.Continue);
leave=(Button) findViewById(R.id.leave);
no.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
name=(EditText) findViewById(R.id.name);
yes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
String date = df.format(c.getTime());
sds.open();
Score s=new Score(0,name.getText().toString(),date, g.getPoints(),"Classic");
s=sds.createScore(s);
sds.close();
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
});
continueb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stoped.setVisibility(View.GONE);
g.setRunning(true);
new Thread(g.snakethread).start();
}
});
leave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
});
scoretv=(TextView) findViewById(R.id.scoretv);
screen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(g.getGameover()){
scoretv.setText("Your score is:"+g.getPoints());
gameover.setVisibility(View.VISIBLE);
g.setGameover(false);
}
}
});
}
#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;
}
}
----ClassicActivity usus this activity:
public class Classic extends SurfaceView implements Runnable {
private boolean isRunning=false;
private final long FPS=8;//MAX is 16
final float scale = Resources.getSystem().getDisplayMetrics().density;
float PART_SIZE= 10*scale;
private SurfaceHolder holder;
Thread snakethread;
static SnakePlayer snake;
Arena a;
LinearLayout view;
Bitmap arena;
Food food1;
Boolean gameover;
int points;
Paint writePB=new Paint();
Paint writePR=new Paint();
public Classic(Context context,LinearLayout view) {
super(context);
this.gameover=false;
this.view=view;
this.a=new Arena(view,PART_SIZE,33,36,1,0);
this.food1=new Food(PART_SIZE,"GRAY");
points=0;
writePB.setColor(Color.BLACK);
writePB.setTextSize(PART_SIZE);
writePR.setColor(Color.RED);
writePR.setTextSize(PART_SIZE);
snakethread=new Thread(this);
holder= getHolder();
holder.addCallback(new SurfaceHolder.Callback(){
#Override
public void surfaceCreated(SurfaceHolder arg0) {
setRunning(true);
snakethread.start();
}
#Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,int arg3) {
}
#Override
public void surfaceDestroyed(SurfaceHolder arg0) {
setRunning(false);
while(true){
try {
snakethread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
snake=new SnakePlayer(0+3*PART_SIZE, 0+3*PART_SIZE, "RIGHT",PART_SIZE,2,"RED","BLACK");
}
#SuppressLint("WrongCall")
#Override
public void onDraw(Canvas c) {
a.onDraw(c);
c.drawText("Your Score:"+points, 5*scale, 10*scale, writePB);
food1.drawFood(c);
snake.onDraw(c);
}
#SuppressLint("WrongCall")
#Override
public void run() {
long stepPerSecond=1000/FPS;
long startTime;
long sleepTime;
Canvas c = null;
while(isRunning){
startTime=System.currentTimeMillis();
c = null;
try{
c=this.getHolder().lockCanvas();
onDraw(c);
}
catch(Exception e){
}
finally{
if(c!=null){
this.getHolder().unlockCanvasAndPost(c);
}
}
AllChecks(c);
sleepTime= stepPerSecond-(System.currentTimeMillis()-startTime);
if(sleepTime>0)
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(gameover){
try{
c=this.getHolder().lockCanvas();
c.drawText("GAMEOVER", 125*scale, 150*scale, writePR);
c.drawText("Touch Anywhere To Continue ", 87*scale, 162*scale, writePR);
}
catch(Exception e){
}
finally{
if(c!=null){
this.getHolder().unlockCanvasAndPost(c);
}
}
}
}
boolean firstTime=true;
public void AllChecks(Canvas c){
/*
* Call all the right function, to check if the snake has bumped in a wall, or eaten a food.
*/
if(Arena.WallCollision(snake.getxLoc(), snake.getyLoc())){
setRunning(false);
gameover=true;
}
if(snake.snakeCollisionNoHead(snake.getxLoc(), snake.getyLoc())){
setRunning(false);
gameover=true;
}
if(firstTime){
firstTime=false;
changeFoodLoc(food1);
}
if(food1.hasEaten(snake.getxLoc(), snake.getyLoc())){
changeFoodLoc(food1);
snake.Add();
points+=10;
}
}
public void changeFoodLoc(Food food){
/*
* if a snake has eaten a food it changes the location of the food randomly
*/
Random rnd = new Random();
food.x=(rnd.nextInt(32) + 3)*food.SIZE;
food.y=(rnd.nextInt(35) + 1)*food.SIZE;
if(snake.snakeCollision(food.getX(), food.getY()))
changeFoodLoc(food);
}
public void setRunning(boolean b){
isRunning=b;
}
public Boolean getRunning(){
return isRunning;
}
public int getPoints() {
return points;
}
public void setPoints(int points) {
this.points = points;
}
public Boolean getGameover() {
return gameover;
}
public void setGameover(Boolean gameover) {
this.gameover = gameover;
}
}
First I would try removing the override on the MainActivity for onPause - you do not need to call finish. In your second activity, if you want to go back to the MainActivity- call finish on the 2nd activity and it should go back to the MainActivity.
I integrated Google+ button, and I see it in my app but it's "greyed out" or white, it's not red and white and when I click it nothing happens. Here's my code:
public class Menu extends Activity implements OnClickListener, ConnectionCallbacks,
OnConnectionFailedListener{
private static final int REQUEST_CODE_RESOLVE_ERR = 9000;
private ProgressDialog mConnectionProgressDialog;
private PlusClient mPlusClient;
private ConnectionResult mConnectionResult;
private PlusOneButton mPlusOneButton;
ImageButton exit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final PlusOneButton mPlusOneButton = new PlusOneButton(this);
mPlusClient = new PlusClient.Builder(this, this, this).clearScopes()
.build();
mPlusOneButton.initialize("https://market.android.com/details?id=" + getPackageName(),
new OnPlusOneClickListener() {
#Override
public void onPlusOneClick(Intent intent) {
mPlusOneButton.setVisibility(View.INVISIBLE);
startActivityForResult(intent, 0);
}
});
setContentView(R.layout.menu);
inicijalizujVars();
}
#Override
protected void onStart() {
super.onStart();
mPlusClient.connect();
}
#Override
protected void onStop() {
super.onStop();
mPlusClient.disconnect();
}
#Override
public void onConnectionFailed(ConnectionResult result) {
if (mConnectionProgressDialog.isShowing()) {
if (result.hasResolution()) {
try {
result.startResolutionForResult(this,
REQUEST_CODE_RESOLVE_ERR);
} catch (SendIntentException e) {
mPlusClient.connect();
}
}
}
mConnectionResult = result;
}
#Override
protected void onActivityResult(int requestCode, int responseCode,
Intent intent) {
if (requestCode == REQUEST_CODE_RESOLVE_ERR
&& responseCode == RESULT_OK) {
mConnectionResult = null;
mPlusClient.connect();
}
}
private void inicijalizujVars() {
exit = (ImageButton) findViewById(R.id.imageButtonExit);
exit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
#Override
public void onResume(){
super.onResume();
}
#Override
public void onBackPressed()
{
super.onBackPressed();
}
#Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
}
#Override
public void onDisconnected() {
// TODO Auto-generated method stub
}
}
And my button in my layout xml:
<com.google.android.gms.plus.PlusOneButton
xmlns:plus="http://schemas.android.com/apk/lib/com.google.android.gms.plus"
android:id="#+id/plus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginTop="-30dp"
android:gravity="center"
plus:annotation="bubble"
plus:size="tall" />