无法启动活动ComponentInfo LogCat

时间:2014-08-25 00:39:29

标签: java android logcat android-logcat

我不知道这些错误是什么意思以及为什么应用程序无法启动:( 我在AndroidManifest .MyActivity中有正确的名字 包也一样。

logcat的:

08-24 23:38:55.605      370-370/com.example.shinobi.fatburnheartratecalc W/ActivityThread﹕ Application com.example.shinobi.fatburnheartratecalc is waiting for the debugger on port 8100...
08-24 23:38:55.615      370-370/com.example.shinobi.fatburnheartratecalc I/System.out﹕ Sending WAIT chunk
08-24 23:38:55.775      370-375/com.example.shinobi.fatburnheartratecalc I/dalvikvm﹕ Debugger is active
08-24 23:38:55.826      370-370/com.example.shinobi.fatburnheartratecalc I/System.out﹕ Debugger has connected
08-24 23:38:55.826      370-370/com.example.shinobi.fatburnheartratecalc I/System.out﹕ waiting for debugger to settle...
08-24 23:38:56.025      370-370/com.example.shinobi.fatburnheartratecalc I/System.out﹕ waiting for debugger to settle...
08-24 23:38:56.224      370-370/com.example.shinobi.fatburnheartratecalc I/System.out﹕ waiting for debugger to settle...
08-24 23:38:56.425      370-370/com.example.shinobi.fatburnheartratecalc I/System.out﹕ waiting for debugger to settle...
08-24 23:38:56.635      370-370/com.example.shinobi.fatburnheartratecalc I/System.out﹕ waiting for debugger to settle...
08-24 23:38:56.835      370-370/com.example.shinobi.fatburnheartratecalc I/System.out﹕ waiting for debugger to settle...
08-24 23:38:57.035      370-370/com.example.shinobi.fatburnheartratecalc I/System.out﹕ waiting for debugger to settle...
08-24 23:38:57.235      370-370/com.example.shinobi.fatburnheartratecalc I/System.out﹕ waiting for debugger to settle...
08-24 23:38:57.435      370-370/com.example.shinobi.fatburnheartratecalc I/System.out﹕ waiting for debugger to settle...
08-24 23:38:57.645      370-370/com.example.shinobi.fatburnheartratecalc I/System.out﹕ waiting for debugger to settle...
08-24 23:38:57.845      370-370/com.example.shinobi.fatburnheartratecalc I/System.out﹕ debugger has settled (1449)
08-24 23:38:58.805      370-370/com.example.shinobi.fatburnheartratecalc D/AndroidRuntime﹕ Shutting down VM
08-24 23:38:58.815      370-370/com.example.shinobi.fatburnheartratecalc W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-24 23:38:58.875      370-370/com.example.shinobi.fatburnheartratecalc E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.shinobi.fatburnheartratecalc/com.example.shinobi.fatburnheartratecalc.MyActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
            at android.app.ActivityThread.access$1500(ActivityThread.java:117)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:123)
            at android.app.ActivityThread.main(ActivityThread.java:3683)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at android.app.Activity.findViewById(Activity.java:1647)
            at com.example.shinobi.fatburnheartratecalc.MyActivity.<init>(MyActivity.java:22)
            at java.lang.Class.newInstanceImpl(Native Method)
            at java.lang.Class.newInstance(Class.java:1409)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
            at android.app.ActivityThread.access$1500(ActivityThread.java:117)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:123)
            at android.app.ActivityThread.main(ActivityThread.java:3683)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)
08-24 23:39:02.155      370-370/com.example.shinobi.fatburnheartratecalc I/Process﹕ Sending signal. PID: 370 SIG: 9
device not found

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.shinobi.fatburnheartratecalc" >

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

activity_my.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_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=".MyActivity">

    <TabHost
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/tabHost"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"></TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <LinearLayout
                    android:id="@+id/Calc"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical">

                    <EditText
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:inputType="number"
                        android:ems="10"
                        android:id="@+id/inWiek"
                        android:layout_gravity="center_horizontal"
                        android:layout_marginTop="50dp"
                        android:hint="Your Age" />

                    <EditText
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:inputType="number"
                        android:ems="10"
                        android:id="@+id/inTspocz"
                        android:layout_marginTop="25dp"
                        android:hint="Your resting heart rate   (Hint)" />

                    <Button
                        android:layout_width="100dp"
                        android:layout_height="50dp"
                        android:id="@+id/Btn"
                        android:layout_gravity="center_horizontal"
                        android:layout_marginTop="40dp"
                        android:hint="Do it" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/Hints"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">

                    </LinearLayout>

            </FrameLayout>


    </LinearLayout>
    </TabHost>
</LinearLayout>

MyActivity.java

package com.example.shinobi.fatburnheartratecalc;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.Toast;




public class MyActivity extends ActionBarActivity {


    EditText wiekEditText = (EditText) findViewById(R.id.inWiek);
    EditText tspoczEditText = (EditText) findViewById(R.id.inTspocz);

    int Wynik; 


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);


        int Tmax, RT;
        int wiek = Integer.parseInt(wiekEditText.getText().toString());
        int tspocz = Integer.parseInt(tspoczEditText.getText().toString());


        Tmax = 220 - wiek;
        RT = Tmax - tspocz;
        Wynik = 70*RT/100 + tspocz;



        TabHost tabHost = (TabHost) findViewById(R.id.tabHost);

        tabHost.setup();    

        TabHost.TabSpec tabSpec = tabHost.newTabSpec("Calc");  
        tabSpec.setContent(R.id.Calc);
        tabSpec.setIndicator("Calc");
        tabHost.addTab(tabSpec);

        tabSpec = tabHost.newTabSpec("Hints");                    
        tabSpec.setContent(R.id.Hints);
        tabSpec.setIndicator("Hints");
        tabHost.addTab(tabSpec);



        final Button Btn = (Button) findViewById(R.id.Btn);
        Btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"blablabla"+ Wynik,Toast.LENGTH_SHORT).show();

            }
        });




        wiekEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                Btn.setEnabled(!(wiekEditText.getText().toString().trim().isEmpty()));
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        }); {

        }


    }


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

1 个答案:

答案 0 :(得分:1)

你的问题是:

EditText wiekEditText = (EditText) findViewById(R.id.inWiek);
EditText tspoczEditText = (EditText) findViewById(R.id.inTspocz);  

您在布局被夸大之前找到了视图,因此NullPointerException

onCreate()后的setContentView()内移动此内容。