2013-08-31 9 views
6

Also heres mein Problem. Ich habe eine Taste nach oben, die beim Anklicken soll eine neue Aktivität öffnen, aber wenn darauf geklickt wird Ich erhalte eine Fehlermeldung: Leider „app_name“ hat aufgehört zu arbeiten meine logcat sage: Fatal Exception HauptStarten Sie eine andere Aktivität, indem Sie auf eine Schaltfläche klicken

So here meine xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="30dp" 
     android:layout_marginTop="58dp" 
     android:text="Monday" 
     android:textSize="20sp"/> 

     <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView1" 
     android:layout_below="@+id/textView1" 
     android:layout_marginTop="30dp" 
     android:textSize="20sp" 
     android:text="Tuesday" /> 

     <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView2" 
     android:layout_below="@+id/textView2" 
     android:layout_marginTop="30dp" 
     android:textSize="20sp" 
     android:text="Wednesday" /> 

     <TextView 
     android:id="@+id/textView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView3" 
     android:layout_below="@+id/textView3" 
     android:layout_marginTop="30dp" 
     android:textSize="20sp" 
     android:text="Thursday" /> 

     <TextView 
     android:id="@+id/textView5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView4" 
     android:layout_below="@+id/textView4" 
     android:layout_marginTop="30dp" 
     android:textSize="20sp" 
     android:text="Friday" /> 

     <TextView 
     android:id="@+id/textView6" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView5" 
     android:layout_below="@+id/textView5" 
     android:layout_marginTop="30dp" 
     android:textSize="20sp" 
     android:text="Saturday" /> 

     <TextView 
     android:id="@+id/textView7" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView6" 
     android:layout_below="@+id/textView6" 
     android:layout_marginTop="30dp" 
     android:textSize="20sp" 
     android:text="Sunday" /> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBaseline="@+id/textView1" 
      android:layout_alignBottom="@+id/textView1" 
      android:layout_alignParentRight="true" 
      android:layout_marginRight="46dp" 
      android:text="Edit" 
      android:onClick="mondayintent" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBaseline="@+id/textView2" 
      android:layout_alignBottom="@+id/textView2" 
      android:layout_alignLeft="@+id/button1" 
      android:text="Edit" /> 

     <Button 
      android:id="@+id/button3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBaseline="@+id/textView3" 
      android:layout_alignBottom="@+id/textView3" 
      android:layout_alignLeft="@+id/button2" 
      android:text="Edit" /> 

     <Button 
      android:id="@+id/button4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBaseline="@+id/textView4" 
      android:layout_alignBottom="@+id/textView4" 
      android:layout_alignLeft="@+id/button3" 
      android:text="Edit" /> 

     <Button 
      android:id="@+id/button5" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBaseline="@+id/textView5" 
      android:layout_alignBottom="@+id/textView5" 
      android:layout_alignLeft="@+id/button4" 
      android:text="Edit" /> 

     <Button 
      android:id="@+id/button6" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBaseline="@+id/textView6" 
      android:layout_alignBottom="@+id/textView6" 
      android:layout_alignLeft="@+id/button5" 
      android:text="Edit" /> 

     <Button 
      android:id="@+id/button7" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBaseline="@+id/textView7" 
      android:layout_alignBottom="@+id/textView7" 
      android:layout_alignLeft="@+id/button6" 
      android:text="Edit" /> 
</RelativeLayout> 

und die zweite Xml zu, die in einer anderen Aktivität zu erreichen im Versuch:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="20dp" 
     android:textSize="40sp" 
     android:text="Monday" /> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView1" 
     android:layout_marginTop="36dp" 
     android:layout_toLeftOf="@+id/textView1" 
     android:ems="3" 
     android:inputType="time" /> 

    <EditText 
     android:id="@+id/editText2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/editText1" 
     android:layout_alignBottom="@+id/editText1" 
     android:layout_centerHorizontal="true" 
     android:ems="3" 
     android:inputType="time" > 
     <requestFocus /> 
    </EditText> 

     <Spinner 
     android:id="@+id/profileSelector" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 

     android:layout_alignBottom="@+id/editText2" 
     android:layout_toRightOf="@+id/textView1" 
     android:ems="3" 
     android:prompt="@string/profile" 
     android:entries="@array/profileSelector"/> 
</RelativeLayout> 

schließlich der Code, die beide unterstützt:

package com.example.hush; 

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

public class MainActivity extends Activity { 
     Intent clickedDay; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.mainlayout); 
     Button mondayEdit=(Button)findViewById(R.id.button1); 
    } 

    public void mondayintent() 
    { 
     clickedDay= new Intent(this,Monday.class); 
     startActivity(clickedDay); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
} 

zweiten Aktivitätscode:

package com.example.hush; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.Button; 

public class Monday extends Activity{   

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

Im Folgenden werde ich mein Android-manifest.xml

hinzugefügt haben
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.hush" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.hush.MainActivity" 
      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="com.example.hush.Monday" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="com.example.hush.Monday" />  
       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity>    
    </application>  
</manifest> 
+0

[Starten Sie eine neue Aktivität auf einen Knopf klicken] (http://themasterworld.com/in-this-lecture-we-will-see-how-to-start-a-new-activity-on-a- button-click-in-the-current-activity /) –

Antwort

17

Sie benötigen eine andere Tätigkeit wie dies in der OnClickListener() der Taste rufen

Button mondayEdit= (Button)findViewById(R.id.button1); 
    mondayEdit.setOnClickListener(new OnClickListener() 
    { public void onClick(View v) 
     { 
      Intent intent = new Intent(main.this, secondActivity.class); 
       startActivity(intent);  
       finish(); 
     } 
    }); 
+1

aber stattdessen habe ich einen android: onClick in der xml – alaboudi

+0

Aber Sie haben nicht die "View v" als Argumente hinzugefügt. public void montagintent (View v) {// Dein Code hier} – Mothy

+1

Du bist unglaublich !!!!!!!!!!!!!!!!!!!!!!!!!!!! – alaboudi

3

Wahrscheinlich wird Ihr Code aufgrund der Android-Manifestdatei angezeigt. Versuchen Sie, die richtige Klasse in man hinzuzufügen ifest-Datei. Die erste Aktivität als Haupt- und andere als Standard. Andere Teile des Codes sind genau richtig.

+0

Ich habe ein Manifest für Sie hinzugefügt, um es zu überprüfen ... Vielen Dank für die Zeit, um über diesen Beitrag zu lesen: D – alaboudi

+0

ändern Sie diese Zeile '< aktion android: name = "com.beispiel.hush.Monday" /> 'zu' ' –

Verwandte Themen