0

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>
Cœur
  • 37,241
  • 25
  • 195
  • 267
shubham
  • 9
  • 5

2 Answers2

0

There could be many reasons, You have to be sure that:

  1. 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.

  2. You gave Your RelativeLayout the same Id like the button:

    android:id="@+id/button1"
    

    The best way would be, give both another, different id.

  3. I don´t know why You are using String.format(), just use tb.setText("Hi");

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
0

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.