Android InputStream und URL force Close

C

chrix_123

Neues Mitglied
3
Hallo Leute,
ich versuche mit einer Android app eine Website auszulesen. Also ich lese den code aus, und reduziere ihn so weit bis ich meine Informationen übrig habe.
Am PC (als normales Java Programm) funktioniert das auch wunderbar, bis auf das das Auslesen manchmal einen zu kurzen Code zurückgibt. Das soll aber nicht Thema hier sein.

Jetzt habe ich versucht, das ganze in eine App zu packen, aber immer wenn ich dann den Input Stream öffnen will, gibt es einen force Close. Ich hab der App schon Internet Permissions gegeben.

Hier mal ein vereinfachter Code, der auch nicht geht:
Code:
package com.vertretung.asgsg;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {
    InputStream i;

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
public void disp(){
    try {
        i=new URL("http://www.google.de").openStream();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    int c=0;
    try {
        
        c = i.available();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();System.out.println("Hier ists");
    }
    String text="";
    for(int ic=0;ic<c;ic++){
        try {
            text=text+(char)(i.read());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();System.out.println("Hier ists");
        }
    }
    try {
        i.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    TextView t=(TextView) findViewById(R.id.textView8);
    t.setText(text);
}
    
}

Die Methode disp() wird von einem Button aufgerufen. Sobald ich den Button drücke, ist die App abgestürzt.

Vlt. liegt es dran, dass ich mit API16 programmiere, und man muss iwas beachten.

Vielen Dank schonmal im Vorraus.
 
niemand kann hellsehen. => logcat

wahrscheinlich networkOnMainThreadException
 
Entschuldigung. Hab ich ganz vergessen.

Hier die Fehlermeldung:
Code:
08-30 14:19:21.098: D/gralloc_goldfish(715): Emulator without GPU emulation detected.
08-30 14:19:23.599: D/AndroidRuntime(715): Shutting down VM
08-30 14:19:23.599: W/dalvikvm(715): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
08-30 14:19:23.669: E/AndroidRuntime(715): FATAL EXCEPTION: main
08-30 14:19:23.669: E/AndroidRuntime(715): java.lang.IllegalStateException: Could not find a method disp(View) in the activity class com.vertretung.asgsg.MainActivity for onClick handler on view class android.widget.Button with id 'button1'
08-30 14:19:23.669: E/AndroidRuntime(715):     at android.view.View$1.onClick(View.java:3578)
08-30 14:19:23.669: E/AndroidRuntime(715):     at android.view.View.performClick(View.java:4084)
08-30 14:19:23.669: E/AndroidRuntime(715):     at android.view.View$PerformClick.run(View.java:16966)
08-30 14:19:23.669: E/AndroidRuntime(715):     at android.os.Handler.handleCallback(Handler.java:615)
08-30 14:19:23.669: E/AndroidRuntime(715):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-30 14:19:23.669: E/AndroidRuntime(715):     at android.os.Looper.loop(Looper.java:137)
08-30 14:19:23.669: E/AndroidRuntime(715):     at android.app.ActivityThread.main(ActivityThread.java:4745)
08-30 14:19:23.669: E/AndroidRuntime(715):     at java.lang.reflect.Method.invokeNative(Native Method)
08-30 14:19:23.669: E/AndroidRuntime(715):     at java.lang.reflect.Method.invoke(Method.java:511)
08-30 14:19:23.669: E/AndroidRuntime(715):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-30 14:19:23.669: E/AndroidRuntime(715):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-30 14:19:23.669: E/AndroidRuntime(715):     at dalvik.system.NativeStart.main(Native Method)
08-30 14:19:23.669: E/AndroidRuntime(715): Caused by: java.lang.NoSuchMethodException: disp [class android.view.View]
08-30 14:19:23.669: E/AndroidRuntime(715):     at java.lang.Class.getConstructorOrMethod(Class.java:460)
08-30 14:19:23.669: E/AndroidRuntime(715):     at java.lang.Class.getMethod(Class.java:915)
08-30 14:19:23.669: E/AndroidRuntime(715):     at android.view.View$1.onClick(View.java:3571)
08-30 14:19:23.669: E/AndroidRuntime(715):     ... 11 more
08-30 14:19:26.408: I/Process(715): Sending signal. PID: 715 SIG: 9
 
es sollte
public void disp(View v)


heißen, wenn du onClick im xml verwendest
 
Jo, da hät ich dran denken sollen. Hilft aber auch nicht wirklich.

Code:
08-30 14:19:21.098: D/gralloc_goldfish(715): Emulator without GPU emulation detected.
08-30 14:19:23.599: D/AndroidRuntime(715): Shutting down VM
08-30 14:19:23.599: W/dalvikvm(715): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
08-30 14:19:23.669: E/AndroidRuntime(715): FATAL EXCEPTION: main
08-30 14:19:23.669: E/AndroidRuntime(715): java.lang.IllegalStateException: Could not find a method disp(View) in the activity class com.vertretung.asgsg.MainActivity for onClick handler on view class android.widget.Button with id 'button1'
08-30 14:19:23.669: E/AndroidRuntime(715):     at android.view.View$1.onClick(View.java:3578)
08-30 14:19:23.669: E/AndroidRuntime(715):     at android.view.View.performClick(View.java:4084)
08-30 14:19:23.669: E/AndroidRuntime(715):     at android.view.View$PerformClick.run(View.java:16966)
08-30 14:19:23.669: E/AndroidRuntime(715):     at android.os.Handler.handleCallback(Handler.java:615)
08-30 14:19:23.669: E/AndroidRuntime(715):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-30 14:19:23.669: E/AndroidRuntime(715):     at android.os.Looper.loop(Looper.java:137)
08-30 14:19:23.669: E/AndroidRuntime(715):     at android.app.ActivityThread.main(ActivityThread.java:4745)
08-30 14:19:23.669: E/AndroidRuntime(715):     at java.lang.reflect.Method.invokeNative(Native Method)
08-30 14:19:23.669: E/AndroidRuntime(715):     at java.lang.reflect.Method.invoke(Method.java:511)
08-30 14:19:23.669: E/AndroidRuntime(715):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-30 14:19:23.669: E/AndroidRuntime(715):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-30 14:19:23.669: E/AndroidRuntime(715):     at dalvik.system.NativeStart.main(Native Method)
08-30 14:19:23.669: E/AndroidRuntime(715): Caused by: java.lang.NoSuchMethodException: disp [class android.view.View]
08-30 14:19:23.669: E/AndroidRuntime(715):     at java.lang.Class.getConstructorOrMethod(Class.java:460)
08-30 14:19:23.669: E/AndroidRuntime(715):     at java.lang.Class.getMethod(Class.java:915)
08-30 14:19:23.669: E/AndroidRuntime(715):     at android.view.View$1.onClick(View.java:3571)
08-30 14:19:23.669: E/AndroidRuntime(715):     ... 11 more
08-30 14:19:26.408: I/Process(715): Sending signal. PID: 715 SIG: 9

Der neue Log
 
ja so gehts aber

zeig mal dein xml
 
Hier mein XML:

Code:
<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" >

 
    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_toLeftOf="@+id/button1"
        android:text=" "
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView7"
        android:layout_marginTop="112dp"
        android:layout_toRightOf="@+id/textView7"
        android:text="Button" 
        android:onClick="disp"/>

</RelativeLayout>
 
unabhängig vom problem

layout_toRightOf="@+id/..."

warum @+id/ ???
das passt mal nicht...

das onclick passt so - versuch einfach einen onclicklistener mal ganz normal einfügen.
 
unabhängig vom problem

layout_toRightOf="@+id/..."

warum @+id/ ???
das passt mal nicht..

keine Ahnung!!!
Ich bin noch neu in Android programmieren und benutze die grafische Oberfläche für die xml.


An dem Button, bzw. an der onClick methode kanns nicht liegen, da es denselben Fehler gibt, wenn ich disp() beim Start der App ausführen lasse.
 

Ähnliche Themen

H
Antworten
2
Aufrufe
1.309
Hcman
H
M
Antworten
4
Aufrufe
1.171
swa00
swa00
5
Antworten
0
Aufrufe
1.143
586920
5
Zurück
Oben Unten