This is what the main screen of my app is supposed to look like I have that down I just have to figure out animations but the problem is I can't get the app to open because it crashes and I can't figure out what is causing my crash I used views for the lines at the top and just a regular android background change in the xml code for the mountain picture wondering if any of that might be the problem here is the xml code:
<androidx.constraintlayout.widget.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:id="@ id/mountains"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/nz"
tools:context=".MainActivity">
<TextView
android:id="@ id/titleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/baloo_thambi"
android:text="NZ"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:textColor="@color/white"
android:textSize="120sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@ id/purple_medium"
android:layout_width="17dp"
android:layout_height="155dp"
android:layout_marginEnd="54dp"
android:background="@drawable/purple_gradient"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@ id/green"
android:layout_width="17dp"
android:layout_height="235dp"
android:layout_marginEnd="13dp"
android:background="@drawable/green_gradient"
app:layout_constraintEnd_toStartOf="@ id/purple_medium"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@ id/orange_long"
android:layout_width="17dp"
android:layout_height="215dp"
android:layout_marginEnd="13dp"
android:background="@drawable/orange_gradient"
app:layout_constraintEnd_toStartOf="@ id/green"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@ id/purple_long"
android:layout_width="17dp"
android:layout_height="265dp"
android:layout_marginEnd="13dp"
android:background="@drawable/purple_gradient"
app:layout_constraintEnd_toStartOf="@ id/orange_long"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@ id/orange_short"
android:layout_width="17dp"
android:layout_height="110dp"
android:layout_marginEnd="13dp"
android:background="@drawable/orange_gradient"
app:layout_constraintEnd_toStartOf="@ id/purple_long"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@ id/purple_short"
android:layout_width="17dp"
android:layout_height="60dp"
android:layout_marginEnd="13dp"
android:background="@drawable/purple_gradient"
app:layout_constraintEnd_toStartOf="@ id/orange_short"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@ id/subText"
android:layout_width="84dp"
android:layout_height="31dp"
android:layout_marginEnd="164dp"
android:layout_marginBottom="66dp"
android:fontFamily="@font/allura"
android:text="Next NZ"
android:textColor="@android:color/darker_gray"
android:textSize="22sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Whenever I try to check out what the problem in the logcat is this is what it tells me:
2022-02-14 12:26:47.614 20077-20077/com.revolution.covidnz E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.revolution.covidnz, PID: 20077
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.revolution.covidnz/com.revolution.covidnz.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
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 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:107)
at com.revolution.covidnz.MainActivity.<init>(MainActivity.java:22)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
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)
Im also going to include the main activity code in case something in my code is making the app crash:
public class MainActivity extends AppCompatActivity {
private final int SPLASH_DISPLAY_LENGTH = 2000;
Animation slide_down = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.slide_down);
Animation slide_up = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.slide_up);
View gSlideDown, gSlideUp;
View pShortSlideDown, pShortSlideUp;
View pMediumSlideDown, pMediumSlideUp;
View pLongSlideDown, pLongSlideUp;
View oShortSlideDown, oShortSlideUp;
View oLongSlideDown, oLongSlideUp;
TextView textView, textView2;
RelativeLayout relativeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
textView2 = findViewById(R.id.subText);
gSlideDown = findViewById(R.id.green);
gSlideUp = findViewById(R.id.green);
pShortSlideDown = findViewById(R.id.purple_short);
pShortSlideUp = findViewById(R.id.purple_short);
pMediumSlideDown = findViewById(R.id.purple_medium);
pMediumSlideUp = findViewById(R.id.purple_medium);
pLongSlideDown = findViewById(R.id.purple_long);
pLongSlideUp = findViewById(R.id.purple_long);
oShortSlideDown = findViewById(R.id.orange_short);
oShortSlideUp = findViewById(R.id.orange_short);
oLongSlideDown = findViewById(R.id.orange_long);
oLongSlideUp = findViewById(R.id.orange_long);
relativeLayout = findViewById(R.id.mountains);
final ViewGroup transitionsContainer = (ViewGroup) findViewById(R.id.transition_position);
final TextView text = (TextView) transitionsContainer.findViewById(R.id.text);
gSlideUp.setOnClickListener(new View.OnClickListener() {
boolean visible;
@Override
public void onClick(View v) {
TransitionManager.beginDelayedTransition(transitionsContainer);
visible = !visible;
text.setVisibility(visible ? View.VISIBLE : View.GONE);
}
});
gSlideUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Slide slide = new Slide();
slide.setSlideEdge(Gravity.START);
TransitionManager.beginDelayedTransition(relativeLayout, slide);
textView.setVisibility(View.VISIBLE);
}
});
gSlideUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Slide slide = new Slide();
slide.setSlideEdge(Gravity.END);
TransitionManager.beginDelayedTransition(relativeLayout, slide);
gSlideDown.setVisibility(View.VISIBLE);
}
});
final android.os.Handler handler = new android.os.Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(MainActivity.this, Second.class);
startActivity(intent);
overridePendingTransition(R.transition.fade_in,R.transition.fade_out);
}
}, 3000);
}
}
CodePudding user response:
See context can be of anything, so if you want the context of activity then use ActivityName.context, other the context you receive may not be of the activity.
One tip: Please study before making apps directly. A programmer is one who reads the documentation and creates apps.
CodePudding user response:
So as you error states, you are trying to invoke getContext() on views/properties that are still null or have not been even created and that is what is happening here. You are trying to access the views and pass them the context even before they are created.
All the views are created during the onCreate method call and you can use them inside this method or after this method has been executed.
So try passing in the context to the views inside a meaningful method where your views are not null
For ref read the activity lifecycle: https://developer.android.com/guide/components/activities/activity-lifecycle