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

时间:2016-12-04 14:22:34

标签: java android android-studio android-activity native-methods

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

这是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>

这是Launcher活动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_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <Button
        android:text="Rechner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="wrap_content"
    android:layout_height="wrap_content"
    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_width="match_parent"
            android:layout_height="wrap_content">

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

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

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

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

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

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

        </TableRow>
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

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

            <TextView
                android:text="@string/r31"
                android:padding="10dp"
                android:background="@drawable/cell_shape"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:text="@string/r41"
                android:padding="10dp"
                android:background="@drawable/cell_shape"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:text="@string/r51"
                android:padding="10dp"
                android:background="@drawable/cell_shape"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:text="@string/r61"
                android:padding="10dp"
                android:background="@drawable/cell_shape"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </TableRow>
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

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

            <TextView
                android:text="@string/r22"
                android:padding="10dp"
                android:background="@drawable/cell_shape"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:text="@string/r32"
                android:padding="10dp"
                android:background="@drawable/cell_shape"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:text="@string/r42"
                android:padding="10dp"
                android:background="@drawable/cell_shape"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:text="@string/r52"
                android:padding="10dp"
                android:background="@drawable/cell_shape"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

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

我在Stundenplan.java中制作了一个意图:

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

问题是我每次在Xperia Z3上调试启动应用程序并按下按钮打开&#34; Rechner&#34; -Activity,应用程序关闭并显示以下错误:( sry for格式不好)

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 ComponentInfo{com.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_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.markwitt.schul_app.Rechner">

<EditText
    android:id="@+id/nummer1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    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_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/ergebnis"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="22dp"
    android:text="=" />

<RadioGroup
    android:id="@+id/radioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    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_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="wrap_content"
    android:layout_height="wrap_content"
    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_width="wrap_content"
    android:layout_height="wrap_content"
    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 个答案:

答案 0 :(得分:2)

在设置内容视图之前,不能使用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