android: ошибка при запуске приложения на эмуляторе затмения -"к сожалению, edvi(имя проекта) остановилось"

Я новичок в затмении, и я делаю небольшие проекты, чтобы открыть свою руку на Android. Каждый раз, когда я запускаю свой проект eclipse на эмуляторе, он отображает "к сожалению, edvi (имя проекта) остановилось"

Копия манифеста моего проекта:

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

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

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

COPY OF MAINACTIVITY.JAVA ФАЙЛА МОЕГО ПРОЕКТА:

package com.example.edvi;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Build;

import com.example.edvi.R;

public class MainActivity extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText eText;
        final Button btn;
        eText = (EditText) findViewById(R.id.editText1);
        btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v) 
            {
                String str = eText.getText().toString();
                Toast msg = Toast.makeText(getBaseContext(),str,
                Toast.LENGTH_LONG);
                msg.show();
                msg.show();
            }
        });
    }
    @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;
    }
}

Копия моего проекта LOGCAT:

05-26 01:57:34.831: D/AndroidRuntime(1166): Shutting down VM
05-26 01:57:34.831: W/dalvikvm(1166): threadid=1: thread exiting with uncaught exception (group=0xb2a8eba8)
05-26 01:57:34.871: E/AndroidRuntime(1166): FATAL EXCEPTION: main
05-26 01:57:34.871: E/AndroidRuntime(1166): Process: com.example.edvi, PID: 1166
05-26 01:57:34.871: E/AndroidRuntime(1166): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.edvi/com.example.edvi.MainActivity}: java.lang.NullPointerException
05-26 01:57:34.871: E/AndroidRuntime(1166):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-26 01:57:34.871: E/AndroidRuntime(1166):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-26 01:57:34.871: E/AndroidRuntime(1166):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-26 01:57:34.871: E/AndroidRuntime(1166):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-26 01:57:34.871: E/AndroidRuntime(1166):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-26 01:57:34.871: E/AndroidRuntime(1166):     at android.os.Looper.loop(Looper.java:136)
05-26 01:57:34.871: E/AndroidRuntime(1166):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-26 01:57:34.871: E/AndroidRuntime(1166):     at java.lang.reflect.Method.invokeNative(Native Method)
05-26 01:57:34.871: E/AndroidRuntime(1166):     at java.lang.reflect.Method.invoke(Method.java:515)
05-26 01:57:34.871: E/AndroidRuntime(1166):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-26 01:57:34.871: E/AndroidRuntime(1166):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-26 01:57:34.871: E/AndroidRuntime(1166):     at dalvik.system.NativeStart.main(Native Method)
05-26 01:57:34.871: E/AndroidRuntime(1166): Caused by: java.lang.NullPointerException
05-26 01:57:34.871: E/AndroidRuntime(1166):     at com.example.edvi.MainActivity.onCreate(MainActivity.java:32)
05-26 01:57:34.871: E/AndroidRuntime(1166):     at android.app.Activity.performCreate(Activity.java:5231)
05-26 01:57:34.871: E/AndroidRuntime(1166):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-26 01:57:34.871: E/AndroidRuntime(1166):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-26 01:57:34.871: E/AndroidRuntime(1166):     ... 11 more
05-26 01:58:12.301: D/AndroidRuntime(1206): Shutting down VM
05-26 01:58:12.301: W/dalvikvm(1206): threadid=1: thread exiting with uncaught exception (group=0xb2a8eba8)
05-26 01:58:12.331: E/AndroidRuntime(1206): FATAL EXCEPTION: main
05-26 01:58:12.331: E/AndroidRuntime(1206): Process: com.example.edvi, PID: 1206
05-26 01:58:12.331: E/AndroidRuntime(1206): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.edvi/com.example.edvi.MainActivity}: java.lang.NullPointerException
05-26 01:58:12.331: E/AndroidRuntime(1206):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-26 01:58:12.331: E/AndroidRuntime(1206):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-26 01:58:12.331: E/AndroidRuntime(1206):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-26 01:58:12.331: E/AndroidRuntime(1206):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-26 01:58:12.331: E/AndroidRuntime(1206):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-26 01:58:12.331: E/AndroidRuntime(1206):     at android.os.Looper.loop(Looper.java:136)
05-26 01:58:12.331: E/AndroidRuntime(1206):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-26 01:58:12.331: E/AndroidRuntime(1206):     at java.lang.reflect.Method.invokeNative(Native Method)
05-26 01:58:12.331: E/AndroidRuntime(1206):     at java.lang.reflect.Method.invoke(Method.java:515)
05-26 01:58:12.331: E/AndroidRuntime(1206):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-26 01:58:12.331: E/AndroidRuntime(1206):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-26 01:58:12.331: E/AndroidRuntime(1206):     at dalvik.system.NativeStart.main(Native Method)
05-26 01:58:12.331: E/AndroidRuntime(1206): Caused by: java.lang.NullPointerException
05-26 01:58:12.331: E/AndroidRuntime(1206):     at com.example.edvi.MainActivity.onCreate(MainActivity.java:32)
05-26 01:58:12.331: E/AndroidRuntime(1206):     at android.app.Activity.performCreate(Activity.java:5231)
05-26 01:58:12.331: E/AndroidRuntime(1206):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-26 01:58:12.331: E/AndroidRuntime(1206):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-26 01:58:12.331: E/AndroidRuntime(1206):     ... 11 more
05-26 01:58:16.341: I/Process(1206): Sending signal. PID: 1206 SIG: 9
05-26 01:58:42.821: D/AndroidRuntime(1245): Shutting down VM
05-26 01:58:42.821: W/dalvikvm(1245): threadid=1: thread exiting with uncaught exception (group=0xb2a8eba8)
05-26 01:58:42.851: E/AndroidRuntime(1245): FATAL EXCEPTION: main
05-26 01:58:42.851: E/AndroidRuntime(1245): Process: com.example.edvi, PID: 1245
05-26 01:58:42.851: E/AndroidRuntime(1245): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.edvi/com.example.edvi.MainActivity}: java.lang.NullPointerException
05-26 01:58:42.851: E/AndroidRuntime(1245):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-26 01:58:42.851: E/AndroidRuntime(1245):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-26 01:58:42.851: E/AndroidRuntime(1245):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-26 01:58:42.851: E/AndroidRuntime(1245):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-26 01:58:42.851: E/AndroidRuntime(1245):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-26 01:58:42.851: E/AndroidRuntime(1245):     at android.os.Looper.loop(Looper.java:136)
05-26 01:58:42.851: E/AndroidRuntime(1245):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-26 01:58:42.851: E/AndroidRuntime(1245):     at java.lang.reflect.Method.invokeNative(Native Method)
05-26 01:58:42.851: E/AndroidRuntime(1245):     at java.lang.reflect.Method.invoke(Method.java:515)
05-26 01:58:42.851: E/AndroidRuntime(1245):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-26 01:58:42.851: E/AndroidRuntime(1245):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-26 01:58:42.851: E/AndroidRuntime(1245):     at dalvik.system.NativeStart.main(Native Method)
05-26 01:58:42.851: E/AndroidRuntime(1245): Caused by: java.lang.NullPointerException
05-26 01:58:42.851: E/AndroidRuntime(1245):     at com.example.edvi.MainActivity.onCreate(MainActivity.java:32)
05-26 01:58:42.851: E/AndroidRuntime(1245):     at android.app.Activity.performCreate(Activity.java:5231)
05-26 01:58:42.851: E/AndroidRuntime(1245):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-26 01:58:42.851: E/AndroidRuntime(1245):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-26 01:58:42.851: E/AndroidRuntime(1245):     ... 11 more
05-26 01:58:47.891: I/Process(1245): Sending signal. PID: 1245 SIG: 9

Копия моего проекта FRAGMENT_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:background="@style/AppTheme"
    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.edvi.MainActivity$PlaceholderFragment" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="42dp"
        android:ems="10"
        android:hint="@string/enter_something_fix" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="47dp"
        android:text="@string/click_me_fix" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button1"
        android:layout_marginTop="56dp"
        android:text="@string/text_fix"
        android:textSize="40sp" />

</RelativeLayout>

2 ответа

Попробуй это..

Изменить это..

setContentView(R.layout.activity_main);

в

setContentView(R.layout.fragment_main);

потому что все Button, TextView, EditText в fragment_main.xml, Так setContentView следует ссылаться на макет fragment_main.xml не activity_main.xml

Проблема в том, что все ваши UI находятся в FRAGMENT_MAIN.XML файл, пока вы используете

 setContentView(R.layout.activity_main);

Поэтому каждый раз, когда вы получаете исключение нулевого указателя. Так что просто измените название вашего макета или поставьте все UI в activity_main.xml файл.

Другие вопросы по тегам