奇怪的错误 java.lang.reflect.Method.invokeNative(Native Method) Android Studio 请回答

Posted

技术标签:

【中文标题】奇怪的错误 java.lang.reflect.Method.invokeNative(Native Method) Android Studio 请回答【英文标题】:Strange error java.lang.reflect.Method.invokeNative(Native Method) Android Studio please answer 【发布时间】:2017-04-18 23:30:45 【问题描述】:

我正在为我的学校开发一个带有时间表和小计算器的应用程序

这是 manifest.xml:

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

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:launchMode="singleTop"
    android:minSdkVersion="20"
    android:supportsRtl="true"
    android:targetSdkVersion="25"
    android:theme="@style/AppTheme">
    <activity android:name=".Stundenplan">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Vertretungsplan" />
    <activity android:name=".Rechner" />
    <activity android:name=".Overlay"></activity>
</application>

这是启动器活动 activity_stundenplan.xml:

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    android:background="@color/white">

    <Button
        android:text="Rechner"
        android:layout_
        android:layout_
        android:id="@+id/buttonrechner"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="44dp"
        android:layout_marginStart="44dp" />

    <Button
        android:layout_
        android:layout_
        android:id="@+id/buttonvertr"
        android:text="Vertretungsplan"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/Tabelle"
        android:layout_alignEnd="@+id/Tabelle" />

<TableLayout
    android:id="@+id/Tabelle"
    android:layout_
    android:layout_
    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.markwitt.schul_app.Stundenplan"
    android:background="@color/white"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true">


        <TableRow
            android:layout_
            android:layout_>

            <TextView
                android:text="@string/r10"
                android:padding="10dp"
                android:background="@drawable/cell_shape"
                android:textStyle="normal|bold"
                android:layout_
                android:layout_ />

            <TextView
                android:text="@string/r20"
                android:padding="10dp"
                android:background="@drawable/cell_shape"
                android:textStyle="normal|bold"
                android:layout_
                android:layout_ />

            <TextView
                android:text="@string/r30"
                android:padding="10dp"
                android:background="@drawable/cell_shape"
                android:textStyle="normal|bold"
                android:layout_
                android:layout_ />

            <TextView
                android:text="@string/r40"
                android:padding="10dp"
                android:background="@drawable/cell_shape"
                android:textStyle="normal|bold"
                android:layout_
                android:layout_ />

            <TextView
                android:text="@string/r50"
                android:padding="10dp"
                android:background="@drawable/cell_shape"
                android:textStyle="normal|bold"
                android:layout_
                android:layout_ />

            <TextView
                android:text="@string/r60"
                android:padding="10dp"
                android:background="@drawable/cell_shape"
                android:textStyle="normal|bold"
                android:layout_
                android:layout_ />

        </TableRow>
        <TableRow
            android:layout_
            android:layout_>

            <TextView
                android:text="@string/r11"
                android:padding="10dp"
                android:background="@drawable/cell_shape"
                android:textStyle="normal|bold"
                android:layout_
                android:layout_ />
            <TextView android:text="@string/r21" android:padding="10dp"
                android:background="@drawable/cell_shape"
                android:layout_
                android:layout_ />

            <TextView
                android:text="@string/r31"
                android:padding="10dp"
                android:background="@drawable/cell_shape"
                android:layout_
                android:layout_ />

            <TextView
                android:text="@string/r41"
                android:padding="10dp"
                android:background="@drawable/cell_shape"
                android:layout_
                android:layout_ />

            <TextView
                android:text="@string/r51"
                android:padding="10dp"
                android:background="@drawable/cell_shape"
                android:layout_
                android:layout_ />

            <TextView
                android:text="@string/r61"
                android:padding="10dp"
                android:background="@drawable/cell_shape"
                android:layout_
                android:layout_ />
        </TableRow>
        <TableRow
            android:layout_
            android:layout_>

            <TextView
                android:text="@string/r12"
                android:padding="10dp"
                android:background="@drawable/cell_shape"
                android:textStyle="normal|bold"
                android:layout_
                android:layout_ />

            <TextView
                android:text="@string/r22"
                android:padding="10dp"
                android:background="@drawable/cell_shape"
                android:layout_
                android:layout_ />

            <TextView
                android:text="@string/r32"
                android:padding="10dp"
                android:background="@drawable/cell_shape"
                android:layout_
                android:layout_ />

            <TextView
                android:text="@string/r42"
                android:padding="10dp"
                android:background="@drawable/cell_shape"
                android:layout_
                android:layout_ />

            <TextView
                android:text="@string/r52"
                android:padding="10dp"
                android:background="@drawable/cell_shape"
                android:layout_
                android:layout_ />

            <TextView
                android:text="@string/r62"
                android:padding="10dp"
                android:background="@drawable/cell_shape"
            //more TableRows
            </TableLayout>
            </Relativelayout>

我在 Stundenplan.java 中创建了一个 Intent:

rechner.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            startActivity(new Intent(Stundenplan.this, Rechner.class));
        
    );

问题是每次我在 Xperia Z3 上调试启动应用程序并按下按钮打开“Rechner”-Activity 时,应用程序都会关闭并显示以下错误:(抱歉格式错误)

I/OpenGLRenderer: Initialized EGL, version 1.4
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@f97be6e time:6061206
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@f97be6e time:6065998
I/Timeline: Timeline: Activity_launch_request id:com.example.markwitt.schul_app time:6074313
D/AndroidRuntime: Shutting down VM


                  --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.markwitt.schul_app, PID: 22562
                  java.lang.RuntimeException: Unable to instantiate activity ComponentInfocom.example.markwitt.schul_app/com.example.markwitt.schul_app.Rechner: 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:2375)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
                      at android.app.ActivityThread.access$900(ActivityThread.java:154)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:224)
                      at android.app.ActivityThread.main(ActivityThread.java:5526)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                   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:116)
                      at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:147)
                      at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:27)
                      at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:53)
                      at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:29)
                      at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:203)
                      at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
                      at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:525)
                      at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:193)
                      at com.example.markwitt.schul_app.Rechner.<init>(Rechner.java:16)
                      at java.lang.Class.newInstance(Native Method)
                      at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2365)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524) 
                      at android.app.ActivityThread.access$900(ActivityThread.java:154) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:224) 
                      at android.app.ActivityThread.main(ActivityThread.java:5526) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
I/Process: Sending signal. PID: 22562 SIG: 9
Disconnected from the target VM, address: 'localhost:8600', transport: 'socket'

我的 Rechner.java 和我的 activity_rechner 看起来像这样:

package com.example.markwitt.schul_app;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class Rechner extends AppCompatActivity 
private RadioGroup radioGroup;
private RadioButton checked;
private Button los;
public String mode;
EditText number1 = (EditText) findViewById(R.id.nummer1);
EditText number2 = (EditText) findViewById(R.id.nummer2);
EditText ergebnis = (EditText) findViewById(R.id.ergebnis);
public int text1;
@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rechner);

    addListenerOnButton();


public void addListenerOnButton() 
    radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
    los = (Button) findViewById(R.id.button5);

    los.setOnClickListener(new View.OnClickListener() 

        @Override
        public void onClick(View v) 

            int selectedId = radioGroup.getCheckedRadioButtonId();

            checked = (RadioButton) findViewById(selectedId);

            switch (checked.getId())
                case R.id.plus: mode = "+";
                case R.id.minus: mode = "-";
                case R.id.mal: mode = "*";
                case R.id.geteilt: mode = ":";
                case R.id.fakultaet: mode = "fak";

            
            text1 = Integer.parseInt(number1.getText().toString());
            int text2 = Integer.parseInt(number2.getText().toString());
            switch (mode)
                case "+": ergebnis.setText(text1 + text2);
                case "-": ergebnis.setText(text1 - text2);
                case "*": ergebnis.setText(text1 * text2);
                case ":": ergebnis.setText(text1 / text2);
                case "fak": ergebnis.setText(Long.toString(fakultaet(text1)));
            

        

    );





static long fakultaet(int n) 
    int ergebnis = 1;
    for (int i = 1; i <= n; i++) 
        ergebnis = ergebnis * i;
    
    return ergebnis;

...

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_durchschnitt"
android:layout_
android:layout_
tools:context="com.example.markwitt.schul_app.Rechner">

<EditText
    android:id="@+id/nummer1"
    android:layout_
    android:layout_
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="12dp"
    android:layout_weight="1"
    android:ems="10"
    android:hint="Nummer 1"
    android:inputType="number"
    android:maxLines="1"
    android:textAlignment="center" />

<Button
    android:id="@+id/button5"
    android:layout_
    android:layout_
    android:layout_above="@+id/ergebnis"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="22dp"
    android:text="=" />

<RadioGroup
    android:id="@+id/radioGroup"
    android:layout_
    android:layout_
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/nummer1"
    android:layout_marginEnd="42dp"
    android:layout_marginRight="42dp"
    android:layout_marginTop="69dp"
    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/mal"
        android:layout_
        android:layout_
        android:layout_alignBaseline="@+id/geteilt"
        android:layout_alignBottom="@+id/geteilt"
        android:layout_toLeftOf="@+id/geteilt"
        android:layout_toStartOf="@+id/geteilt"
        android:layout_weight="1"
        android:text="* "
        android:textAllCaps="true"
        android:textStyle="bold" />

    <RadioButton
        android:id="@+id/fakultaet"
        android:layout_
        android:layout_
        android:layout_alignBaseline="@+id/mal"
        android:layout_alignBottom="@+id/mal"
        android:layout_toLeftOf="@+id/mal"
        android:layout_toStartOf="@+id/mal"
        android:layout_weight="1"
        android:text="Fakultät "
        android:textAllCaps="true"
        android:textStyle="bold" />

    <RadioButton
        android:id="@+id/plus"
        android:layout_
        android:layout_
        android:layout_alignBaseline="@+id/minus"
        android:layout_alignBottom="@+id/minus"
        android:layout_toEndOf="@+id/nummer1"
        android:layout_toRightOf="@+id/nummer1"
        android:layout_weight="1"
        android:text="+ "
        android:textAllCaps="true"
        android:textStyle="bold" />

    <RadioButton
        android:id="@+id/geteilt"
        android:layout_
        android:layout_
        android:layout_alignTop="@+id/radioGroup"
        android:layout_toEndOf="@+id/radioGroup"
        android:layout_toRightOf="@+id/radioGroup"
        android:layout_weight="1"
        android:text=": "
        android:textAllCaps="true"
        android:textStyle="bold" />

    <RadioButton
        android:id="@+id/minus"
        android:layout_
        android:layout_
        android:layout_alignBaseline="@+id/geteilt"
        android:layout_alignBottom="@+id/geteilt"
        android:layout_toLeftOf="@+id/plus"
        android:layout_toStartOf="@+id/plus"
        android:layout_weight="1"
        android:text="- "
        android:textAllCaps="true"
        android:textStyle="bold" />

</RadioGroup>

<EditText
    android:id="@+id/ergebnis"
    android:layout_
    android:layout_
    android:layout_alignLeft="@+id/nummer2"
    android:layout_alignParentBottom="true"
    android:layout_alignStart="@+id/nummer2"
    android:layout_marginBottom="61dp"
    android:ems="10"
    android:hint="Ergebnis"
    android:inputType="number"
    android:textAlignment="center" />

<EditText
    android:id="@+id/nummer2"
    android:layout_
    android:layout_
    android:layout_alignLeft="@+id/nummer1"
    android:layout_alignStart="@+id/nummer1"
    android:layout_centerVertical="true"
    android:ems="10"
    android:hint="Nummer 2"
    android:inputType="number"
    android:textAlignment="center" />
</RelativeLayout>

你能告诉我该怎么做吗?在我的手机和模拟器上也是如此 如果您有任何问题,请与我联系!

PS:我必须在 12 月 12 日之前完成它

【问题讨论】:

【参考方案1】:

在设置内容视图之前,您不能使用 findviewbyid。它返回 null,这是你的例外。

把你的代码改成这样:

EditText number1;
EditText number2;
EditText ergebnis;
public int text1;
@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rechner);

    number1 = (EditText) findViewById(R.id.nummer1);
    number2 = (EditText) findViewById(R.id.nummer2);
    ergebnis = (EditText) findViewById(R.id.ergebnis);

    addListenerOnButton();

另外,我建议你阅读 android 代码风格推荐。 https://source.android.com/source/code-style.html

【讨论】:

senk ju wäri matsch 兄弟 我在 findViewById 之前添加 (EditText)。同样在过去,我会在每个 if 语句中需要它们时定义它们,但这会使应用程序变慢。所以,建议在 OnCreate 函数之后写。

以上是关于奇怪的错误 java.lang.reflect.Method.invokeNative(Native Method) Android Studio 请回答的主要内容,如果未能解决你的问题,请参考以下文章

CGContext 的奇怪错误

MySQL 语法错误?奇怪的错误

在spark中遇到了奇怪的错误,找到了奇怪的解决方法

奇怪的错误未定义的索引“项目”

C++:奇怪的分段错误

ASP.NET 奇怪的编译错误