I have 2 classes for an Android project.
The first class is the Activity and the second class is just a OnClickListener which implements the interface.
If I run the project on my phone I always get an runtime error.
Also I got the message:
The specified activity does not exist! Getting the launcher activity.
Here are my two classes
SendActivity
package kops.sms;
//import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
public class SendActivity extends Activity {
Button buttonSend= (Button) findViewById(R.id.buttonSend);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send);
buttonSend.setOnClickListener(new ButtonListener());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.send, menu);
return true;
}
}
and the ButtonListener
package kops.sms;
import android.view.View;
import android.view.View.OnClickListener;
public class ButtonListener implements OnClickListener {
#Override
public void onClick(View v)
{
}
}
I donĀ“t know what is wrong...
I look forward to your replies! :)
You cannot call findViewById() until after you call setContentView(). Please move:
Button buttonSend= (Button) findViewById(R.id.buttonSend);
to after:
setContentView(R.layout.activity_send);
and before:
buttonSend.setOnClickListener(new ButtonListener());
Also, in the future, please use LogCat (e.g., in the DDMS perspective in Eclipse) to examine the Java stack trace associated with your crashes. You would have been told about your NullPointerException, and that may have helped you to fix your problem.
Be sure that your Activity is declared in your manifest. Also, change your onCreate()
public class SendActivity extends Activity {
Button buttonSend;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send);
buttonSend = (Button) findViewById(R.id.buttonSend);
buttonSend.setOnClickListener(new ButtonListener());
}
You can't call a View such as a Button before you call setContentView() as it exists in your Layout and you haven't inflated your Layout until you call setContentViewe().
If these don't fix your problem then please post Logcat
Edit
Unless I missed it, you need to have all of your Activitys in your manifest. Something like:
<activity
android:name="your.package.name.SendActivity"
// activity attributes such as config changes, lable, etc...
</activity>
Logcat
Logcat output can be one of the most important pieces to determining a crash. It lists what the error was and a line number with activity where the problem occurred. If using Eclipse,
Window-->Show View-->Other-->Android-->Logcat
If you copy/paste the Logcat using coding brackets, it makes getting help much easier. You can also set filters for the logs so you don't get every single message and it is much more manageable. For example, I have a filter with: Filter Name: Runtime, by Log Tag: AndroidRuntime, by Log Level: error. This gives me only error messages for runtime errors/crashes. These filters are on the left side of the logcat view. Hope this helps
Related
I'm trying to develop a game app. At the gameover screen, I want to have a button that goes back to the start when you click it. But the problem is, it doesn't work. I really don't know why it doesn't work, I have tried everything but can't find the problem. Can someone help me?
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class GameOver extends AppCompatActivity {
MediaPlayer gameoversound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gameover);
Button weiter_button = (Button) findViewById(R.id.weiter);
weiter_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { goToMainActivity(); }
});
}
private void goToMainActivity() {
Intent back = new Intent( this, MainActivity.class);
startActivity(back);
}
}
Change this to GameOver.this
You don't have to cast widgets explicitly anymore, except for some special cases. It has been this way for a while.
If the game is over, you'd best insert a finish(); after launching the main Activity. You don't want users to be able to go back to the gameover screen by pressing the back button.
Set up FLAGS for your Intents. This comes in handy because if you didn't finish(); some Activity it remains in the stack, so it will be launching that one, but then you might want a new one. This will cause issues in navigation.
Add/ check your onBackPressed() methods for the 2 Activities.
Furthermore, specify that you are overriding the method
#Override
public void onClick(View view)
{
}
In the XML, <Button> tag, add
android:clickable=true
android:focusable=true
I'm trying to create a game log, to show what happened on each round of the game. The log is in another activity but I want to be updated constantly with whatever the player does. A player just presses buttons that do certain things. It's too much code to post, but I have made a new bundle, and an intent.
In MainActivity.
Bundle bundle = new Bundle();
Intent GameLogSwitch = new Intent(getApplicationContext(),GameLog.class);
I am trying to put this to send to the other activity but I don't know if you can put variables in it. otherwise it works with simple words such as ("key","It works")
GameLogSwitch.putExtra("Break","---BREAK---"+"\n"+Player1Name+": "+GreenResult.getText()+"("+GrTop1+","+GrTop2+","+GrTop3+")"+"\n"+Player2Name+": "+RedResult.getText()+"("+RdTop1+","+RdTop2+","+RdTop3+")");
and then of course I have this when the gamelog button is pressed
startActivity(GameLogSwitch);
Now in Gamelog.class i have this.
package com.example.adam.snookerproject;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class GameLog extends AppCompatActivity {
private TextView GameLogText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_log);
GameLogText = (TextView) findViewById(R.id.GameLogText);
Intent GameLogSwitch = getIntent();
String myString = GameLogSwitch.getStringExtra("Break");
GameLogText.append(myString);
}
}
I have a couple of questions. First, why does append only work once with my string when I start the activity i.e when I go back and press the same button again it won't write the same thing again underneath?
Secondly, it doesn't seem to work for my "Break" key, does this have to do with the fact that there are variables in the text I'm sending? It only works for simple text like GameLogSwitch.putExtra("key","It works");
There must be an easier way to do this! Thank you.
UPDATE 1: The answer from Drv does seem to work, but when I try to do GameLogText.append(AppConstants.log) it just replaces everything in the textview no matter how many times I press the button. I think the activity is just resetting each time I start it again. Any way around this?
Make a global string in Constants class and use it wherever you want:
public class AppConstants{
public static String log="";
}
Edit the string in the class where you are sending it in intent:
AppConstants.log="---BREAK---"+"\n"+Player1Name+": "+GreenResult.getText()+"("+GrTop1+","+GrTop2+","+GrTop3+")"+"\n"+Player2Name+": "+RedResult.getText()+"("+RdTop1+","+RdTop2+","+RdTop3+")";
And use it in your class as:
package com.example.adam.snookerproject;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class GameLog extends AppCompatActivity {
private TextView GameLogText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_log);
GameLogText = (TextView) findViewById(R.id.GameLogText);
//get your log here using AppConstants.log
}
}
Try to use getContext() instead of getApplicationContext, then check your string in debug mode
Use Callback or Interface. Fire the Interface from the Main Activity and Implement the call Interface in the Activity where you want the Textview to be Updated. Inside that method Update your Text View.
Read here More about Communication between Activity Activity and Fragment Activity
(Posted solution on behalf of the OP).
I managed to append using "\n"+AppConstants.log at the end of each output.
to the point, i am working in a library for android app's, all programing part is almost finish, i am testing a idea.
the idea is put some interfaces in the librery that i am working, and this GUI can be loaded in the main app.
i try to do it but i get to error the first was i did not declare the activity in the AndroidManifest in the main app, the second one is the one a i cant solve, the autogenerated Class R don't capture the GUI that it is in the librery.
it is anyway i can do this or it is imposible.
code to see what i am trying todo
in my librery (movilsecure)
EmisionActivity.java (have the activity_emision.xml in the res of my librery)
in the Main Android App (
import ve.com.idyseg.movilsecure.EmisionActivity;
import ve.com.idyseg.movilsecure.MSMasterControllerTEST;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
public void captureEvent (View v){
Intent intento = new Intent(this,EmisionActivity.class);
startIntent(intento);
}
}
In Eclipses, right click the project, the click Build Path then click Add Library. Please comment if you're still stuck.
In the library's manifest, in the <activity> node, did you set android:exported="true"?
Example:
<activity
android:name="com.example.app.EmisionActivity"
android:exported="true">
</activity>
Maybe it sounds silly, but I am getting an error on Button keyword (where it should not be).
I am a newbie, and looked almost everywhere. And everyone says that
Button b = findViewById(R.id.button1);
is correct.
My code:
package com.example.myfirstappnew;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = findViewById(R.id.button1);
}
#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;
}
}
Here is the screenshot:
http://pbrd.co/ZEsSw7
From what I see, you have an error because findViewById returns View and not Button, and you need to cast it to Button.
You need to cast the Button to a Button:
Button b = (Button) findViewById(R.id.button1);
And add this import:
import android.widget.Button;
Next time, when you say you have an error please include it :)
findViewById returns View, you need to cast it into Button.
Button b = (Button) findViewById(R.id.button1);
Edit:
Click on the first link that show import import Button(android.widget.)
or simply press ctrl+shift+o
The code needs to import Button (as suggested in the top hint in the screenshot).
First you need to change
Button b = findViewById(R.id.button1);
to
Button b = (Button) findViewById(R.id.button1);
You also need to add
import java.widget.Button;
to your import statements at the top of your file. If you are using Eclipse, you should use it's "organize imports" feature (or whatever Eclipse calls it) to do this automagically for you.
i am fairly new to android programming and usually find my answers to my problems by searching, but this one i just cant and its very confusing.
The code itself doesn't show any signs of problems, well i do get 2 java exception breakpoints but i dont know how to fix those as they are "unknown"but when i run it on the emulator it says the application has stopped unexpectedly force close. I try to debug it but i dont know how to do it that well. any way here are the codes btw the app is just a test all it to do is have buttons that take me to other "pages" and back. I would appreciate any help.
Main java file
package com.simbestia.original;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class original extends Activity implements View.OnClickListener {
Button button1, button2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button) findViewById(R.id.pagetwo);
button2 = (Button) findViewById(R.id.main);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.pagetwo:
setContentView(R.layout.pagetwo);
break;
case R.id.main:
setContentView(R.layout.main);
break;
}
}
}
Main xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<Button android:text="pagetwo" android:id="#+id/pagetwo" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
Well here is what i change the code to this one is just one button but it works with multiple and i made a class for every page...
package com.simbestia.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mainmenu = (Button) findViewById(R.id.mainmenu);
mainmenu.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), mainmenu.class);
startActivityForResult(myIntent, 0);
}
});
}
}
Works how i wanted to so its all good i guess ty again
In the command line/terminal, use ./adb logcat to see, in real time, warnings, erros and such from your device, while you run your app. That should help you a lot.
Note: Don't forget to be in the right folder... <android-sdk-version>/platform-tools, that's where the ADB is.
You should learn to debug your own application. Starting with a break point right in the first line of your onCreate() method.
You can also take a look here: http://www.droidnova.com/debugging-in-android-using-eclipse,541.html
Another possibility is to add a log call in the first line of your onCreate() so you can see where the log of your app starts...
edit:
The way you want to switch the layout is wrong. Try layout switcher or start a new activity for your new layout. calling setContentView more than once is basically just wrong...
Some things to check: make sure your code source folder is the same as your package name (com.simbestia.original) ; make sure it builds (without errors) before you try and run it and make sure that your manifest file has its package attribute set to your package name (com.simbestia.original).