Forced Close

C

-chriss-

Fortgeschrittenes Mitglied
6
Hallo,
ich möchte hier nur ganz "einfach" die andere Activity per Button öffnen:

PHP:
public class Startseite extends Activity implements OnClickListener {

    private Button btnList;
    private Button btnAdd;
    private Button btnEdit;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnList = (Button) findViewById(R.id.List);
        btnList.setOnClickListener(this);

        btnAdd = (Button) findViewById(R.id.Add);
        btnAdd.setOnClickListener(this);

        btnEdit = (Button) findViewById(R.id.Edit);
        btnEdit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.Edit:
            startActivity(new Intent().setClass(this, CanteenEdit.class));
        case R.id.Add:
            startActivity(new Intent().setClass(this, CanteenEdit.class));
        case R.id.List:
            startActivity(new Intent().setClass(this, CanteenList.class));
        }
    }
}
In Eclipse wird nichts falsches angezeigt, bekomme aber einen FC.
 
Schau mal nach was in der LogCat drinsteht, da dürfte dir Eclipse dann auch was falsches anzeigen. ^^

Window -> Show View -> Other -> Android -> LogCat
 
Jetzt bekomme ich einen FC, werde dann aber trotdem zur nächsten Activity weitergeleiter^^

Das hier wird angezeigt :


edit:
Es wäre nicht schlecht, die Reihenfolge der Buttons einzuhalten.... zumindest läuft es jetzt. Aber für was benenne ich die Buttons dann ?

Jetzt hab ich das Problem, das es egal ist, welchen Button ich drücke, ich komme immer zur gleichen Seite^^
 

Anhänge

  • Zwischenablage02.png
    Zwischenablage02.png
    33,7 KB · Aufrufe: 247
Zuletzt bearbeitet:
Code:
[COLOR=#000000][COLOR=#007700]        case [/COLOR][COLOR=#0000BB]R[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]id[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]Edit[/COLOR][COLOR=#007700]:
            [/COLOR][COLOR=#0000BB]startActivity[/COLOR][COLOR=#007700](new [/COLOR][COLOR=#0000BB]Intent[/COLOR][COLOR=#007700]().[/COLOR][COLOR=#0000BB]setClass[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]this[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]CanteenEdit[/COLOR][COLOR=#007700].class));
        case [/COLOR][COLOR=#0000BB]R[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]id[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]Add[/COLOR][COLOR=#007700]:
            [/COLOR][COLOR=#0000BB]startActivity[/COLOR][COLOR=#007700](new [/COLOR][COLOR=#0000BB]Intent[/COLOR][COLOR=#007700]().[/COLOR][COLOR=#0000BB]setClass[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]this[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]CanteenEdit[/COLOR][COLOR=#007700].class));[/COLOR][/COLOR]

Also mindestens den beiden Buttons weist du ja auch genau das gleiche zu. :winki:
 
Lilithian schrieb:
Code:
[COLOR=#000000][COLOR=#007700]        case [/COLOR][COLOR=#0000bb]R[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]id[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Edit[/COLOR][COLOR=#007700]:
            [/COLOR][COLOR=#0000bb]startActivity[/COLOR][COLOR=#007700](new [/COLOR][COLOR=#0000bb]Intent[/COLOR][COLOR=#007700]().[/COLOR][COLOR=#0000bb]setClass[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]this[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000bb]CanteenEdit[/COLOR][COLOR=#007700].class));
        case [/COLOR][COLOR=#0000bb]R[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]id[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Add[/COLOR][COLOR=#007700]:
            [/COLOR][COLOR=#0000bb]startActivity[/COLOR][COLOR=#007700](new [/COLOR][COLOR=#0000bb]Intent[/COLOR][COLOR=#007700]().[/COLOR][COLOR=#0000bb]setClass[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]this[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000bb]CanteenEdit[/COLOR][COLOR=#007700].class));[/COLOR][/COLOR]
Also mindestens den beiden Buttons weist du ja auch genau das gleiche zu. :winki:

Das ist auch beabsichtigt, da ich die Änderungen an der ListView beide in die CanteenEdit packen werde. Das Problem ist, dass ich mit dem 3. Button auf die CanteenEdit Seite komme.

Es wird immernoch ein FC gemacht, anschließend wird die App geöffnet (mit der richtigen Seite)...
 
Also ich habs mal nachgebaut und mit genau deinem Code funktionierts bei mir.

Bei mir im Emulator funktioniert der Code mit Android Version 2.2 und 1.6, meine CanteenEdit.java und CanteenList.java haben bisher nur ein onCreate was im Log zeigt, dass sie aufgerufen wurden, im Layout sind nur die drei Buttons. Startseite ist genauso wie bei dir.

Laut Google gibts diese Fehlermeldung häufiger wenn was im Layout nicht passt. Ansonsten bleibt ja nur noch das Manifest wo es nicht passen könnte.
 
Zuletzt bearbeitet:
du hast beim switch cases - ohne break, beabsichtigt?
 
unwahrscheinlich^^
 
Hmm natürlich lag es daran, dass break; gefehlt hat^^ ohje.... dafür bedarf es keinem weiteren KOmmentar
 
So hab nun wieder einen FC. Log sieht wie folgt aus:

Activity1 hat 3 Buttons, per Button kam ich eigentlich bisher immer zu Activity2, dies wird nun mit einem FC beendet. Die einzige Änderung war, dass ich in Activity2 Buttons und einen Menüeintrag hinzugefügt habe. Ohne diese läuft es ganz normal. Was mich zu dem Punkt bringt, dass es mit den Intents irgendwelche Probleme gibt oder mit dem EventListener.
 

Anhänge

  • Zwischenablage01.png
    Zwischenablage01.png
    28,8 KB · Aufrufe: 240
-chriss- schrieb:
Was mich zu dem Punkt bringt, dass es mit den Intents irgendwelche Probleme gibt oder mit dem EventListener.

afaik kannst du keinen EventListener einem Button hinzufügen, wenn dieser Button nicht im ContentView geladen ist.

regards
 
Wieso bekomm ich einen FC, wenn ich diesen Button auswähle ?

... irgendetwas übersehe ich doch :(

PHP:
package de.mycanteen;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MyCanteen extends Activity implements OnClickListener {

    private Button btnlist;
    private Button btnexit;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnlist = (Button) findViewById(R.id.btnlist);
        btnlist.setOnClickListener(this);

        btnexit = (Button) findViewById(R.id.btnexit);
        btnexit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btnlist:
            startActivity(new Intent().setClass(MyCanteen.this, CanteenList.class));
            break;
        case R.id.btnexit:
            finish();break;
        }
    }
}
Das hier ist die Activity, welche gestartet werden soll:

PHP:
package de.mycanteen;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.sax.Element;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

public class CanteenList extends Activity {
    
    // ArrayList, welche die verschiedenen Objekte der HashMap enthält
    private  ArrayList<HashMap<String, Object>> myList;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
             
        // erstelle HashMap
        myList = new ArrayList<HashMap<String, Object>>();
        
        // Fülle Hashmap mit Mensadaten
        addDataToList();
        
        // Viewlist
        ListView mainListView = (ListView) findViewById(R.id.main_listview);
        
        // Adapter initialisieren
        SimpleAdapter aa = new SimpleAdapter(this, myList, R.layout.row,
                new String[] {"name", "town", "link"}, 
                new int[] {R.id.txt_name, R.id.txt_town});
        mainListView.setAdapter(aa);
        
        // Listener
        mainListView.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
              // Besorge die Position des angeklickten Listenpunkts
              HashMap<String, Object> user = myList.get(position);

              //Initialisiere linkXML StringBuffer
                StringBuffer linkXML = new StringBuffer();
              
              // Die Attributenamen der HashMap besorgen
              String name = (String)user.get("name");
              String town = (String)user.get("town");
              String url = (String)user.get("link");                           
              
//              //lade die xml in inputLine
//                    try {
//                        URL urlXML = new URL(url);
//                        InputStreamReader isr;
//                        try {
//                            isr = new InputStreamReader(urlXML.openStream());
//                            BufferedReader in = new BufferedReader(isr);
//
//                                String inputLine;
//
//                            while ((inputLine = in.readLine()) != null){
//                               linkXML.append(inputLine);
//                            }
//                        } catch (IOException e) {
//                            // TODO Auto-generated catch block
//                            e.printStackTrace();
//                        }
//
//                        }
//                    catch (MalformedURLException e) {
//                        e.printStackTrace();
//                    }
          
                    
//            /*************************************Test****************/        
//          }
//        }
//           
//        /***************************Parser*****************************/
//        
//                    private int ParseXMLString() {
//                        String xmlRecords = inputLine.toString();
//                        try {
//                               DocumentBuilderFactory dbf =
//                                    DocumentBuilderFactory.newInstance();
//                                DocumentBuilder db = dbf.newDocumentBuilder();
//                                InputSource is = new InputSource();
//                                is.setCharacterStream(new StringReader(xmlRecords));
//                                Document doc = db.parse(is);
//                                NodeList nodes = doc.getElementsByTagName("item");            
//                                for (int i = 0; i < nodes.getLength(); i++) {
//                                   Element element = (Element) nodes.item(i);
//                                   NodeList name = element.getElementsByTagName("title");
//                                   Element line = (Element) name.item(0);         
//                                   String dayString = getCharacterDataFromElement(line).split(",")[0];
//                                   dayString = dayString.trim();
//                                   String dateString = getCharacterDataFromElement(line).split(",")[1];
//                                   dateString = dateString.trim();
//                                   NodeList title = element.getElementsByTagName("description");   
//                                   line = (Element) title.item(0);
//                                   String menuString = getCharacterDataFromElement(line);
//                                   menuString = menuString.trim();
//                                   menuString = menuString.replace("<br>", "\n");
//                                   menuString = menuString.replace("<b>", "");
//                                   menuString = menuString.replace("</b>", "");
//                                   menuString = menuString.replace("<u>", "\n");
//                                   menuString = menuString.replace("</u>", "");
//                                   byte[] b = menuString.getBytes("ISO-8859-1");
//                                   menuString = new String(b, "UTF-8");          
//                                }
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//                        return 0;
//                    }
//        
//                    /***
//    }
//                    
//                    ***/
                    
                    
              // Erstelle einen neuen Dialog mit dem aktuellen Menüs
              final Dialog dialog = new Dialog(CanteenList.this);
              dialog.setTitle("Menü der " + name + town + url);
              TextView txtDescription = new TextView(CanteenList.this);
              txtDescription.setPadding(10, 0, 0, 10);
              txtDescription.setText("Detail Description");
              dialog.setContentView(txtDescription);

              dialog.setCanceledOnTouchOutside(true);
              dialog.show();
          }
        });  
    }
    

// Fülle die HashMap mit Mensen
    private void addDataToList(){
        HashMap<String, Object> map1 = new HashMap<String, Object>();
        map1.put("name","Mensa ");
        map1.put("town","Furtwangen");
        map1.put("link", "http://www.studentenwerk.uni-freiburg.de/index.php?id=speiseplaene&no_cache=1&L=&Tag=0&Ort_ID=641");
        
        HashMap<String, Object> map2 = new HashMap<String, Object>();    
        map2.put("name", "Mensa ");
        map2.put("town", "Trossingen");
        map2.put("link", "http://www.studentenwerk.uni-freiburg.de/index.php?id=speiseplaene&no_cache=1&L=&Tag=0&Ort_ID=672");
        
        
        myList.add(map1);
        myList.add(map2);
        
    }
}
Manifest:

PHP:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="de.mycanteen"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MyCanteen"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity android:name=".CanteenList" android:label="@string/app_name">
        </activity>
        
    </application>
    <uses-sdk android:minSdkVersion="8" />

</manifest>
Strings:

PHP:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    
    <string name="app_name">MyCanteen</string>
    <string name="add_canteen">Add Canteen</string>
    <string name="add">Hinzufügen</string>
    <string name="delete">Entfernen</string>
    <string name="list">Zeige Liste</string>
    <string name="cancel">Beenden</string>
    <string name="canteen">Canteen</string>
    <string name="Name:">Name:</string>
    <string name="Town:">Stadt:</string>
</resources>
main.xml

PHP:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_height="fill_parent"
    android:id="@+id/main"
    android:layout_width="fill_parent"
    android:background="@drawable/background">
   
<TextView
    android:textSize="20dp"
    android:textStyle="bold"
    android:textColor="#000000"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/message"
    android:text="@string/app_name"
    android:padding="20dp"/>

<Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/btnlist" android:text="@string/list"></Button>
<Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/btnexit" android:text="@string/cancel"></Button>

</LinearLayout>
row.xml

PHP:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingBottom="10dp" android:paddingTop="10dp"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <LinearLayout android:id="@+id/LinearLayout01"
        android:layout_height="wrap_content" android:gravity="center_vertical"
        android:layout_width="fill_parent">
        
        <!--<ImageView android:layout_height="wrap_content" android:id="@+id/img_user"
            android:layout_marginLeft="40dp" android:layout_width="wrap_content"></ImageView>
        
        -->
        <TableLayout android:id="@+id/TableLayout01"
            android:layout_below="@+id/LinearLayout01" android:layout_height="wrap_content"
            android:layout_width="fill_parent" android:layout_marginLeft="10dp">
            <TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView android:id="@+id/TextView01"
                    android:layout_width="wrap_content" android:layout_height="wrap_content"
                    android:textColor="#FFFFFF" android:paddingRight="10dp"
                    android:text="@string/Name:"></TextView>
                <TextView android:layout_height="wrap_content"
                    android:layout_width="wrap_content" android:id="@+id/txt_name"
                    android:textColor="#FFFFFF" android:textStyle="bold"
                    android:paddingBottom="5dp"></TextView>
            </TableRow>
            
            <TableRow android:id="@+id/TableRow02" android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView android:layout_width="wrap_content"
                    android:layout_height="wrap_content" android:textColor="#FFFFFF"
                    android:paddingRight="10dp" android:text="@string/Town:" android:id="@+id/TextView02"></TextView>
                <TextView android:layout_height="wrap_content"
                    android:layout_width="wrap_content" android:textColor="#FFFFFF"
                    android:paddingBottom="5dp" android:id="@+id/txt_town"></TextView>
            </TableRow>
            
            <!--<TableRow android:id="@+id/TableRow03" android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView android:layout_width="wrap_content"
                    android:layout_height="wrap_content" android:textColor="#FFFFFF"
                    android:paddingRight="10dp" android:text="Link:" android:id="@+id/TextView03"></TextView>
                <TextView android:layout_height="wrap_content"
                    android:layout_width="wrap_content" android:textColor="#FFFFFF"
                    android:paddingBottom="5dp" android:id="@+id/txt_link"></TextView>
                </TableRow>
        
        --></TableLayout>
    </LinearLayout>
</RelativeLayout>
canteenlist.xml

PHP:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:id="@+id/LinearLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/background">
            SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.mainlist_item, c,
                    new String[] { "Zeitpunkt", "Menge" }, new int[] { android.R.id.text1, android.R.id.text1 });
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="?android:attr/listPreferredItemHeight" android:textAppearance="?android:attr/textAppearanceLarge" android:gravity="center_vertical" android:paddingLeft="5dip" android:layout_width="fill_parent" android:singleLine="false" android:id="@android:id/text1"/>

</LinearLayout>
 
Zuletzt bearbeitet:

Ähnliche Themen

missspelled
  • missspelled
Antworten
4
Aufrufe
623
missspelled
missspelled
N
  • N3cron
Antworten
5
Aufrufe
775
dthiess
D
F
Antworten
4
Aufrufe
711
Frask3r
F
Zurück
Oben Unten