Why is my program crashing when button is clicked? - java

I am trying to make a login and register app where the register details are stored on a database within android studio. Can you please help me as when i run my program the next page does not appear (shown code for one page that won't appear from main menu which is the register activity). If you also know how to make a sql database for a login and register in android studio would be much appreciated. Thanks
Main Activity:
package com.example.emily.loginapp;
import android.content.DialogInterface;
import android.content.Intent;
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;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button Login, Register, Delete, Update;
int status = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Login = (Button) findViewById(R.id.Login);
Register = (Button) findViewById(R.id.Register);
Delete = (Button) findViewById(R.id.Delete);
Update = (Button) findViewById(R.id.Update);
Login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
status = 1;
Bundle b = new Bundle();
b.putInt("status", status);
Intent i = new Intent("login_filter");
i.putExtras(b);
startActivity(i);
}
});
Register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent("register_filter");
startActivity(i);
}
});
Update.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
status = 2;
Bundle b = new Bundle();
b.putInt("status", status);
Intent i = new Intent("login_filter");
i.putExtras(b);
startActivity(i);
}
});
Delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
status= 3;
Bundle b = new Bundle();
b.putInt("status", status);
Intent i = new Intent("login_filter");
i.putExtras(b);
startActivity(i);
}
});
}
}
Register:
package com.example.emily.loginapp;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Button;
import android.widget.Toast;
public class RegisterActivity extends AppCompatActivity {
EditText USER_NAME, USER_PASS, CON_PASS;
String user_name, user_pass,con_pass;
Button REG;
Context ctx = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_register);
USER_NAME = (EditText) findViewById(R.id.reg_user);
USER_PASS = (EditText)findViewById(R.id.reg_pass);
CON_PASS = (EditText) findViewById(R.id.con_pass);
REG = (Button) findViewById(R.id.user_reg);
REG.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
user_name = USER_NAME.getText().toString();
user_pass = USER_PASS.getText().toString();
con_pass = CON_PASS.getText().toString();
if (!(user_pass.equals(con_pass))){
Toast.makeText(getBaseContext(),"Passwords are not matching", Toast.LENGTH_LONG).show();;
USER_NAME.setText("");
USER_PASS.setText("");
CON_PASS.setText("");
}else{
DatabaseOperations DB = new DatabaseOperations(ctx);
DB.putInformation(DB, user_name, user_pass);
Toast.makeText(getBaseContext(),"Registration success", Toast.LENGTH_LONG).show();
finish();
}
}
});
}
}
LOGCAT:
03-09 22:33:25.504 2672-2672/com.example.emily.loginapp D/AndroidRuntime: Shutting down VM
03-09 22:33:25.537 2672-2672/com.example.emily.loginapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.emily.loginapp, PID: 2672
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=register_filter }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1798)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)
at android.app.Activity.startActivityForResult(Activity.java:3917)
at android.app.Activity.startActivityForResult(Activity.java:3877)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:784)
at android.app.Activity.startActivity(Activity.java:4200)
at android.app.Activity.startActivity(Activity.java:4168)
at com.example.emily.loginapp.MainActivity$2.onClick(MainActivity.java:43)
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)
03-09 22:33:36.952 2672-2679/com.example.emily.loginapp W/art: Suspending all threads took: 29.841ms
manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.emily.loginapp">
<application
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"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DeleteActivity"
android:label="#string/title_activity_delete"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".RegisterActivity"
android:label="#string/title_activity_register"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".UpdateActivity"
android:label="#string/title_activity_update"
android:theme="#style/AppTheme.NoActionBar"></activity>
</application>
</manifest>

You are using using an intent flag(register_filter) which is not available on the device thats why it is crashing the application...!
The constructor of the Intent class should be call with the context of
the activity/app and the activity on which you have to move(If you are
not using an activity which is not available on the device)...!
You should use the below code to access the RegisterActivity,
Intent i = new Intent(MainActivity.this, RegistrationActivity.class);
startActivity(i);

You need to create your intent using a class extending Activity.
For example like this:
Intent i = new Intent(this, SomeActivity.class);
startActivity(i);
where this is current Context and SomeActivity is the activity you want to start.
Take a look at the docs:
http://developer.android.com/training/basics/firstapp/starting-activity.html

To call a new Activity from the current Activity you need to to pass the context of the current activity and the "NewClass" which you want to open as parameters.
For Eg :
Intent i = new Intent(CurrentClassName.this, TheClassToBeOpened.class);
startActivity(i);

IMHO using actions is overkill for what you want to achieve. You should just ude something like:
Intent i = new Intent(getContext(), RegistrationActivity.class);
startActivity(i);
(you can use this instead getContext() too).
If you do really want to use actions, then you must define the intent filter register_filter in your activity. Something along the lines:
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="login_filter" />
</intent-filter>
</activity>
should do the trick.
For reference, please have a look at the documentation and this question.

Related

My java code could not search for nearby Bluetooth Devices

I have written a code to list nearby Bluetooth Devices but my code is unable to detect any.
According to my code, the action just starts and end and not print any device in logs when I do String action = intent.getAction(); Log.i("Action",action);
in onRecieve() under BroadCastReciever.
Here is my MainActivity.java :
package com.example.findbluetoothdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
TextView statusTextView;
ListView listView;
Button searchButton;
ArrayList<String> bluetoothDevices = new ArrayList<>();
ArrayList<String> addresses = new ArrayList<>();
ArrayAdapter arrayAdapter;
BluetoothAdapter bluetoothAdapter;
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i("Action",action);
if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
statusTextView.setText("Finished");
searchButton.setEnabled(true);
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String name = device.getName();
String address = device.getAddress();
String rssi = Integer.toString(intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE));
Log.i("Device Found","Nmae :"+name+" Address :"+address+" RSSI :"+rssi);
if (!addresses.contains(address)) {
addresses.add(address);
String deviceString = "";
if (name==null || name.equals("")) {
deviceString = address+" - RSSI "+rssi+"dBm";
} else {
deviceString = name+" - RSSI "+rssi+"dBm";
}
bluetoothDevices.add(deviceString);
arrayAdapter.notifyDataSetChanged();
}
}
}
};
public void searchFunction(View view) {
bluetoothDevices.add("ADDING NEW DEVICES...");
statusTextView.setText("Searching...");
searchButton.setEnabled(false);
arrayAdapter.notifyDataSetChanged();
bluetoothDevices.clear();
addresses.clear();
bluetoothAdapter.startDiscovery();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
statusTextView = findViewById(R.id.statusTextView);
listView = findViewById(R.id.listView);
searchButton = findViewById(R.id.button);
arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,bluetoothDevices);
listView.setAdapter(arrayAdapter);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(broadcastReceiver ,intentFilter);
}
}
And here is AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.findbluetoothdemo">
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission>
<uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>
<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>
You need to ask for permission if you haven't done already.
int premissionCehck = context.checkSelfPermission("Manifest.premission.ACCES_FINE_LOCATION");
premissionCehck += context.checkSelfPermission("Manifest.premission.ACCES_FINE_LOCATION");
if (premissionCehck != 0) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001);
}
Take Care, If you already denied permission once, the permission request doesn't appear again, so you need to give it free in your android settings.

Why is my TabLayout giving me a NullPointerException, and how do I fix it? [duplicate]

When my SplashActivity opens the LoginActivity my app crashes.
The following is my SplashActivity.java:
package com.example.android.appName;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import java.util.Timer;
import java.util.TimerTask;
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
}, 1500);
}
}
and my LoginActivity.java:
package com.example.android.appName;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
public class LoginActivity extends AppCompatActivity {
private EditText usernameField = (EditText)findViewById(R.id.username),
passwordField = (EditText)findViewById(R.id.password);
private TextView error = (TextView)findViewById(R.id.error);
private ProgressBar progress = (ProgressBar)findViewById(R.id.progress);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.login_menu, menu);
return true;
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (android.os.Build.VERSION.SDK_INT > 5
&& keyCode == KeyEvent.KEYCODE_BACK
&& event.getRepeatCount() == 0) {
onBackPressed();
return true;
}
return super.onKeyDown(keyCode, event);
}
public void exit(MenuItem item) {
finish();
}
public void signIn(View view) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
Part of AndroidManifest.xml:
<activity android:name=".SplashActivity"
android:theme="#style/NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login" />
Error in logcat:
04-16 23:24:16.124 4015-4015/com.example.android.appName E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.appName, PID: 4015
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.appName/com.example.android.appName.LoginActivity}: 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:2993)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3248)
at android.app.ActivityThread.access$1000(ActivityThread.java:197)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1681)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6872)
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:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
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:145)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:42)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:186)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:168)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:508)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:180)
at com.example.android.appName.LoginActivity.<init>(LoginActivity.java:20)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1690)
at android.app.Instrumentation.newActivity(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2983)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3248) 
at android.app.ActivityThread.access$1000(ActivityThread.java:197) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1681) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:6872) 
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:1404) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
An Activity is not fully initialized and ready to look up views until after setContentView(...) is called in onCreate().
Only declare the fields like the following:
private EditText usernameField, passwordField;
private TextView error;
private ProgressBar progress;
and then assign the values in onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
usernameField = (EditText)findViewById(R.id.username);
passwordField = (EditText)findViewById(R.id.password);
error = (TextView)findViewById(R.id.error);
progress = (ProgressBar)findViewById(R.id.progress);
}
Might not be part of the problem but as an extra bit of advice a Timer runs the TimerTask on a background thread and that should be avoided in this case. Replace the Timer with a Handler instead to run it on the UI thread.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
}, 1500);
Views defined in xml file can only be accessed in java code, after setting the ContentView as the required xml file using:
setContentView(R.layout.xml_file_name);
So 1st call above method inside onCreate method and then initialize the View instances inside onCreate or inside the methods in which the instance will be used.

How to set an image to the button using the other application package name?

Im facing a problem as below stated.
I'm having a single button on the widget.On click of the button it open other application.Now my question is how to assign that open application image to my button which is in the widget.
Please help me.
This is my Main class.
{
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
public class WidgetMainProvider extends AppWidgetProvider {
// Activity a=new Activity();
public static String GET_ACTION = "Get_Action";
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (GET_ACTION.equals(intent.getAction())) {
Toast.makeText(context, "onReceiver()", Toast.LENGTH_LONG).show();
}
}
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
Log.i("ExampleWidget", "Updating widgets " + Arrays.asList(appWidgetIds));
// Perform this loop procedure for each App Widget that belongs to this
// provider
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
//Which defines the UI of Widget
// initializing widget layout
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget1);
Intent intent = new Intent(context, WidgetOneActivity.class);
// intent.setAction(GET_ACTION);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
remoteViews.setOnClickPendingIntent(R.id.button,pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}}}
This is my another class where i have declared the package name,based on that it will open the application.
package com.purvotara.example.widget;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
public class WidgetOneActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PackageInfo pkgInfo=new PackageInfo();
context.getPackageManager().getApplicationIcon(pkgInfo.packageName);
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.drdo.s.a.m.s");
startActivity(LaunchIntent);
}
}
And also please help me where to put the code which which will set image to the button dynamically.
Here is my Android. manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.eightbitcloud.example.widget"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/icon"
android:label="#string/app_name" >
<activity
android:name="com.purvotara.example.widget.WidgetOneActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.purvotara.example.widget.WidgetMainProvider"
android:label="#string/widget1name" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#XML/widget1_info" />
</receiver>
</application>
</manifest>
Please help me geeks,im waiting.
As I understand, you are trying to assign another app icon to your widget button. You can try:
PackageManager packageManager = mContext.getPackageManager();
Drawable icon = packageManager .getApplicationIcon("com.example.yourapp");
button.setBackgroundDrawable(icon);
or
Drawable icon = getPackageManager().getApplicationIcon("com.example.yourapp");
button.setBackgroundDrawable(icon);
I added the following in the WidgetOneActivity class:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = null;
//YOU NEED TO INITIALIZE YOUR BUTTON HERE, YOUR BUTTON IS NULL
try {
Drawable icon;
icon = getPackageManager().getApplicationIcon("com.drdo.s.a.m.s");
button.setBackground(icon);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.drdo.s.a.m.s");
startActivity(LaunchIntent);
}
});
}
After adding this my app is closing.

PreferenceActivity and BroadcastReceiver - Implement dynamic preferences

I'm going by the following code example to dynamically build a preference activity.
http://www.linuxtopia.org/online_books/android/devguide/guide/samples/ApiDemos/src/com/example/android/apis/app/PreferencesFromCode.html
The preference dialog shows, but I'm not able to see any changes after closing it.
Here's where I'm defining the activity in AndroidManifest.xml
<activity
android:name="PreferencesActivity" android:label="#string/preferences_name">
</activity>
Here's where I'm defining the receiver.
<receiver
android:name="FroyVisualReceiver"
android:label="#string/app_name"
android:exported="false">
<intent-filter>
<action android:name="com.starlon.froyvisuals.PREFS_UPDATE"/>
</intent-filter>
</receiver>
And here's the BroadcastReceiver. I never see the "WTF" in logcat. What am I doing wrong?
package com.starlon.froyvisuals;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.Context;
import android.util.Log;
public class FroyVisualsReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Log.e("WTFWTF", "WTFWTFWTFW");
String action = intent.getAction();
if(action.equals("com.starlon.froyvisuals.PREFS_UPDATE"))
{
((FroyVisuals)context).updatePrefs();
}
}
}
Oh here's onPause where I'm broadcasting the PREFS_UPDATE intent.I do see the logcat message. This method is part of my PreferenceActivity.
/** another activity comes over this activity */
#Override
public void onPause()
{
Log.i(TAG, "onPause ================================ ");
super.onPause();
Intent i = new Intent(this, FroyVisualsReceiver.class);
i.setAction("com.starlon.froyvisuals.PREFS_UPDATE");
sendBroadcast(i);
}
Edit: I think it may have to do with this line. 'this' points to my PreferenceActivity.
Intent i = new Intent(this, FroyVisualsReceiver.class);
Try a simple Intent:
Intent i = new Intent();
i.setAction("com.starlon.froyvisuals.PREFS_UPDATE");
sendBroadcast(i);

Activity wont start with "startActivity();"

Here's my code for DroidArmoryActivity
package com.maxgenero.droidarmory;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class DroidArmoryActivity extends Activity implements View.OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.ibM4A1:
Intent intentM4A1 = new Intent("com.maxgenero.droidarmory.M4A1GUN");
startActivity(intentM4A1);
break;
}
}
}
It's not starting the java file (Activity) at all, no errors. Btw, the case is looking for an imageButton.
Here's my Manifest, at least the part you need:
<activity android:name=".M4a1"
android:label="#string/app_name"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="com.maxgenero.droidarmory.M4A1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
And the file name for the java file is M4a1.java. If you need more info let me know, thanks.
I dont see where you define a listener on your Button or your View that will be clicked to launch the second Activity ??
yourView.setOnClickListener(this);
the second thing is that you should add declare your activity on your manifest file on the tag like this :
<activity android:name="your.package.name.NameOfYourAcitivity" />
the last thing is : try to instantiate the intent like this :
this.startActivity(new Intent(this, SecondActivity.class));
Regards,
Instead of...
case R.id.ibM4A1:
Intent intentM4A1 = new Intent("com.maxgenero.droidarmory.M4A1GUN");
startActivity(intentM4A1);
Try
Intent intentM4A1 = new Intent(this, ACTIVITY_NAME.class);
startActivity(intentM4A1);
Also dont forget to call your setOnclickListener().

Categories

Resources