Warum Android-App stürzt ab beim Versuch, starten Sie eine neue Aktivität

So, ich bin versucht, das design der app, um Sie zu einem anderen Bildschirm mit der Absicht, aber es stürzt immer ab.

Ich bin mit imageButton-Steuerelement mit einem ein-Klick-listener. Ich fügte hinzu, eine zweite java-Modul für die Ziel-Seite ein layout und die Applikation Hinzugefügt, um die manifest-Datei. Bitte lassen Sie mich wissen, wenn Sie wollen, mich zu diesem mehr informativen

Logcat Fehler

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bet/com.example.bet.MainActivity}: java.lang.NullPointerException

     Caused by: java.lang.NullPointerException
        at com.example.bet.MainActivity.onCreate(MainActivity.java:42)

Habe ich festgestellt, welche Zeile 42

Haupttätigkeit primär-code Aktualisiert

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;

import android.view.ViewGroup;
import android.os.Build;

import android.view.View;
import android.content.Intent;  //Launching other pages
import android.app.Activity; //Activity Launcher
import android.widget.ImageButton; //Image buttons
import android.view.View.OnClickListener;

public class MainActivity extends ActionBarActivity {

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }


}



@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;
}

@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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);


        ////////////////////////
        //Initialize forms of the ImageButtons
        ////////////////////////
        /* Forms Button */
        ImageButton pcaButton = (ImageButton)findViewById(R.id.pcaButton);

        ////////////////////////
        //Attach the Listener (On Click Event handler)
        ////////////////////////

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

        return rootView;
    }
}

}

Manifest-Datei

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.bet.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.bet.pcaActivity"
        android:label="@string/title_activity_pca" >
    </activity>
</application>

String.xml

<?xml version="1.0" encoding="utf-8"?>

<string name="app_name">bet</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="title_activity_pca">pca</string>

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" >

fragment_main.xml

<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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity$PlaceholderFragment"
android:background="@drawable/abc_ab_transparent_light_holo">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true">

    <ImageButton
        android:id="@+id/pcaButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/pca1"
        android:layout_gravity="center_horizontal"
        />
</LinearLayout>

  • Überprüfen Sie, ob pcaButton null ist.
  • direkt am Ziel, ist es. Aber warum ist das so? Ich importierte android.- widget.ImageButton
  • Tut activity_main.xml einen Blick mit @+id/pcaButton? Versuchen Sie auch, die Reinigung und den Wiederaufbau Ihres Projekts.
  • Ja, aber nicht genau in activity_main.xml es ist in den anderen layout-Datei assoicated mit MainActivity, fragment_main.xml (es ist, wie studio angefangen mich mit). Habe versucht, die Reinigung und die Gebäude wieder, nicht den trick tun. Hinzugefügt beide Dateien text in Frage zu stellen
InformationsquelleAutor Iancovici | 2013-11-29
Schreibe einen Kommentar