简体   繁体   中英

When I am running my first android app using Genymotion emulator its giving error unfortunately “app name” has stopped. Please help me

I am giving the details of all my files below.Please check them and let me know where I am doing the mistake due to which the error that the "unfortunately "app name" has stopped" error is coming when I am running my first android app using android SDK. Since I am a beginner in android development Please explain the solution nicely.

My MainActivity.java file :

package com.example.sunny.myfirstapp;
import android.app.Activity;
import android.os.Bundle;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;


public class MainActivity extends Activity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView=(TextView)findViewById(R.id.greetings_view);
}
public void showGreetings(View view)
{
    String message= "welcome to my app";
    textView.setText(message);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

My activity_main.xml file is as follows:

  <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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context=".MainActivity"
     android:id="@+id/greetings_view">
      <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="greetings here"
      android:id="@+id/greetings_view"

      />
     <Button
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="show greetings"
      android:layout_below="@+id/greetings_view"
      android:onClick="My button"
     android:id="@+id/greetings_view"
    />
  </RelativeLayout>

My AndroidManifest.xml file :

 <?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.sunny.myfirstapp"
   android:versionCode="1"
   android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="5"
    android:targetSdkVersion="22" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.sunny.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>
</application>

My logcat :

        06-26 10:37:23.137  31265-31265/? D/dalvikvm﹕ Late-enabling CheckJNI
        06-26 10:37:24.013  31265-31265/com.example.sunny.myfirstapp D/AndroidRuntime﹕ Shutting down VM
        06-26 10:37:24.021  31265-31265/com.example.sunny.myfirstapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa62be288)
        06-26 10:37:24.025  31265-31265/com.example.sunny.myfirstapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sunny.myfirstapp/com.example.sunny.myfirstapp.MainActivity}: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView

all your views have the same ID greetings_view , elements must have no ID (blank) or different IDs.

ps.: in the future, the relevant log cat is the Stacktrace, is the part that have the direct message related to your crash. The log you posted is just some random stuff from your phone.

just try like this , it will Work

 package com.example.sunny.myfirstapp;

    public class MainActivity extends Activity {
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=(TextView)findViewById(R.id.greetings_view);

        String message= "welcome to my app";
        textView.setText(message);
    }
    }

Remove from android:id="@+id/greetings_view" Relative Layout , You given that id in Two places TextvieW and Relative Layout

simple textview 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:id="@+id/greetings_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM