Добрый день.
Сделал всё по уроку. Но при попытке запуска приложения через эмулятор выскакивает сообщение о том, что приложение не может быть запущено. При установке apk файла на телефон приложение тоже не открывается.
Acivity Main:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="113dp"
android:layout_marginTop="166dp"
android:onClick="onClick"
android:text="Поздороваться"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="163dp"
android:layout_marginTop="289dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="115dp"
android:layout_marginTop="30dp"
android:text="Считаем ворон"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<Button
android:id="@+id/button_counterCats"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="116dp"
android:layout_marginTop="26dp"
android:text="Считаем котов"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="163dp"
android:layout_marginTop="410dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Java:
package me.brainstory.ravencounter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private int mCount = 0;
private int mCatsCount = 0;
TextView mInfoTextView = findViewById(R.id.textView);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mCrowsCounterButton = findViewById(R.id.button_counter);
mCrowsCounterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mInfoTextView.setText("Я насчитал " + ++mCount + " ворон");
}
});
final TextView mInfoCatsView = findViewById(R.id.textView2);
Button mCatsCounterButton = findViewById(R.id.button_counterCats);
mCatsCounterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mInfoCatsView.setText("Я насчитал " + ++mCatsCount + " ворон");
}
});
}
public void onClick(View view) {
mInfoTextView.setText("Hello Kitty!");
}
}
Error Log:
04/08 15:02:06: Launching app
$ adb install-multiple -r -t D:\AndroidStudioProjects\RavenCounterNew\app\build\intermediates\resources\instant-run\debug\resources-debug.apk D:\AndroidStudioProjects\RavenCounterNew\app\build\intermediates\split-apk\debug\dep\dependencies.apk D:\AndroidStudioProjects\RavenCounterNew\app\build\intermediates\split-apk\debug\slices\slice_0.apk D:\AndroidStudioProjects\RavenCounterNew\app\build\intermediates\split-apk\debug\slices\slice_1.apk D:\AndroidStudioProjects\RavenCounterNew\app\build\intermediates\split-apk\debug\slices\slice_2.apk D:\AndroidStudioProjects\RavenCounterNew\app\build\intermediates\split-apk\debug\slices\slice_3.apk D:\AndroidStudioProjects\RavenCounterNew\app\build\intermediates\split-apk\debug\slices\slice_4.apk D:\AndroidStudioProjects\RavenCounterNew\app\build\intermediates\split-apk\debug\slices\slice_5.apk D:\AndroidStudioProjects\RavenCounterNew\app\build\intermediates\split-apk\debug\slices\slice_6.apk D:\AndroidStudioProjects\RavenCounterNew\app\build\intermediates\split-apk\debug\slices\slice_7.apk D:\AndroidStudioProjects\RavenCounterNew\app\build\intermediates\split-apk\debug\slices\slice_8.apk D:\AndroidStudioProjects\RavenCounterNew\app\build\intermediates\split-apk\debug\slices\slice_9.apk D:\AndroidStudioProjects\RavenCounterNew\app\build\intermediates\instant-run-apk\debug\app-debug.apk
Split APKs installed
$ adb shell am start -n "me.brainstory.ravencounternew/me.brainstory.ravencounternew.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Connected to process 6334 on device emulator-5554
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/InstantRun: starting instant run server: is main process
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: me.brainstory.ravencounternew, PID: 6334
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{me.brainstory.ravencounternew/me.brainstory.ravencounternew.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2578)
at me.brainstory.ravencounternew.MainActivity.<init>(MainActivity.java:15)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Application terminated.
В чем может быть проблема?
Спасибо!