Issue
I know there are a lot of questions about crashing upon using setContentView, but I haven't found any with issues as basic as mine.
This is basically my first day using Android Studio. I'm following along Paulo Dichone's Android course on Udemy currently. One of the first things he has us do is change the starting layout to one we make ourselves. Even changing less than a line is causing my emulated program to crash in Genymotion.
Here is the code for MainActivity.java. The only thing I changed from the original(which works) is activity_main as an argument to setContentView (in the second line of onCreate), which I changed to mylayout.
package welcomeandroid.myandroid.com.welcomeandroid;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The XML code for my custom layout 'mylayout' is the following:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Please work"
android:id="@+id/textView2"
android:layout_gravity="center_horizontal" />
The logcat messages from running the program are as follows:
11-12 23:28:17.584 2244-2244/? I/art: Late-enabling -Xcheck:jni
11-12 23:28:18.026 2244-2244/welcomeandroid.myandroid.com.welcomeandroid D/AndroidRuntime: Shutting down VM
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: FATAL EXCEPTION: main
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: Process: welcomeandroid.myandroid.com.welcomeandroid, PID: 2244
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{welcomeandroid.myandroid.com.welcomeandroid/welcomeandroid.myandroid.com.welcomeandroid.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:151)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5254)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:100)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:93)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at android.support.v7.internal.app.ToolbarActionBar.<init>(ToolbarActionBar.java:78)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:206)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at welcomeandroid.myandroid.com.welcomeandroid.MainActivity.onCreate(MainActivity.java:19)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5990)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:151)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5254)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
11-12 23:28:22.531 2244-2244/welcomeandroid.myandroid.com.welcomeandroid I/Process: Sending signal. PID: 2244 SIG: 9
I would really appreciate any help. It's a little bit discouraging to fall into such trouble having changed only a tiny bit of code in a way instructed to by the instructor.
Solution
You have to remove the following lines from your code:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
because you have no Toolbar and FloatingActionButton included in your layout.
Answered By - Benjamin Scharbau
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.