Get String[] from the other class - java

This is my main class:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Context con;
protected void onClickCityBreak(View v) {
String [] arrayCityBreak = con.getResources().getStringArray(R.array.citybreak);
Intent myintent = new Intent(MainActivity.this, ActivityTo.class);
myintent.putExtra("PLACES",arrayCityBreak);
startActivity(myintent);
}
}
And this is the class i want to send myintent array to:
public class ActivityTo extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
String[] arrayCityBreak = getIntent().getStringArrayExtra("PLACES");
ArrayAdapter<String> adapterCityBreak = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayCityBreak);
ListView myview = getListView();
myview.setAdapter(adapterCityBreak);
}
}
My AndroidManifest.xml file:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ActivityTo">
</activity>
I can't find the problem in this. My application shuts down when i click the button to get to the new intent. It is because of the String[] i try to send, but I don't see why is won't work.
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ivarrreyna.assignment, PID: 2626
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
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.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
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.res.Resources android.content.Context.getResources()' on a null object reference
at com.ivarrreyna.assignment.MainActivity.onClickCityBreak(MainActivity.java:24)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
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) 
Application terminated.

There is no need to pass array to , since ActivityTo is also an activity so can fetch the resources array here too
public class ActivityTo extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
String [] arrayCityBreak = getResources().getStringArray(R.array.citybreak);
ArrayAdapter<String> adapterCityBreak = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayCityBreak);
ListView myview = getListView();
myview.setAdapter(adapterCityBreak);
}
}
To remove current error,
either do this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
con=this;
}
or
use this in MainActivity because con is not initialized
String [] arrayCityBreak = getResources().getStringArray(R.array.citybreak);
instead of
String [] arrayCityBreak = con.getResources().getStringArray(R.array.citybreak);

My application shuts down when i click the button to get to the new intent. It is because of the String[] i try to send, but I don't see why is won't work.
It is caused by this con, because the context is null:
Context con;
Use this like:
String [] arrayCityBreak = this.getResources().getStringArray(R.array.citybreak);
or
String [] arrayCityBreak = getResources().getStringArray(R.array.citybreak);

Related

Null pointer exception on 'android.content.Context.getApplicationInfo()' [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
I'm very new to android, just took it as a weekend development for fun, trying to make a very simple app and I'm having an error.
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gwallet">
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
Context context;
final TextView txtNewDailyDetailDescription = findViewById(R.id.txt_new_daily_detail_description);
final TextView txtNewDailyDetailSpend = findViewById(R.id.txt_new_daily_detail_spend);
final Button btnNewDailyDetail = findViewById(R.id.btn_new_daily_detail);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
txtNewDailyDetailSpend.setInputType(InputType.TYPE_CLASS_NUMBER);
btnNewDailyDetail.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
btnNewDailyDetailClick();
}
});
}
protected void btnNewDailyDetailClick() {
//Some irrelevant code.
}
}
Stack trace of the error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.gwallet, PID: 32507
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.gwallet/com.gwallet.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3027)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3264)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1955)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7091)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:164)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
at android.content.Context.obtainStyledAttributes(Context.java:677)
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:692)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:659)
at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:479)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:214)
at com.gwallet.MainActivity.<init>(MainActivity.java:18)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:41)
at android.app.Instrumentation.newActivity(Instrumentation.java:1219)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3015)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3264) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1955) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7091) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975) 
I do get that something that is not initialized is being tried to get in use, but I don't know what it is. I keep reading Activities documentation. Any help?
Let me know if this work!
public class MainActivity extends AppCompatActivity {
Context context;
TextView txtNewDailyDetailDescription;
TextView txtNewDailyDetailSpen;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtNewDailyDetailDescription=findViewById(R.id.txt_new_daily_detail_description);
txtNewDailyDetailSpend = findViewById(R.id.txt_new_daily_detail_spend);
btnNewDailyDetail = findViewById(R.id.btn_new_daily_detail);
context = this;
txtNewDailyDetailSpend.setInputType(InputType.TYPE_CLASS_NUMBER);
btnNewDailyDetail.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
btnNewDailyDetailClick();
}
});
}
protected void btnNewDailyDetailClick() {
//Some irrelevant code.
}
}

java.lang.IllegalStateException: Could not execute method for android:onClick when trying to click a button

When I click the buttons on my page, I get an "IllegalStateException" in my logcat. I'm new at Java development, so I don't really know if it's something simple that i'm doing wrong. All I'm trying to do is get the button to open the activity. It worked fine until i started playing with the fragments etc for the layout.
log cat
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.as1.app172, PID: 5621
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5637) 
at android.view.View$PerformClick.run(View.java:22429) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.as1.app172/com.as1.app172.LoginActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1805)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1523)
at android.app.Activity.startActivityForResult(Activity.java:4225)
at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
at android.app.Activity.startActivityForResult(Activity.java:4183)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:720)
at android.app.Activity.startActivity(Activity.java:4522)
at android.app.Activity.startActivity(Activity.java:4490)
at com.as1.app172.Import1.BtnLogin_Click(Import1.java:25)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:5637) 
at android.view.View$PerformClick.run(View.java:22429) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
java
package com.as1.app172;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class Import1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_import1);
}
public void BtnRegister_Click (View v) {
Intent i = new Intent (Import1.this, RegistrationActivity.class);
startActivity(i);
}
public void BtnLogin_Click(View v) {
Intent i = new Intent (Import1.this, LoginActivity.class);
startActivity(i);
}
}
Check your AndroidManifest.xml and make sure you are already declared the activity LoginActivity. <activity android:name=".view.acitvity.LoginActivity" />

My app crashes when execute Intent statement [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I learn android studio programing and I have problem in my programs,
when I want to go to new activity using intent statement my app crashed,
this is my main activity;
public class MainActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
}
public void onStart(){
super.onStart();
FirebaseUser CurrentUser = mAuth.getCurrentUser();
if(CurrentUser == null){
Intent StartIntent = new Intent(MainActivity.this,
StartActivity.class);
startActivity(StartIntent);
finish();
}
}
}
and this is manifest :
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
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>
<activity android:name=".StartActivity"
android:label="#string/app_name">
</activity>
there is no error message
but thit is what i have into android monitor :
08-23 21:50:33.507 11264-11264/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ruse.ayse.areyousmartenough, PID: 11264
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ruse.ayse.areyousmartenough/com.ruse.ayse.areyousmartenough.StartActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2378)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5433)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.ruse.ayse.areyousmartenough.StartActivity.onCreate(StartActivity.java:31)
at android.app.Activity.performCreate(Activity.java:5301)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2291)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2378) 
at android.app.ActivityThread.access$800(ActivityThread.java:155) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5433) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
at dalvik.system.NativeStart.main(Native Method) 
I solved this problem into another project by adding "intent-filter" into manifest file but it does not work with this project,
this is StartActivity :
public class StartActivity extends AppCompatActivity {
private Button mRegBtn ;
private Button mLoginBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
mRegBtn = (Button) findViewById(R.id.RegBtn);
mLoginBtn = (Button) findViewById(R.id.LoginBtn);
mRegBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent createAccount = new Intent(StartActivity.this, RegisterActivity.class);
startActivity(createAccount);
}
});
mLoginBtn.setOnClickListener(new View.OnClickListener(){ /////this is line 31
#Override
public void onClick(View view) {
Intent loginAccount = new Intent(StartActivity.this, LoginActivity.class);
startActivity(loginAccount);
}
});
}}
Your problem is at StartActivity.java:31 you are trying to read from a null reference.
By your comment, you look to miss getting the mLoginBtn from your Layout=
mLoginBtn = (Button) findViewById(R.id.btn_login);

execute method for android:onClick

I faced this errors when i click the button to display the ProgressDialog
03-08 14:29:00.169 8564-8564/com.mohammedmoaayed.test2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mohammedmoaayed.test2, PID: 8564
Theme: themes:{}
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21158)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5471)
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.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5204) 
at android.view.View$PerformClick.run(View.java:21158) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5471) 
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 'void android.app.ProgressDialog.setTitle(java.lang.CharSequence)' on a null object reference
at com.mohammedmoaayed.test2.MainActivity.buclick(MainActivity.java:26)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:5204) 
at android.view.View$PerformClick.run(View.java:21158) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5471) 
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) 
and this is my java code
package com.mohammedmoaayed.test2;
import android.app.Activity;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
Activity myac;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myac=this;
}
mythread t1;
ProgressDialog bardown;
public void buclick(View view) {
bardown.setTitle("Downloaing ");
bardown.setMessage("Download is >>>>");
bardown.setProgressStyle(bardown.STYLE_HORIZONTAL);
bardown.setProgress(0);
bardown.setMax(20);
bardown.show();
t1 = new mythread();
t1.start();
}
class mythread extends Thread {
public void run (){
while (bardown.getProgress()<20) {
myac.runOnUiThread(new Runnable() {
#Override
public void run() {
bardown.incrementProgressBy(1);
}
});
try {
Thread.sleep(1000);
} catch (Exception ex) {}
}
myac.runOnUiThread(new Runnable() {
#Override
public void run() {
bardown.dismiss();
}
});
}
}
}
xml
<?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="com.mohammedmoaayed.test2.MainActivity">
<Button
android:id="#+id/button2"
android:layout_width="395dp"
android:layout_height="587dp"
android:onClick="buclick"
android:text="Button"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
You haven't inialiase your ProgressDialog so Initialize your ProgressDialog "bardown"
Initialize in Activity
bardown = new ProgressDialog(this);// write this line
Initialize in Fragment
bardown = new ProgressDialog(getActivity());// write this line
for e.g.
public void buclick(View view) {
bardown = new ProgressDialog(this);// write this line
bardown.setTitle("Downloaing ");
bardown.setMessage("Download is >>>>");
bardown.setProgressStyle(bardown.STYLE_HORIZONTAL);
bardown.setProgress(0);
bardown.setMax(20);
bardown.show();
t1 = new mythread();
t1.start();
}
you are calling setTitle() on null object.you have to instantiate it first;
bardown = (ProgressBar)findViewById(R.id.bardown);
or if you creating it dynamically use this
bardown - new ProgressBar(this);
You haven't initialized you ProgressDialog. Initialize it before using it. Add below line before calling the setTitle method.
bardown = new ProgressDialog(MainActivity.this);
use this
initialize dialog instant
ProgressDialog dialog = new ProgressDialog(Activity_Map_Advance_Tracking.this);dialog.setMessage("Loading....");
dialog.show();

Getting error when changing other activity's TextView

I'm a beginner at Android and Java and I have an error when trying to change an other activity's TextView. I've created MainActivity, and another one (PlayerNames).
I've set PlayerNames to be the Launcher activity, maybe the error is that, I don't know. I post the error and the code:
FATAL EXCEPTION: main
Process: com.example.android.mapamundi, PID: 1423
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.mapamundi/com.example.android.mapamundi.PlayerNames}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:68)
at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:146)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:41)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:190)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:172)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:512)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:184)
at com.example.android.mapamundi.PlayerNames.<init>(PlayerNames.java:12)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1606)
at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
MAINACTIVITY.JAVA
Well, on MainActivity I only cast the TextViews with public static TextView tv1, so I don't post it
PLAYERNAMES.JAVA
public class PlayerNames extends AppCompatActivity implements View.OnClickListener {
Button accept = (Button) findViewById(R.id.changeNames);
EditText playerInput1 = (EditText) findViewById(R.id.PlayerNameInput1);
EditText playerInput2 = (EditText) findViewById(R.id.PlayerNameInput2);
EditText playerInput3 = (EditText) findViewById(R.id.PlayerNameInput3);
EditText playerInput4 = (EditText) findViewById(R.id.PlayerNameInput4);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player_names);
accept.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case (R.id.changeNames):
MainActivity.playerName1.setText(playerInput1.toString());
MainActivity.playerName2.setText(playerInput2.toString());
MainActivity.playerName3.setText(playerInput3.toString());
MainActivity.playerName4.setText(playerInput4.toString());
Intent intent = new Intent(PlayerNames.this, MainActivity.class);
startActivity(intent);
}
}
}
I post AndroidManifest.xml, just in case I have an error or something that causes the error
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="com.example.android.mapamundi.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".PlayerNames">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Can someone please tell me what am I doing wrong? TKS!!
You are calling findViewById() to early, before your activity is fully created. It cannot be in a field initialiser. It has to be in (or after) onCreate(). And also after setContentView(), because you first have to set the content of your activity, before you can try to find views inside of it.

Categories