1

我開發一個應用程序爲我校有一個時間表和一個小計算器奇怪的錯誤java.lang.reflect.Method.invokeNative(本機方法)的Android工作室請回答

下面是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_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> 

我做了一個Intent在Stundenplan.java:

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

的問題是,每次我調試發動對我的Xperia Z3的應用程序,並按下按鈕,打開「雷希納」 -activity,應用程序關閉它顯示了我下面的錯誤:(SRY的壞格式)

I/OpenGLRenderer: Initialized EGL, version 1.4 
I/Timeline: Timeline: Activity_idle id: [email protected] time:6061206 
I/Timeline: Timeline: Activity_idle id: [email protected] 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日

回答

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

+0

senk juwärimatsch bro –