Whenever i try to make any app even simple hello world app every time i get this type of error, even in android virtual devices and android phone also, so i want to know that what are mistakes that i made every time in made, and can any one please make this code correct. So that i can run this app on my mobile phone.
this is my MainActivity_hello.java file
package com.example.helloapp1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity_hello extends Activity {
TextView tb;
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity_hello);
addListenerOnButton();
tb= (TextView) findViewById(R.id.Tbox);
}
private void addListenerOnButton() {
// TODO Auto-generated method stub
b = (Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
tb.setText( String.format( " Hi ") );
}
});
}
#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_activity_hello, menu);
return true;
}
}
activity_main_activity_hello.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity_hello" >
<TextView
android:id="#+id/Tbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Tbox"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:text="Hit Me" />
</RelativeLayout>
Androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloapp1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.helloapp1.MainActivity_hello"
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>
</manifest>
There could be many reasons, You have to be sure that:
The R. import is not R.android, which references to the Android System Resources. If it is, Your Button b is referred to the system framework button. This System Framework Button is identified with "button1".
Well, in Your case, I think You have the wright import, because You have no problem by using the id for the textView.
You gave Your RelativeLayout the same Id like the button:
android:id="#+id/button1"
The best way would be, give both another, different id.
I donĀ“t know why You are using String.format(), just use tb.setText("Hi");
You are declaring the same id button1 twice, once in the layout and once for the button.
Change it to something else for your layout, like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/holder1"
...
>
Since it seems like you are getting started with Android development I would recommend switching over to using Android Studio.
Related
I'm trying to deploy my first test app, but it's giving me a Parse Error - There was a problem while parsing the package.
Whilst, it executes fine on my Genymotion virtual device. It's the normal first "Hello World" app you'd get automatically.
Also, it executes on both virtual devices of Samsung Galaxy S3 and Samsung Galaxy S5 but not on real devices such as Samsung Galaxy S5 and Samsung Galaxy Note 10.1.
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lovely"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-sdk
android:minSdkVersion="3"
android:targetSdkVersion="24" />
<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>
</application>
</manifest>
Main activity:
package com.example.lovely;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.lovely.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
Appreciate any assistance, thank you.
Exporting as signed rather than unsigned fixed it. Not sure why, my phone and tablet both accept " unknown sources " feel free to explain why this has hapenned
I started off by trying to make a simple app on Eclipse.
the target for me was to create a button which simply calls a number.
however, after many tutorials and such, I still couldn't figure out where to write the code and what code was the best.
here is my Manifest . I inserted uses permission CALL_PHONE since it is required (As I learned from some tutorials)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.projectrandomfox.randomfox"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.projectrandomfox.randomfox.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>
here is my activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select VAS"
android:textSize="40dp"
android:layout_gravity="center" />
<Button
android:id="#+id/balance"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Check Balance"
android:layout_gravity="center"
/> </LinearLayout>
here is my activity java
package com.projectrandomfox.randomfox;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
THATS ALL.
please help me figure out where to write code in activity, and what code to write
it would mean a lot to me :)
Add this inside your onCreate method:
Button button = (Button) findViewById(R.id.balance);
myButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + telephoneNumber));
startActivity(intent);
}
}
All you need to do is set the telephoneNumber variable
First you have to get a reference your Button.
Button myButton = findViewById(R.id.balance);
Then add the onClickListener.
myButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
// Do whatever
}
}
I am a beginner in Android, and I need some advice/help with the following issue as I'm doing an Android tutorial http://developer.android.com/training/basics/firstapp/starting-activity.html#AddToManifest. I got a little problem here when running the app it displays a BLANK ACTIVITY of myfirstapp (both on emulator and my Samsung Galaxy S2) - it doesn't show up anything on the app - no text field, no button.
MainActivity.java:
package com.example.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE ="com.example.myfirstapp.MESSAGE";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
#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;
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="true"
android:orientation="horizontal" >
<EditText
android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
activity_display_message.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".DisplayMessageActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.myfirstapp.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=".DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
</manifest>
However when I remove this particular piece of code from my main activity:
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
It will run an app as normally showing everything although it won't process the text entered. Once you click on the button it will only show up a new activity with Hello World. Any ideas where the problem occurs?
I hope you are aware that in the tutorial two classes are used - MainActivity and DisplayMessageActivity - it seems you've merged them into one class accidentally.
The following code should be in the onCreate method of DisplayMessageActivity.java and not in MainActivity.java
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
Are you getting anything in the message variable. If it is empty then nothing will be set in the setText(message) and hence nothing will be displayed.
Okay, this is really ticking me off. I ran it on both my emulator and on an android device. The code does NOT display "Helloworld, Android -mykong.com". I start the app, find it on the emulator, click on it, and it goes to interface of the app. However, it is just a blank screen! This error is not an issue of me finding the app, its not with the emulator or the android phone, the issue has to lie either in the code or in the way the code is structured/built. I know the the helloworld code is 100% correct because its from a reputable tutorial site. These are my three pairs of code. Please help, I am in desperate need!
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test123"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.test123.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>
</manifest>
MainActivity.java
package com.example.test123;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void activity()
{
Intent helloWorld = new Intent(getApplicationContext(), HelloWorldjavaactivity.class );
startActivity( helloWorld );
}
#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;
}
}
HelloWorldjavaactivity.java
package com.example.test123;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloWorldjavaactivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView text = new TextView(this);
text.setText("Hello World, Android - mkyong.com");
setContentView(text);
}
}
Activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="35dp"
android:layout_marginTop="57dp"
android:text=""
/>
</RelativeLayout>
You're not starting the HelloWorldjavaactivity.java activity. You need something like the following in your MainActivity:
Intent helloWorld = new Intent(getApplicationContext(), HelloWorldjavaactivity.class );
startActivity( helloWorld );
Of course, you need that code to be triggered by some even, a button press for example.
It would be easier to just eliminate the second activity (since your MainActivity isn't doing anything anyway) and put the code in your MainActivity instead. You'll also need to edit the layout for your MainActivity so that it includes the appropriate views.
I tried to run first example from "Beginning Android Games" book (by Mario Zechner, Apress.com):
I copied this code to my project (MainActivity.java file):
HelloWorldActivity.java
package com.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements View.OnClickListener {
Button button;
int touchCount;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
button = new Button(this);
button.setText( "Touch me!" );
button.setOnClickListener(this);
setContentView(button);
}
public void onClick(View v) {
touchCount++;
button.setText("Touched me " + touchCount + " time(s)");
}
}
but when I have run this simple application, I didn't see any button on the emulator (I have seen only an "Android" label. I tried "project-->clean..": no result. (and "project-->Build Automatically is swiched on). Where do I need to declare and describe this button? But I followed all steps described in the book. And at the end, I removed xml-file from layout folder at all.
Thanks.
________________Edited: Added xml-files___________
res-->layout-->activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
</RelativeLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.firstbuttontest2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="4"
android:targetSdkVersion="11" />
<application
android:allowBackup="true"
android:debuggable="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.firstbuttontest2.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>
</manifest>
Try this it might help you.
Add button to particular layout. Like this
LinearLayout linearLayout=(LinearLayout)findViewById(R.id.linearLayout);
linearLayout.addView(button);
I know that there is a addContentView method which may be more what you are looking for as opposed to setContentView.
Try
setContentView(R.layout.activity_main)
then
button = (Button) findviewbyid(R.id.Button1);
this may solve your problem