Android Studio无法找到Resuorces

时间:2022-01-10 20:50:27

This is the error message which shows up when I run the apk on my virtual device.

这是我在虚拟设备上运行apk时出现的错误消息。

05-03 13:00:03.652    2354-2354/de.hochrad.hochradapp I/art﹕ Not late-enabling -Xcheck:jni (already on)
05-03 13:00:05.966    2354-2354/de.hochrad.hochradapp D/AndroidRuntime﹕ Shutting down VM
05-03 13:00:05.970    2354-2354/de.hochrad.hochradapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: de.hochrad.hochradapp, PID: 2354
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.hochrad.hochradapp/de.hochrad.hochradapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
        at android.widget.Toast.<init>(Toast.java:101)
        at android.widget.Toast.makeText(Toast.java:250)
        at de.hochrad.hochradapp.MainActivity.onCreate(MainActivity.java:26)
        at android.app.Activity.performCreate(Activity.java:5937)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
05-03 13:00:08.238    2354-2354/de.hochrad.hochradapp I/Process﹕ Sending signal. PID: 2354 SIG: 9

Somehow it cannot find the required Resources.

不知何故,它无法找到所需的资源。

I hope you can help me!!! Thx for all answers!!!

我希望你能帮帮我!!!谢谢所有答案!!!

package de.hochrad.hochradapp;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

ArrayAdapter<String> klassen_adapter;
Vertretungsplan vertretungsplan;
Spinner klassen;

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


    Toast.makeText(null, "Laden...", Toast.LENGTH_SHORT).show();
    klassen_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);



    klassen = (Spinner) findViewById(R.id.klassenspinner);

    Thread downloadThread = new Thread() {
        public void run() {
            vertretungsplan = new Vertretungsplan("1");

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (vertretungsplan.Ex != null) {
                        klassen_adapter.add("Fehler!");
                    } else {
                        klassen_adapter.add("Wähle deine Klasse!");
                        for (Klassenvertretung s : vertretungsplan.Klassen) {
                            klassen_adapter.add(s.Bezeichnung);
                        }
                    }
                }
            });
        }
    };
    downloadThread.start();

    klassen.setAdapter(klassen_adapter);
    klassen.setSelection(0);
    klassen.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {


        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(parent.getContext(),
                    "Deine Auswahl ist:" + parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();

        }


        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, 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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

package de.hochrad.hochradapp;

    import java.util.ArrayList;
    import java.util.List;

public class Klassenvertretung {
public String Bezeichnung;

public List<Vertretung> Vertretungen = new ArrayList<Vertretung>();

public void Hinzufügen(Vertretung neuesElement) {
    Vertretungen.add(neuesElement);
}
}


package de.hochrad.hochradapp;

public class Vertretung {
public String Klasse;
public String Stunde;
public String Art;
public String Fach;
public String Raum;
public String stattFach;
public String stattRaum;
public String Informationen;
}

package de.hochrad.hochradapp;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Vertretungsplan {

public Vertretungsplan(String woche) {
    Woche = woche;
    Einlesen(woche);
}

public String Woche;
public Exception Ex;

public List<Klassenvertretung> Klassen = new ArrayList<Klassenvertretung>();

private void Hinzufügen(Klassenvertretung neuesElement) {
    Klassen.add(neuesElement);
}

private void Einlesen(String woche) {
    try {
        for (int webseite = 1; webseite < 10000; webseite++) {
            Klassenvertretung klassenvertretung = new Klassenvertretung();

            String teilseite = "0000";
            if (webseite < 10)
                teilseite = teilseite + "0";
            teilseite = teilseite + webseite;

            Connection connection = Jsoup
                    .connect("www.gymnasium-hochrad.de/Vertretungsplan/Vertretungsplan_Internet/"
                            + woche + "/w/w" + teilseite + ".htm");
            Document doc = connection.get();
            Element h2 = doc.select("h2").get(0);
            klassenvertretung.Bezeichnung = h2.text();

            Element table = doc.select("table").get(1);
            Element[] elemente = table.select("tr").toArray(new Element[0]);
            for (int i = 1; i < elemente.length; i++) {
                Element[] tds = elemente[i].select("td").toArray(
                        new Element[0]);

                Vertretung vertretung = new Vertretung();
                vertretung.Klasse = tds[0].text();
                vertretung.Stunde = tds[1].text();
                vertretung.Art = tds[2].text();
                vertretung.Fach = tds[3].text();
                vertretung.Raum = tds[4].text();
                vertretung.stattFach = tds[5].text();
                vertretung.stattRaum = tds[6].text();
                vertretung.Informationen = tds[7].text();
                klassenvertretung.Hinzufügen(vertretung);
            }

            Hinzufügen(klassenvertretung);
        }
    } catch (IOException io) {
        if (Klassen.size() == 0) {
            Ex = io;
        }
    } finally {
    }
}
}

okay here is my code. I am form germany and so lots of Names are german (i hope thats not a problem.

好的,这是我的代码。我是德国人,所以很多名字都是德国人(我希望这不是问题。

Maybe it helps.

也许有帮助。

I guess the error must be in the main activity in one of the toasts. But dont hesitate to look at the other lines.

我猜错误必须在其中一个祝酒词的主要活动中。但不要犹豫,看看其他线路。

2 个解决方案

#1


You have not initialised your adapter namely "klassen_adapter" in your main activity. It's null and invoking any method on it will be null pointer exception

您尚未在主活动中初始化适配器,即“klassen_adapter”。它是null并且调用它上面的任何方法都将是空指针异常

#2


A/c to logcat error your are getting null reference error. try to update this line

A / c到logcat错误你的null引用错误。尝试更新此行

 Toast.makeText(parent.getContext(),
                "Deine Auswahl ist:" + parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();

with the following code

使用以下代码

 Toast.makeText(MainActivity.this,
                "Deine Auswahl ist:" + parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();

#1


You have not initialised your adapter namely "klassen_adapter" in your main activity. It's null and invoking any method on it will be null pointer exception

您尚未在主活动中初始化适配器,即“klassen_adapter”。它是null并且调用它上面的任何方法都将是空指针异常

#2


A/c to logcat error your are getting null reference error. try to update this line

A / c到logcat错误你的null引用错误。尝试更新此行

 Toast.makeText(parent.getContext(),
                "Deine Auswahl ist:" + parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();

with the following code

使用以下代码

 Toast.makeText(MainActivity.this,
                "Deine Auswahl ist:" + parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();