Surface View not working - java

I am trying to set up a surface view for graphics but an error keeps showing up that I have to import SView when I already did. Everything I try never seems to work. Is anything missing in the following code? or is it that I forgot to add something.
Firstclass.java:
package com.XXX.XXX;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import com.mtprogramming.blockfight.SView;
SView ourView;
public class Singleplayer extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ourView = new SView(this);
setContentView(ourView);
}
}
SView:
package com.XXX.XXX;
public class SView extends SurfaceView implements Runnable{
public SView(Singleplayer singleplayer) {
// TODO Auto-generated constructor stub
}
public void run(){
}
}

Check the package for SView.java: com.XXX.XXX it intended as so?
If yes, use import com.XXX.XXX.Sview instead of import com.mtprogramming.blockfight.SView in Firstclass.java

Related

Disable screenshots in React Native

I know that you can not 100% stop the user from taking a screenshot if he insists to. But I read that you can still stop manual screenshots by setting LayoutParams.FLAG_SECURE in Java.
I tried adding it to my MainApplication file but getWindow() kept on throwing errors no matter what I do. So I moved that line of code to the MainActivity file and it worked without any errors.
Problem is, I can still normally take screenshots.
MainApplication:
package com.testapp;
import android.app.Activity;
import com.reactnativenavigation.NavigationApplication;
import com.facebook.react.modules.i18nmanager.I18nUtil;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import android.support.annotation.Nullable;
/* custom modules */
import com.oblador.vectoricons.VectorIconsPackage;
import org.pgsqlite.SQLitePluginPackage;
import com.learnium.RNDeviceInfo.RNDeviceInfo;
import java.util.Arrays;
import java.util.List;
public class MainApplication extends NavigationApplication {
#Override
public boolean isDebug() {
return BuildConfig.DEBUG;
}
#Nullable
#Override
public List<ReactPackage> createAdditionalReactPackages() {
return Arrays.<ReactPackage>asList(
new SQLitePluginPackage(),
new VectorIconsPackage(),
new RNDeviceInfo()
);
}
#Override
public void onCreate() {
super.onCreate();
I18nUtil sharedI18nUtilInstance = I18nUtil.getInstance();
sharedI18nUtilInstance.allowRTL(getApplicationContext(), false);
}
}
MainActivity:
package com.testapp;
import android.widget.ImageView;
import com.reactnativenavigation.controllers.SplashActivity;
import android.os.Bundle;
import android.view.WindowManager.LayoutParams;
public class MainActivity extends SplashActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE);
}
}
I did simply the following and it is working properly:
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE);
setContentView(R.layout.activity_main);
}
}

OnSharedPreferenceChangeListener Won't Work?

I am trying to change the application background color, and the font type via theme when a preference is changed in the prefs area.
So far I have the preferences working but when I put the listener in, it just does not get called. I am testing using toasts to see if it appears.
My Code:
package alertssystem.lsa13tafeproj.lsa13.resistorcalculator;
import android.app.ActionBar;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.widget.RelativeLayout;
import android.widget.Toast;
import java.util.prefs.PreferenceChangeEvent;
import java.util.prefs.PreferenceChangeListener;
public class Prefs extends PreferenceActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new PreferencesFragment()).commit();
}
#Override
public void onBackPressed()
{
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
public static class PreferencesFragment extends PreferenceFragment
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
}
There is no changeListener in above code as none of the solutions I found worked, can someone please tell me how I can implement this into this.
The user clicks on the ListPreference and it has the 2 options I have inserted, but nothing happens when new option is selected.
This works for me:
public class MyPreferenceFragment extends PreferenceFragment implements
OnSharedPreferenceChangeListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
PreferenceManager.getDefaultSharedPreferences(getActivity()).registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onDestroy() {
PreferenceManager.getDefaultSharedPreferences(getActivity()).unregisterOnSharedPreferenceChangeListener(this);
super.onDestroy();
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// Toast
}
}
Make sure you register your listener before changing the pref, and not unregistering it until you do.

How can make a relation between 2 class in one program?(android)

I must write a program with android which can find other ssid's and show them. I desinged it with 2 xml pages. I create an imagebutton in page2 and want to make a relation between imagebutton and searching method. It means i want to click on imagebutton and seaching method begin it's work and search ssid's and show them...
My problem is, I download my search method and because of that i can not recognize which method i must call on my setonclick method that i write for an imagebutton in second page? I try to create another class seperately for search method and call it from the second class of page2.
but i dont know how can i make a relation between these 2 calss(i mean the second and third class). Or i must write the method of searching and on click of my imagebutton in one class?
Thanks for your suggestion.this is the code that i was copy:
import java.util.Date;
import java.util.List;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class wifiScan extends Activity {
private class WifiReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context c, Intent intent) {
List<ScanResult> results = wifi.getScanResults();
Date tempDate=new Date();
String info=testNumber+" "+(tempDate.getTime()-testDate.
getTime()) +" "+results.size();
Log.i("wifiScan", info);
wifiText.setText(info);
testNumber++;
testDate=new Date();
wifi.startScan();
}
}
private TextView wifiText;
private WifiManager wifi;
private WifiReceiver receiver;
private Date testDate;
private static int testNumber=0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
testNumber=0;
wifiText = (TextView) findViewById(R.id.wifiText);
receiver=new WifiReceiver();
registerReceiver(receiver, new IntentFilte
(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
wifi =(WifiManager)getSystemService(Context.WIFI_SERVICE);
if(!wifi.isWifiEnabled()){
wifi.setWifiEnabled(true);
}
startScan();
}
#Override
public void onStop(){
super.onStop();
finish();
}
public void startScan(){
testDate=new Date();
wifi.startScan();
}
}
you_image_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(this, wifiScan.class);
getApplicationContext().startActivity(i);
}
});

FORCE CLOSE error in Android Emulator

I am learning android so I wrote this code just to toggle phone ringer mode. The code compiles with no problem, I made entry in Android Manifest, set content view to the required Layout but I run this app, I get Force close error. Can somebody tell me why Force Close errors occur so that in future I should be to figure out the problem myself.Here is the code:
package com.umer.practice2;
import android.R.bool;
import android.app.Activity;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;
public class RingerMode extends Activity implements View.OnClickListener {
ToggleButton tb;
ImageView Riv;
TextView tv;
AudioManager mRing;
boolean silent;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.ringermode);
tb.setOnClickListener(this);
InitializeShit();
mRing=(AudioManager) getSystemService(AUDIO_SERVICE);
}
private void InitializeShit() {
// TODO Auto-generated method stub
tb= (ToggleButton) findViewById(R.id.ringTB);
tv= (TextView) findViewById(R.id.ringTV);
Riv= (ImageView) findViewById(R.id.ringIV);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
checkMode();
if(silent)
{
Riv.setImageResource(R.drawable.mysplash);
}else
{
Riv.setImageResource(R.drawable.myscreen);
}
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
checkMode();
}
private void checkMode() {
// TODO Auto-generated method stub
int temp = mRing.getRingerMode();
if(temp==AudioManager.RINGER_MODE_SILENT)
{
tv.setText("Silent");
Riv.setImageResource(R.drawable.mysplash);
silent= true;
}else
if(temp==AudioManager.RINGER_MODE_NORMAL)
{
tv.setText("Normal");
Riv.setImageResource(R.drawable.myscreen);
silent= false;
}
}
Many Thanks
You need to take a look at the logcat to see what happens. See Logcat | Android Developers.
Find the stacktrace of the crash, which points to your problem. If you can't figure it out yourself, please copy/paste the logcat in your question.
In this very case, you are referencing tb before initializing it:
tb.setOnClickListener(this);
At this point, tb is still null, so a NullPointerException occurs. To resolve this, change your code like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.ringermode);
InitializeShit();
tb.setOnClickListener(this);
mRing=(AudioManager) getSystemService(AUDIO_SERVICE);
}
Also, I recommend using Java's conventions regarding methods and variable naming:
Classes start with a capital: e.g. MyClass
Variables start with a lowercase: e.g. myVariable
Methods start with a lowercase: e.g. myMethod()
This will save you from confusion later on.

Externalization/Serialization Not Working?

I am trying to pass a checklist object over an intent to the next activity. Here's the code:
CheckList Object (CheckList.java)
package com.test.serialization;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import android.content.Context;
import android.widget.TableLayout;
public class CheckList extends TableLayout implements Externalizable {
public String name;
public int number_of_rows;
public CheckList() {
super(SerializationActivity.context);
}
public CheckList(Context context) {
super(context);
}
#Override
public void readExternal(ObjectInput input) throws IOException, ClassNotFoundException {
// TODO Auto-generated method stub
}
#Override
public void writeExternal(ObjectOutput output) throws IOException {
// TODO Auto-generated method stub
}
}
Serialization Activity (SerializationActivity.java)
package com.test.serialization;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SerializationActivity extends Activity {
/** Called when the activity is first created. */
private SerializationActivity activity;
private CheckList checklist;
public static Context context;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this.getApplicationContext();
checklist = new CheckList(this.getApplicationContext());
checklist.name="asdasd";
checklist.number_of_rows= 5;
activity = this;
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(on_click_listener);
}
private OnClickListener on_click_listener = new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(activity, DeserializationActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("checklist", checklist);
intent.putExtra("checklist_bundle", bundle);
startActivity(intent);
}
};
}
package com.test.serialization;
import android.os.Bundle;
import android.util.Log;
public class DeserializationActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
try {
Bundle bundle = this.getIntent().getExtras();
Bundle checklist_bundle = bundle.getBundle("checklist_bundle");
CheckList checklist = (CheckList) checklist_bundle.getSerializable("checklist");
Log.d("LOG_TAG", checklist.name);
Log.d("LOG_TAG", checklist.number_of_rows);
} catch (Exception e) {
e.printStackTrace();
}
}
}
I know it's weird to call the the context from the Activity through the static way, but I can't figure how can i do so without it when trying to serialize a view object.
The problem here is that my checklist name and number of rows will be empty and 0.
how can i pass in the values correctly?
In order to serialize an object you need to serialize the whole object graph, i.e. the top object and all objects it refers to and so on. If you try to serialize system provided objects you might encounter some native (OS provided) objects which can't be serialized. That's why serializing system objects is mostly impossible.
For the purpose of sending Views around the Android OS (e.g. Via Intent), there is a special class that you should use: RemoteViews
RemoteViews is not a view, but rather a series of commands how to build a View. You can create it from XML layout resource, manipulate it. Since it implements Parcelable you can easily add it to Intent via intent.putExtra(name, remoteViews).
you are not doing anything in writeExternal method...
its better (in your case) to use serializable interface instead of externalizable ..
Externalizable is an interface which gives the programmer the option to externalize the object in consideration (since you didn't write anything to the stream its not passing an object across).
instead of Externalizable if you use Serializable things would get serialized automatically & things would start to work.

Categories