错误:无法实例化活动ComponentInfo

时间:2017-05-17 21:50:37

标签: xml

我是Android Studio中的新手,我有一点问题。当我按下按钮选择活动时,应用程序崩溃,并在“Android监视器”中显示:

05-17 21:46:25.309 10393-10393/bodo.odysseuscontest E/AndroidRuntime: FATAL EXCEPTION: main
                                                                  Process: bodo.odysseuscontest, PID: 10393
                                                                  java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{bodo.odysseuscontest/bodo.odysseuscontest.Activity3}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2567)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                      at android.os.Looper.loop(Looper.java:154)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                                                                      at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:120)
                                                                      at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:155)
                                                                      at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:31)
                                                                      at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:55)
                                                                      at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:33)
                                                                      at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:33)
                                                                      at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)
                                                                      at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
                                                                      at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
                                                                      at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
                                                                      at bodo.odysseuscontest.Activity3.<init>(Activity3.java:18)
                                                                      at java.lang.Class.newInstance(Native Method)
                                                                      at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2557)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                                      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                      at android.os.Looper.loop(Looper.java:154) 
                                                                      at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

这是.java代码:

package bodo.odysseuscontest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.OverScroller;
import android.widget.Spinner;
import android.widget.TextView;

public class Activity3 extends AppCompatActivity {

private TextView totalTextView;
private EditText valuetxt;

Spinner chooses= (Spinner) findViewById(R.id.spinner);

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

    ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(Activity3.this,
            android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.names));
    myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    chooses.setAdapter(myAdapter);

    Button calcbtn = (Button)findViewById(R.id.calcbtn);
    totalTextView = (TextView)findViewById(R.id.totalTextView);
    valuetxt = (EditText)findViewById(R.id.valuetxt);


    calcbtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            float value = Float.parseFloat(valuetxt.getText().toString());

            if (chooses.equals("Km")) {
                float total = value * 100;
                totalTextView.setText(Float.toString(total));
            }
        }
    });
}

}

这是android清单文件:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bodo.odysseuscontest">

<application
    android:allowBackup="true"
    android:background="@color/Background"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:screenOrientation="portrait"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Activity2"
        android:screenOrientation="portrait" />
    <activity
        android:name="Activity3"
        android:label="Conversion"
        android:parentActivityName=".Activity2"
        android:screenOrientation="portrait" />
    <activity android:name="Activity4"
        android:label="Rockets"
        android:parentActivityName=".Activity2"/>
    <activity android:name="Activity5"
        android:label="Missions"
        android:parentActivityName=".Activity2"/>
    <activity android:name="Activity6"
        android:label="Credits"
        android:parentActivityName=".Activity2"/>
</application>

1 个答案:

答案 0 :(得分:0)

在创建活动的上下文之前,您正在调用findViewById。

尝试改变这一点:

...
...
private TextView totalTextView;
private EditText valuetxt;

Spinner chooses= (Spinner) findViewById(R.id.spinner);

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

....
....

使用此代码:

...
...
private TextView totalTextView;
private EditText valuetxt;

Spinner chooses;

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

      chooses= (Spinner) findViewById(R.id.spinner);

....
....