2016-07-27 8 views
0

Hallo Jungs, ich bin mit 3 Aktivitäten in meinem Android-App, dieWie gehen zweite Aktivität von der ersten Aktivität auf die Schaltfläche klicken und Third-Aktivität aus der zweiten auf die Schaltfläche klicken in android

1.main_activity

2 sind. Secondactivity

3.Third Aktivität

ich bin vor Problem, wenn ich versuche, die Zweite Aktivität zu öffnen von Haupttätigkeit über einen Button, klicken Sie auf Haupttätigkeit. Die App stürzt ab und die Dosis nicht funktioniert

Ich brauche auch meine Dritte Aktivität zu öffnen, über einen Button-Klick auf Zweite Aktivität Wenn es

geöffnet ist, wie ich bin neu auf Android Codierung ist nicht in der Lage zu verstehen, wo ich falsch in meiner Codierung mache. Ich möchte Informationen über suchen und auch eine beliebiges brauche die

Below mit mir aus diesem

Vielen Dank im Voraus helfen kann, ist der vollständige Code und die Fehler sind immer ist dies

07-27 12:13:51.887 23039-23039/com.example.mohammadzakriya.tech_bgkapp E/AndroidRuntime: FATAL EXCEPTION: main 
    Process: com.example.mohammadzakriya.tech_bgkapp, PID: 23039 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mohammadzakriya.tech_bgkapp/com.example.mohammadzakriya.tech_bgkapp.Secondactivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:5417) 
     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 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference 
     at com.example.mohammadzakriya.tech_bgkapp.Secondactivity.OnclickButtonListener(Secondactivity.java:24) 
     at com.example.mohammadzakriya.tech_bgkapp.Secondactivity.onCreate(Secondactivity.java:19) 
     at android.app.Activity.performCreate(Activity.java:6237) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:5417) 
     at java.lang.reflect.Method.invoke(Native Method) "** 
                          at java.lang.reflect.Method.invoke(Native Method)  

Hier ist Android Haupt Activity.java Datei:

package com.example.mohammadzakriya.tech_bgkapp; 

import android.content.Intent; 
import android.support.v7.app.ActionBarActivity; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class MainActivity extends AppCompatActivity{ 

    public static Button button_smb; 


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

    public void OnclickButtonListener(){ 
     button_smb =(Button)findViewById(R.id.button); 
     button_smb.setOnClickListener(
       new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         Intent intent = new Intent("com.example.mohammadzakriya.tech_bgkapp.Secondactivity"); 
         startActivity(intent); 
        } 
       } 
     ); 
    } 
} 

Hier Zweite Activity.java Datei:

package com.example.mohammadzakriya.tech_bgkapp; 

import android.app.Activity; 
import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class Secondactivity extends Activity{ 

    public static Button button_smb1; 


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

    public void OnclickButtonListener(){ 
     button_smb1 =(Button)findViewById(R.id.button); 
     button_smb1.setOnClickListener(
       new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         Intent myintent = new Intent("com.example.mohammadzakriya.tech_bgkapp.ThirdActivity"); 
         startActivity(myintent); 
        } 
       } 
     ); 
    } 
} 

Main Activity XML-Datei:

<?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" 
    tools:context="com.example.mohammadzakriya.tech_bgkapp.MainActivity" 
    android:background="@drawable/frontpic"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Click to proceed" 
     android:id="@+id/button" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" 
     android:textStyle="bold|italic|normal" 
     android:textSize="19dp" 
     android:background="#d55b26f8" 
     android:textColor="#e2e0e0" /> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="  WELCOME TO BAGALKOT APP" 
     android:id="@+id/textView" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:textStyle="bold|italic" 
     android:layout_marginTop="50dp" 
     android:textSize="25dp" 
     android:background="#fbe47d0e" 
     android:textColor="#e2e0e0" /> 
</RelativeLayout> 

Zweite Aktivität xml-Datei:

<?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" 
    tools:context="com.example.mohammadzakriya.tech_bgkapp.Secondactivity" 
    android:background="#eb5d16ea"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="ELECTRONICS" 
     android:id="@+id/button2" 
     android:layout_marginTop="42dp" 
     android:textStyle="bold|italic|normal" 
     android:background="#eec16d0d" 
     android:layout_below="@+id/textView2" 
     android:layout_alignParentStart="true" 
     android:textSize="18dp" 
     android:textColor="#e2e0e0"/> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="    SELECT THE CATEGORY" 
     android:id="@+id/textView2" 
     android:layout_alignParentTop="true" 
     android:layout_alignStart="@+id/button2" 
     android:layout_marginTop="15dp" 
     android:textStyle="bold|italic|" 
     android:background="#fbe47d0e" 
     android:textColor="#e2e0e0"/> 
</RelativeLayout> 

Android-Manifest-Datei:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.mohammadzakriya.tech_bgkapp"> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity 
      android:name=".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=".Secondactivity" 
      android:label="Tech-Bgk App"> 
      <intent-filter> 
       <action android:name="com.example.mohammadzakriya.tech_bgkapp.Secondactivity" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".ThirdActivity" 
        android:label="Tech-Bgk App"> 
      <intent-filter> 
       <action android:name="com.example.mohammadzakriya.tech_bgkapp.ThirdActivity" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 

     </activity> 
    </application> 

</manifest> 

Antwort

0

verwenden:

Intent intent = new Intent(MainActivity.this, Secondactivity.class); 
startActivity(intent); 

Meiner Erfahrung nach, so bewegen Sie sich von einem Activity zu einem anderen Activity durch Code.

Und noch etwas, das ist eine seltsame Art und Weise eine onClickListener() hinzuzufügen, wenn es nur direkt erklären Sie die Taste nur eine einfache „erste Aktivität auf die zweite Aktivität“ ist, sollten Sie und stellen Sie die onClickListener()

Button button_smb =(Button)findViewById(R.id.button); 
button_smb.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     Intent intent = new Intent(this, Secondactivity.class); 
     startActivity(intent); 
     } 
} 
+0

ich hier '' this' OnClickListener' Objekt verweist, nicht 'Activity' verweist. Also ich denke, es wird Ihnen einen Fehler geben oder es wird Ihnen nicht die gewünschte Ausgabe geben. –

+0

Ohooo Ich vergesse fast immer, sorry, edit es –

+0

Deshalb versuchen Sie immer, ein Context-Objekt zu verwenden, es wird Sie in Sicherheit bringen. –

0

unter Ersetzen Code in Ihrer Datei mainActivity.

button_smb.setOnClickListener(
       new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         Intent intent = new Intent(MainActivity.this,Secondactivity.class); 
         startActivity(intent); 
        } 
       } 
     ); 
+0

Hier bezieht sich "this" auf das Objekt "OnClickListener". Es wird also nicht funktionieren, denke ich. –

+0

getan die Bearbeitungen .. –

0

wie Versuchen,

Intent intent = new Intent(MainActivity.this, Secondactivity.class); 
startActivity(intent); 

sollte dies hilft Ihnen

1

Bitte versuchen Sie es mit dem Code wie unten

Button-Klick auf MainActivity zu SecondActivity gehen

@Override 
    public void onClick(View view) { 
     Intent myintent = new Intent(MainActivity.this, SecondActivity.class); 
     startActivity(myintent); 
    } 

Ein d-Taste, klicken Sie auf SecondActivity gehen zu ThirdActivity

@Override 
    public void onClick(View view) { 
     Intent myintent = new Intent(SecondActivity.this, ThirdActivity.class); 
     startActivity(myintent); 
    } 

Herausgegeben

Alles in Ihrem Code ist in Ordnung, außer der Initialisierung von button_smb1 in Secondactivity.

Bitte die Änderung in Secondactivity als

button_smb1 =(Button)findViewById(R.id.button2);.

Jetzt wird es funktionieren. Keine Notwendigkeit, etwas anderes als zu ändern.

+0

Fertig die Bearbeitungen, die Sie oben erwähnt haben, aber konnte den Fehler nicht beheben. Muss ich Änderungen in der Android-Manifest-Datei vornehmen, bevor Sie die Änderungen anwenden? –

+0

Bitte tun Sie dies in der zweiten Aktivität 'button_smb1 = (Button) findViewById (R.id.button2);'. Jetzt wird es funktionieren. Keine Notwendigkeit, etwas anderes als zu ändern. –

+0

Danke, es hat funktioniert. –

0

ändern Code unten
Haupttätigkeit zur zweiten Aktivität

Intent intent = new Intent(this, Secondactivity.class); 
startActivity(intent); 

Zweite Aktivität dritte Aktivität

Intent intent = new Intent(this, ThirdActivity.class); 
startActivity(intent); 
0

die neue Um zu vermeiden, ziehe ich diese approch verwenden:

public class MyActivity extends AppCompactActivity implents View.OnClickListener{ 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    .... 
    button.setOnClickListener(this); 
    ... 
} 

@Override 
public void onClick(View view) { 

    Intent i; 
    switch (view.getId()) { 
     case R.id.button1:      
      i = new Intent(this, SecondActivity.class); 
      i.putExtra("extra", 0); //if you want pass some info to other activity 
      startActivity(i); 
      break; 
     default: 
      break; 
    } 
} 

Auf diese Weise haben Sie mehr Onclick in einer Funktion verarbeiten kann, durch Komponente id Schalt denken