有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法启动活动ComponentInfo LogCat

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

日志:

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 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
            at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
            at 安卓.app.ActivityThread.access$1500(ActivityThread.java:117)
            at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
            at 安卓.os.Handler.dispatchMessage(Handler.java:99)
            at 安卓.os.Looper.loop(Looper.java:123)
            at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at 安卓.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 安卓.app.Instrumentation.newActivity(Instrumentation.java:1021)
            at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
            at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
            at 安卓.app.ActivityThread.access$1500(ActivityThread.java:117)
            at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
            at 安卓.os.Handler.dispatchMessage(Handler.java:99)
            at 安卓.os.Looper.loop(Looper.java:123)
            at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.安卓.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:安卓="http://schemas.安卓.com/apk/res/安卓"
    package="com.example.shinobi.fatburnheartratecalc" >

    <application
        安卓:allowBackup="true"
        安卓:icon="@drawable/ic_launcher"
        安卓:label="@string/app_name"
        安卓:theme="@style/AppTheme" >
        <activity
            安卓:name=".MyActivity"
            安卓:label="@string/app_name" >
            <intent-filter>
                <action 安卓:name="安卓.intent.action.MAIN" />

                <category 安卓:name="安卓.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我的活动。xml

<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="fill_parent"
    安卓:layout_height="fill_parent"
    安卓:paddingLeft="@dimen/activity_horizontal_margin"
    安卓:paddingRight="@dimen/activity_horizontal_margin"
    安卓:paddingTop="@dimen/activity_vertical_margin"
    安卓:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MyActivity">

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

        <LinearLayout
            安卓:layout_width="fill_parent"
            安卓:layout_height="fill_parent"
            安卓:orientation="vertical">

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

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

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

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

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

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

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

                    </LinearLayout>

            </FrameLayout>


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

我的活动。爪哇

package com.example.shinobi.fatburnheartratecalc;

import 安卓.support.v7.app.ActionBarActivity;
import 安卓.os.Bundle;
import 安卓.text.Editable;
import 安卓.text.TextWatcher;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.widget.TabHost;
import 安卓.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) 个答案

  1. # 1 楼答案

    你的问题是:

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

    您正在查找布局膨胀之前的视图,因此NullPointerException

    setContentView()之后的onCreate()中移动此项