(Solved) Thanks to NetCat: it was a UI XML problem :)
Today I have started Android development, i'm using a Mac with Eclipse and the Android SDK install and all working (I have successfully managed to get working a few "Hello World" type apps working) and for the Android device i'm using my new HTC Incredible S.
So the code below should work and their are no error is the debugger when I run it on my phone, but each time I do before it even loads, the phone pops up a message saying "The application Count (process com.count) has stopped unexpectedly. Please try agin."
I have re created the project several times with different SDK versions 1.5 and 2.2 but still no luck.
The code is from another tutorial I successfully worked through but i have changed some of the variables to make a slightly different app. Can you tell me what is wrong with the following code:
package com.count;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class count extends Activity implements OnClickListener {
//Declare widgets
Button btnSave, btnUp, btnDown;
TextView lblCurrentCount, lblCountCard;
//Declare variables
int intCount=0;
int intCardCount=0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Sets up the link between java and the XML widgets
btnSave = (Button)findViewById(R.id.btnSave);
btnUp = (Button)findViewById(R.id.btnUp);
btnDown = (Button)findViewById(R.id.btnDown);
lblCurrentCount = (TextView)findViewById(R.id.lblCurrentCount);
lblCountCard = (TextView)findViewById(R.id.lblCountCard);
//Initialize widgets
lblCurrentCount.setText(String.valueOf(intCount));
lblCountCard.setText("");
//Define button listeners
btnSave.setOnClickListener(this);
btnUp.setOnClickListener(this);
btnDown.setOnClickListener(this);
}
//When any button is clicked
#Override
public void onClick(View src) {
//Actions when buttons are clicked
switch(src.getId()) {
case R.id.btnSave:
lblCountCard.append("\n#" + intCardCount + " " + intCount);
intCardCount++;
intCount=0;
lblCountCard.setText(String.valueOf(intCount));
break;
case R.id.btnUp:
intCount++;
lblCountCard.setText(String.valueOf(intCount));
break;
case R.id.btnDown:
intCount--;
lblCountCard.setText(String.valueOf(intCount));
break;
}
}
}
Thanks Dave
You must supply a layout_width
attribute.
That's probably it. You've got a layout element without a layout_width (and probably, if you make the same kinds of mistakes I do, a layout_height) attribute. Find it, add the attributes, and see what comes next.
Related
I have tried to install OpenCV for Java-App Development in Android Studio. The app starts on the emulator and tried to load but instantly shuts down. This happens when the app encounter anything related to OpenCV in the code (excluding imports).
This is not the entire project, if you want to compile and need everything drop a comment
package com.example.cse535a1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.content.Context;
import android.view.View;
import android.hardware.*;
import android.widget.FrameLayout;
import org.opencv.core.*;
public class MainActivity extends AppCompatActivity {
Camera c;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (!(getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA))) {
this.finish();
System.exit(0);
}
// System.loadLibrary(Core.NATIVE_LIBRARY_NAME); ---------------------------------CRASHES HERE
// Mat mat = Mat.eye(3, 3, CvType.CV_8UC1);
// Log.i("OPENCV", mat.dump());
Button button_symptoms = (Button)findViewById(R.id.button_symptoms);
Button button_upload_signs = (Button)findViewById(R.id.button_upload_signs);
Button button_measure_heart_rate = (Button)findViewById(R.id.button_measure_heart_rate);
Button button_measure_respiratory_rate = (Button)findViewById(R.id.button_measure_respiratory_rate);
c = getcam();
CameraView cv1 = new CameraView(getApplicationContext(), c);
FrameLayout view_camera = (FrameLayout)findViewById(R.id.view_camera);
view_camera.addView(cv1);
button_symptoms.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg_view) {
Intent intent = new Intent(getApplicationContext(), Loggin_symptoms.class);
startActivity(intent);
}
});
button_upload_signs.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg_view) {
}
});
button_measure_heart_rate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg_view) {
}
});
button_measure_respiratory_rate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg_view) {
SensorManager manager_sensor = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Sensor sensor_accelerometer = manager_sensor.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
});
}
I have previously done some research before posting on StackOverflow and I found this->
Android crashes when using OpenCV from activity
This has led me to the install instructions on-> Installing OpenCV 2.4.13.7. The necessary part can be found under Application Development with Static Initialization.
But unfortunately, the entire process of having an Android.mk file is no longer in use. So to solve this problem what do I do now?
Is there a different file where the OPENCV_INSTALL_MODULES:=on line must be inserted?
So, I think I have found the solution to this problem in my case.
Upon looking in the Logcat tab in Android studio, I noticed a problem that libopencv_java451.so was not found. In the jniLibs folder there was instead libopencv_java4.so.
A simple rename fixed this problem. There are two places where I renamed the files.
jniLibs folder in the project directory
jniLibs in the opencv sdk directory (though this one might be ultimately responsible)
Depending on the system (for me-> x86 and x86_64), rename your files.
I have attached a pic showing my project file structure, look at the large yellow marks which show the renamed files.
I'm working on a simple number guessing game. At the moment I have it set that when you guess the correct number it takes you to WinActivity.java. All I want it to do on that screen is say "You won" and to play a little trumpet victory sound (and also display a button that'll bring them to the GameActivity if they want to play again).
The trouble is, when I try bringing in a MediaPlayer the app doesn't even start.
Here is my code for my WinActivity that contains the MediaPlayer.
package com.example.jeremy.numberguessinggame;
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class WinActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_win);
MediaPlayer trumpetsound = MediaPlayer.create(this,R.raw.trumpet);
trumpetsound.start();
Button btnPlayAgain = (Button) findViewById(R.id.button2);
TextView youWon = (TextView) findViewById(R.id.textViewYouWon);
btnPlayAgain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent playAgainIntent = new Intent(WinActivity.this,GameActivity.class);
startActivity(playAgainIntent);
}
});
}
}
When I run the app the Messages Gradle Build window will pop up and says "error: cannot find symbol variable raw".
I added the raw folder to the res location, and was able to drag the sounds into the folder no problem.
I just don't see why I can't make this MediaPlayer work.
I would love some help. If you need more information I'll be glad to give it to you.
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.
Hi I have been racking my brain for hours, did research online but nobody seems to have an answer. My emulator was running my code no problem then I ran it again and I get "Session 'MainActivity': error". I looked through this main activity about 10 times but there is no error sign anywhere and it looks like it should be working fine, I mean it was working before no problem. So I'm not sure if there really is a problem I don't see that Android Studio is not pointing out properly or if this is a different problem all together.
Any help would be greatly appreciated. Thank you.
package tekvision.codedecrypter;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
import gameInfo.GameDatabase;
public class MainActivity extends ActionBarActivity {
//Runs before the application is created
private Button mCampaignButton;
private final Context context = this;
//When the application is created
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Instantiate a GameDatabase object (this activity)
final GameDatabase gDB = new GameDatabase(context);
gDB.fillGameDatabase();
//Keeps screen on so it doesn't fall asleep
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//Finding button by button id after application is created
mCampaignButton = (Button)findViewById(R.id.campaignButtonID);
//Checks if the campaign button is clicked
mCampaignButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String ans = gDB.getAnswer("Ancient",1);
//Toast pop up message
Toast toast = Toast.makeText(getApplicationContext(),
ans ,
Toast.LENGTH_SHORT);
toast.show();
//Intent to go from main activity to campaign Level Select Activity
final Intent intent = new Intent(MainActivity.this, CampaignSelectLevel.class);
startActivity(intent);
}
});
}
}
Try rebuilding and cleaning your project.
Build > Rebuild Project
and
Build > Clean Project
I recently started the learning process of creating android apps, I am using IntelliJ IDEA 13.1.1 and I am following this tutorial of adding action bar buttons and I am getting the error cannot resolve symbol navutils. I have searched online and came across couple of stackoverflow posts (1 and 2) and in those post they seem to be talking about fixing the problem via build.gradle which i have no idea what it is and cant seem to find this file anywhere.
here is my DisplayMessageActivity.java code, so far the app seems to be working just fine, i am able to post the text and see it but this action bar button is driving me crazy.
package com.example.My_First_App;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the message from the intent
Intent intent = getIntent();
String received_message = intent.getStringExtra(MyActivity.SOME_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(received_message);
// Set the text view as the activity layout
setContentView(textView);
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
I will really appreciate any help in this matter as i am stuck and cant move forward with the tutorial.
I think you are missing
import android.support.v4.app.NavUtils;
I don't see you importing NavUtils anywhere.
Add import android.support.v4.app.NavUtils; to your imports.