[英]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. 我在下面提供所有文件的详细信息。请检查它们,并让我知道我在哪里做错了,由于我运行第一个android时出现了“不幸的是,“应用名称”已停止”错误使用android SDK的应用程序。 Since I am a beginner in android development Please explain the solution nicely. 由于我是android开发的初学者,请很好地说明解决方案。
My MainActivity.java file : 我的MainActivity.java文件:
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: 我的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: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 : 我的AndroidManifest.xml文件:
<?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 : 我的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. 您所有的视图都具有相同的ID greetings_view
,元素必须没有ID(空白)或不同的ID。
ps.: in the future, the relevant log cat is the Stacktrace, is the part that have the direct message related to your crash. ps .:将来,相关的日志目录是Stacktrace,是具有与崩溃相关的直接消息的部分。 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 从android:id =“ @ + id / greetings_view”相对布局中删除,您在两个位置TextvieW和Relative Layout中指定了ID
simple textview xml 简单的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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.