0

I'm trying to build an app with four different activities.

On my start page, there are 4 buttons. When you click on a button you should be directed to the correspondent activity.

Although my code has no syntax errors, my app always crashes when I hit a single button.

Can some please tell me what is wrong with my code?

My MainActivity

package com.group6.travlhoe;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.design.widget.BaseTransientBottomBar;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements 
View.OnClickListener {

Button btnTransport, btnUnterkunft, btnAllgemein, btnRechnung;

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

    btnTransport = (Button) findViewById(R.id.Transport);
    btnAllgemein = (Button) findViewById(R.id.Allgemein);
    btnUnterkunft = (Button) findViewById(R.id.Unterkunft);
    btnRechnung = (Button) findViewById(R.id.Rechnung);

    btnTransport.setOnClickListener(this);
    btnRechnung.setOnClickListener(this);
    btnAllgemein.setOnClickListener(this);
    btnUnterkunft.setOnClickListener(this);
}


@Override
public void onClick(View v) {
    if (v.getId() == R.id.Allgemein) {
        startActivity(new Intent(MainActivity.this, Allgemein.class));
    } else if (v.getId() == R.id.Transport) {
        startActivity(new Intent(MainActivity.this, Transport.class));
    } else if (v.getId() == R.id.Unterkunft) {
        startActivity(new Intent(MainActivity.this, Unterkunft.class));
    } else if (v.getId() == R.id.Rechnung) {
        startActivity(new Intent(MainActivity.this, Rechnung.class));
    }
    }
}

One of the classes the button should direct to:

package com.group6.travlhoe;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;



public class Allgemein extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_allgemein);
    }
}

My Error Log-File:

06-22 17:33:17.073 1414-1600/? E/AudioFlinger: not enough memory for AudioTrack size=131296
06-22 17:33:17.073 1414-1600/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block?
06-22 17:33:17.073 2269-10242/? E/AudioRecord: AudioFlinger could not create record track, status: -12
06-22 17:33:17.074 2269-10242/? E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12.
06-22 17:33:17.074 2269-10242/? E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
06-22 17:33:22.111 1414-1599/? E/AudioFlinger: not enough memory for AudioTrack size=131296
06-22 17:33:22.111 1414-1599/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block?
06-22 17:33:22.111 2269-10242/? E/AudioRecord: AudioFlinger could not create record track, status: -12
06-22 17:33:22.120 2269-10242/? E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12.
06-22 17:33:22.120 2269-10242/? E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
06-22 17:33:26.330 1517-1530/? E/memtrack: Couldn't load memtrack module
06-22 17:33:27.159 1414-1499/? E/AudioFlinger: not enough memory for AudioTrack size=131296
06-22 17:33:27.159 1414-1499/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block?
06-22 17:33:27.159 2269-10242/? E/AudioRecord: AudioFlinger could not create record track, status: -12
06-22 17:33:27.165 2269-10242/? E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12.
06-22 17:33:27.166 2269-10242/? E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
06-22 17:33:32.203 1414-1499/? E/AudioFlinger: not enough memory for AudioTrack size=131296
06-22 17:33:32.203 1414-1499/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block?
06-22 17:33:32.203 2269-10242/? E/AudioRecord: AudioFlinger could not create record track, status: -12
06-22 17:33:32.211 2269-10242/? E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12.
06-22 17:33:32.211 2269-10242/? E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
06-22 17:33:37.259 1414-1599/? E/AudioFlinger: not enough memory for AudioTrack size=131296
06-22 17:33:37.259 1414-1599/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block?
06-22 17:33:37.260 2269-10242/? E/AudioRecord: AudioFlinger could not create record track, status: -12
06-22 17:33:37.265 2269-10242/? E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12.
06-22 17:33:37.265 2269-10242/? E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
06-22 17:33:42.359 1414-1414/? E/AudioFlinger: not enough memory for AudioTrack size=131296
06-22 17:33:42.360 1414-1414/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block?
06-22 17:33:42.360 2269-10242/? E/AudioRecord: AudioFlinger could not create record track, status: -12
06-22 17:33:42.366 2269-10242/? E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12.
06-22 17:33:42.368 2269-10242/? E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
06-22 17:33:47.604 1414-1599/? E/AudioFlinger: not enough memory for AudioTrack size=131296
06-22 17:33:47.606 1414-1599/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block?
06-22 17:33:47.652 2269-10242/? E/AudioRecord: AudioFlinger could not create record track, status: -12
06-22 17:33:47.824 2269-10242/? E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12.
06-22 17:33:47.825 2269-10242/? E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
06-22 17:33:47.835 2269-10242/? E/ActivityThread: Failed to find provider info for com.google.android.apps.gsa.testing.ui.audio.recorded
06-22 17:33:48.125 1517-1530/? E/memtrack: Couldn't load memtrack module
06-22 17:33:48.760 1417-1573/? E/installd: Failed to delete /data/app/vmdl1116498233.tmp: No such file or directory
06-22 17:33:48.930 2154-4609/? E/NetworkScheduler.SR: Invalid parameter app
06-22 17:33:48.930 2154-4609/? E/NetworkScheduler.SR: Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
06-22 17:33:49.162 2656-2656/? E/Finsky: [2] com.google.android.finsky.wear.bl.a(3): onConnectionFailed: ConnectionResult{statusCode=API_UNAVAILABLE, resolution=null, message=null}
06-22 17:33:49.272 2154-4609/? E/NetworkScheduler.SR: Invalid parameter app
06-22 17:33:49.272 2154-4609/? E/NetworkScheduler.SR: Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
06-22 17:33:49.547 1342-1342/? E/hw-IPCThreadState: binder thread pool (1 threads) starved for 130 ms
06-22 17:33:49.752 2269-9548/? E/ContentStoreEUAS: Failed to commit the deferred actions
06-22 17:33:51.037 1352-1368/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
06-22 17:33:51.525 2154-4609/? E/ctxmgr: [AppIntervalImpl]closeInterval: ongoing
06-22 17:33:51.545 2154-2383/? E/UidImportanceUtils: exception when invoking removeOnUidImportanceListener
                                                     java.lang.reflect.InvocationTargetException
                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                         at alvy.a(:com.google.android.gms:1123)
                                                         at akva.a(:com.google.android.gms:44820)
                                                         at lsx.a(:com.google.android.gms:246)
                                                         at akuz.run(:com.google.android.gms:2812)
                                                         at android.os.Handler.handleCallback(Handler.java:769)
                                                         at android.os.Handler.dispatchMessage(Handler.java:98)
                                                         at alxb.dispatchMessage(:com.google.android.gms:3028)
                                                         at android.os.Looper.loop(Looper.java:164)
                                                         at android.os.HandlerThread.run(HandlerThread.java:65)
                                                      Caused by: java.lang.IllegalArgumentException: Listener not registered: alvz@2c93e02
                                                         at android.app.ActivityManager.removeOnUidImportanceListener(ActivityManager.java:3628)
                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                         at alvy.a(:com.google.android.gms:1123) 
                                                         at akva.a(:com.google.android.gms:44820) 
                                                         at lsx.a(:com.google.android.gms:246) 
                                                         at akuz.run(:com.google.android.gms:2812) 
                                                         at android.os.Handler.handleCallback(Handler.java:769) 
                                                         at android.os.Handler.dispatchMessage(Handler.java:98) 
                                                         at alxb.dispatchMessage(:com.google.android.gms:3028) 
                                                         at android.os.Looper.loop(Looper.java:164) 
                                                         at android.os.HandlerThread.run(HandlerThread.java:65) 
06-22 17:33:53.354 1517-1608/? E/TaskPersister: File error accessing recents directory (directory doesn't exist?).
06-22 17:34:00.005 1517-1530/? E/memtrack: Couldn't load memtrack module

Android Manifest File:

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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>
    </application>

</manifest>
Dayan
  • 7,634
  • 11
  • 49
  • 76
ste92
  • 434
  • 9
  • 23
  • 4
    Did you check logcat for error messages? – Durus Jun 22 '17 at 14:55
  • Hi, yes but there were no error messages neither in the code nor in Android Monitor – ste92 Jun 22 '17 at 14:57
  • 1
    Make sure you don't have any filters on because it has to be showing something if it is crashing. And that's the only way to get help because there is nothing obviously wrong in the code. – codeMagic Jun 22 '17 at 14:58
  • Could you replace all AppCompatActivity with Activity ? – Abdullah Tellioglu Jun 22 '17 at 14:59
  • @codeMagic I also did check this but no messages are shown. – ste92 Jun 22 '17 at 15:01
  • @AbdullahTellioglu I replaced this but no difference – ste92 Jun 22 '17 at 15:01
  • What code do you have in the onClick? You could try wrapping it in a try/catch and using Log.e() to write it to logcat or show the error as a toast. – Durus Jun 22 '17 at 15:03
  • if your app crashs there is a logcat error report for sure. I guess you have set filters that you don´t know. Anyway, be sure you have registerred all activities inside manifest and be sure the layout that you set with `setContentView(R.layout.activity_main);` has the views that you are reefering. – Opiatefuchs Jun 22 '17 at 15:04
  • Make sure the device is connected to adb, no filters in Log Level or the Search filter, run again, make it crash, post the stacktrace. It's there – codeMagic Jun 22 '17 at 15:05
  • I added the xml-file of the mainActivity. There all buttons are defined. – ste92 Jun 22 '17 at 15:09
  • @Durus, I already tried the try/catch block, but the same problem occurs – ste92 Jun 22 '17 at 15:10
  • @S.Tzavelas Wrap the `startActivity` with try/catch. Add a log statement inside the `catch` and put a debug breakpoint, you can then step through your code. When the exception is thrown, it will be caught and you can inspect it and see the message. Then report back. More than likely, one of your activity's `onCreate()` is throwing an exception, and thus erroring out. – Dayan Jun 22 '17 at 15:13
  • Just to verify; you added a try/catch in the onClick and nothing was logged or toasted and the app still crashed? Was it a force close? – Durus Jun 22 '17 at 15:13
  • @codeMagic I checked the filter in Android Monitor, there are none. Is there another possibility to check the logs than Android Monitor? – ste92 Jun 22 '17 at 15:14
  • @Dayan As I'm quite new to programming I am not sure how to write this. Could post an example here in the comments? – ste92 Jun 22 '17 at 15:16
  • Check this https://developer.android.com/studio/command-line/logcat.html and this https://stackoverflow.com/questions/3707880/using-adb-logcat-with-a-real-phone-and-not-the-emulator – codeMagic Jun 22 '17 at 15:18
  • Include your Android manifest file, please. – Dayan Jun 22 '17 at 15:39
  • I'm pretty sure those error logs are non-related to your application. I updated my answer, you are missing the activities from your manifest. – Dayan Jun 22 '17 at 15:45
  • @Dayan What do you mean? Do you have an idea for the problem? But thank you so far ! – ste92 Jun 22 '17 at 15:57
  • @S.Tzavelas Look at my answer... Your posted manifest shows that you are missing activities in the manifest file. If you try to go into an activity like you are doing now in code, it won't work. Your activities must be added to the Android manifest file. – Dayan Jun 22 '17 at 16:02

1 Answers1

1

You are missing the activities from your manifest. When you create an activity it must be added here as well.

I recommend reading the Android Application Fundamentals.

 <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=".Allgemein"
    android:launchMode="singleTop"/>

Also, In the example you provided for your activity, you are not overriding the onCreate method. Make sure all your Activities have this change.

public class Allgemein extends AppCompatActivity {

    @Override //You are missing this.
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_allgemein);
    }
}
Dayan
  • 7,634
  • 11
  • 49
  • 76
  • Updated my answer, make sure you add the activities to your manifest as well. Is your code compiling fine? Can you build without errors? – Dayan Jun 22 '17 at 15:32
  • But my start acitivty should be the main activity. And I can not add two activities in the manifest.xml – ste92 Jun 22 '17 at 15:59
  • @S.Tzavelas Yes... you can add as many activities as you'll like to your manifest file. You can also add Receivers and Services and a bunch of other goodies. Without the activities in the manifest, you won't be able to launch it. – Dayan Jun 22 '17 at 16:03
  • THANK YOU !! You're genius !! – ste92 Jun 22 '17 at 16:08
  • @S.Tzavelas You are welcome, no genius here :) just had more experience than you with Android. I still got plenty to learn. Have a good one. – Dayan Jun 22 '17 at 16:12