Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Activity only works if it is the main activity?!

Options
  • 12-12-2012 5:11pm
    #1
    Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭


    When I set an activity to be the launcher it works fine, however when I launch this activity from another activity, it will open fine displaywise, some of the functionality works and some of it does not?! Very confusing for me.

    Basically if I open it as the Launcher data can be sent and received over serial. But if I open it from another activity instead absolute crap gets sent over serial and nothing is coming back. However some parts work such as establishing the serial connection?!

    Here is some code:

    In the launching activity this is the code to open the activity I want:
    public void openTextTerminal(View view)
        {
        	Intent intent = new Intent(this, TextBoxActivity.class);
        	startActivity(intent);    	
        }
    

    This is the manifest:
    (I don't think I even need the intent filter?!)
    <activity
                android:name="com.example.TextBoxActivity"
                android:label="@string/title_activity_text_box" >
                
                <intent-filter>
                    <action android:name="android.intent.action.TextBoxActivity" />
    
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
                
            </activity>
    

    Here is the code that it opens (TextBoxActivity.java)):
    I know it works fine.
    public class TextBoxActivity extends Activity implements
    		OnClickListener, AdapterConnectionListener,
    		USB2SerialAdapter.DataListener, OnItemSelectedListener {
    
    	private Spinner mBaudSpinner;
    	private Spinner mDataSpinner;
    	private Spinner mParitySpinner;
    	private Spinner mStopSpinner;
    	private Spinner mDeviceSpinner;
    	private Button mConnect;
    	private ArrayList<String> mDeviceOutputs;
    	private ArrayList<USB2SerialAdapter> mDeviceAdapters;
    	private ArrayAdapter<CharSequence> mDeviceSpinnerAdapter;
    	private EditText mSendBox;
    	private EditText mReceiveBox;
    	private USB2SerialAdapter mSelectedAdapter;
    	private TextView mCurrentSettings;
    	private Button mSend;
    	private Button mUpdateSettings;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		requestWindowFeature(Window.FEATURE_NO_TITLE);
    		setContentView(R.layout.activity_text_box);
    
    		mConnect = (Button) findViewById(R.id.deviceConnect);
    		mConnect.setOnClickListener(this);
    		mSend = (Button) findViewById(R.id.sendData);
    		mSend.setOnClickListener(this);
    		mUpdateSettings = (Button) findViewById(R.id.updateSettings);
    		mUpdateSettings.setOnClickListener(this);
    
    		mBaudSpinner = (Spinner) findViewById(R.id.baudSpinner);
    		ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
    				this, android.R.layout.simple_spinner_item);
    		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    		mBaudSpinner.setAdapter(adapter);
    		String[] tempArray = SlickUSB2Serial.BAUD_RATES;
    		for (int i = 0; i < tempArray.length; i++) {
    			adapter.add(tempArray[i]);
    		}
    		mBaudSpinner.setSelection(SlickUSB2Serial.BaudRate.BAUD_9600.ordinal());
    
    		mDataSpinner = (Spinner) findViewById(R.id.dataSpinner);
    		adapter = new ArrayAdapter<CharSequence>(this,
    				android.R.layout.simple_spinner_item);
    		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    		mDataSpinner.setAdapter(adapter);
    		tempArray = SlickUSB2Serial.DATA_BITS;
    		for (int i = 0; i < tempArray.length; i++) {
    			adapter.add(tempArray[i]);
    
    		}
    		mDataSpinner
    				.setSelection(SlickUSB2Serial.DataBits.DATA_8_BIT.ordinal());
    
    		mParitySpinner = (Spinner) findViewById(R.id.paritySpinner);
    		adapter = new ArrayAdapter<CharSequence>(this,
    				android.R.layout.simple_spinner_item);
    		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    		mParitySpinner.setAdapter(adapter);
    		tempArray = SlickUSB2Serial.PARITY_OPTIONS;
    		for (int i = 0; i < tempArray.length; i++) {
    			adapter.add(tempArray[i]);
    
    		}
    		mParitySpinner.setSelection(SlickUSB2Serial.ParityOption.PARITY_NONE
    				.ordinal());
    
    		mStopSpinner = (Spinner) findViewById(R.id.stopSpinner);
    		adapter = new ArrayAdapter<CharSequence>(this,
    				android.R.layout.simple_spinner_item);
    		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    		mStopSpinner.setAdapter(adapter);
    		tempArray = SlickUSB2Serial.STOP_BITS;
    		for (int i = 0; i < tempArray.length; i++) {
    			adapter.add(tempArray[i]);
    
    		}
    		mStopSpinner
    				.setSelection(SlickUSB2Serial.StopBits.STOP_1_BIT.ordinal());
    
    		mDeviceAdapters = new ArrayList<USB2SerialAdapter>();
    		mDeviceOutputs = new ArrayList<String>();
    
    		mDeviceSpinner = (Spinner) findViewById(R.id.deviceSpinner);
    		mDeviceSpinnerAdapter = new ArrayAdapter<CharSequence>(this,
    				android.R.layout.simple_spinner_item);
    		mDeviceSpinnerAdapter
    				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    		mDeviceSpinner.setAdapter(mDeviceSpinnerAdapter);
    		mDeviceSpinner.setOnItemSelectedListener(this);
    
    		mCurrentSettings = (TextView) findViewById(R.id.currentSettings);
    		mReceiveBox = (EditText) findViewById(R.id.outputBox);
    		mSendBox = (EditText) findViewById(R.id.inputBox);
    
    		//SlickUSB2Serial.initialize(this);
    	}
    
    	@Override
    	public void onClick(View v) {
    
    		if (v == mConnect) {
    			SlickUSB2Serial.autoConnect(this);
    		} else if (v == mSend) {
    			if (mSelectedAdapter == null) {
    				Toast.makeText(this, "no adapters detected", Toast.LENGTH_SHORT).show();
    				return;
    			}
    			String data = mSendBox.getText().toString() + "\r\n";
    			mSelectedAdapter.sendData(data.getBytes());
    			mSendBox.setText("");
    		} else if (v == mUpdateSettings) {
    			if (mSelectedAdapter == null) {
    				return;
    			}
    
    			mSelectedAdapter.setCommSettings(BaudRate.values()[mBaudSpinner
    					.getSelectedItemPosition()], DataBits.values()[mDataSpinner
    					.getSelectedItemPosition()],
    					ParityOption.values()[mParitySpinner
    							.getSelectedItemPosition()],
    					StopBits.values()[mStopSpinner.getSelectedItemPosition()]);
    
    			updateCurrentSettingsText();
    			Toast.makeText(this, "Updated Settings", Toast.LENGTH_SHORT).show();
    
    		}
    	}
    
    	@Override
    	public void onAdapterConnected(USB2SerialAdapter adapter) {
    		adapter.setDataListener(this);
    		mDeviceAdapters.add(adapter);
    		mDeviceOutputs.add("");
    		mDeviceSpinnerAdapter.add("" + adapter.getDeviceId());
    		mDeviceSpinner.setSelection(mDeviceSpinnerAdapter.getCount() - 1);
    
    		Toast.makeText(this,
    				"Adapter: " + adapter.getDeviceId() + " Connected!",
    				Toast.LENGTH_SHORT).show();
    		// Toast.makeText(this, "Baud: "+adapter.getBaudRate()+" Connected!",
    		// Toast.LENGTH_SHORT).show();
    	}
    
    	@Override
    	public void onAdapterConnectionError(int error, String msg) {
    		// TODO Auto-generated method stub
    		if (error == AdapterConnectionListener.ERROR_UNKNOWN_IDS) {
    			final AlertDialog dialog = new AlertDialog.Builder(this)
    					.setIcon(0)
    					.setTitle("Choose Adapter Type")
    					.setItems(new String[] { "Prolific", "FTDI" },
    							new DialogInterface.OnClickListener() {
    								public void onClick(DialogInterface dialog,
    										int optionSelected) {
    									if (optionSelected == 0) {
    										SlickUSB2Serial
    												.connectProlific(TextBoxActivity.this);
    									} else {
    										SlickUSB2Serial
    												.connectFTDI(TextBoxActivity.this);
    									}
    								}
    							}).create();
    			dialog.show();
    			return;
    		}
    		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    	}
    
    	@Override
    	public void onDataReceived(int id, byte[] data) {
    		// TODO Auto-generated method stub
    		final String ReceivedText = mReceiveBox.getText().toString() + " "
    				+ new String(data);
    		runOnUiThread(new Runnable() {
    			public void run() {
    				mReceiveBox.setText(ReceivedText);
    				mReceiveBox.setSelection(ReceivedText.length());
    
    			}
    		});
    	}
    
    	@Override
    	public void onItemSelected(AdapterView<?> parent, View view, int position,
    			long id) {
    		// TODO Auto-generated method stub
    		changeSelectedAdapter(mDeviceAdapters.get(position));
    	}
    
    	@Override
    	public void onNothingSelected(AdapterView<?> arg0) {
    		// TODO Auto-generated method stub
    	}
    
    	public void changeSelectedAdapter(USB2SerialAdapter adapter) {
    
    		if (mSelectedAdapter != null) {
    			mDeviceOutputs.set(mDeviceSpinnerAdapter
    					.getPosition(mSelectedAdapter.getDeviceId() + ""),
    					mReceiveBox.getText().toString());
    		}
    
    		mSelectedAdapter = adapter;
    		mBaudSpinner.setSelection(adapter.getBaudRate().ordinal());
    		mDataSpinner.setSelection(adapter.getDataBit().ordinal());
    		mParitySpinner.setSelection(adapter.getParityOption().ordinal());
    		mStopSpinner.setSelection(adapter.getStopBit().ordinal());
    
    		updateCurrentSettingsText();
    
    		mReceiveBox.setText(mDeviceOutputs.get(mDeviceSpinner
    				.getSelectedItemPosition()));
    		Toast.makeText(this,
    				"Adapter switched toooo: " + adapter.getDeviceId() + "!",
    				Toast.LENGTH_SHORT).show();
    	}
    
    	private void updateCurrentSettingsText() {
    		mCurrentSettings.setText("Current Settings Areeee: "
    				+ mBaudSpinner.getSelectedItem().toString() + ", "
    				+ mDataSpinner.getSelectedItem().toString() + ", "
    				+ mParitySpinner.getSelectedItem().toString() + ", "
    				+ mStopSpinner.getSelectedItem().toString());
    	}
    
    	public void onDestroy() {
    		SlickUSB2Serial.cleanup(this);
    		super.onDestroy();
    	}
    
    }
    

    Any ideas?!



    Full manifest if that helps...it has to be a problem with that. I just made a simpler version and everything works fine, so it makes no sense to me.
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example"
        android:installLocation="auto"
        android:versionCode="49"
        android:versionName="1.0.48" >
    
        <uses-sdk
            android:minSdkVersion="12"
            android:targetSdkVersion="12" />
    
        <uses-feature
            android:name="android.hardware.touchscreen"
            android:required="false" />
    
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.WAKE_LOCK" />
    
        <permission
            android:name="com.example.permission.RUN_SCRIPT"
            android:description="@string/permdesc_run_script"
            android:label="@string/perm_run_script"
            android:permissionGroup="android.permission-group.SYSTEM_TOOLS"
            android:protectionLevel="dangerous" />
        <permission
            android:name="com.example.permission.APPEND_TO_PATH"
            android:description="@string/permdesc_append_to_path"
            android:label="@string/perm_append_to_path"
            android:permissionGroup="android.permission-group.SYSTEM_TOOLS"
            android:protectionLevel="dangerous" />
        <permission
            android:name="com.example.permission.PREPEND_TO_PATH"
            android:description="@string/permdesc_prepend_to_path"
            android:label="@string/perm_prepend_to_path"
            android:permissionGroup="android.permission-group.SYSTEM_TOOLS"
            android:protectionLevel="dangerous" />
    
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/application_terminal" >
            <activity
                android:name="com.example.Term"
                android:configChanges="keyboard|keyboardHidden|orientation"
                android:launchMode="singleTask"
                android:theme="@style/Theme"
                android:windowSoftInputMode="adjustResize|stateAlwaysVisible" >
                <intent-filter>
                    <action android:name="android.intent.action.TERM" />
    
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
    
            <activity-alias
                android:name="com.example.TermInternal"
                android:exported="false"
                android:targetActivity="Term" >
                <intent-filter>
                    <action android:name="com.example.private.OPEN_NEW_WINDOW" />
    
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
                <intent-filter>
                    <action android:name="com.example.private.SWITCH_WINDOW" />
    
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity-alias>
    
            <activity
                android:name="com.example.RemoteInterface"
                android:excludeFromRecents="true" >
                <intent-filter>
                    <action android:name="com.example.OPEN_NEW_WINDOW" />
    
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
    
            <activity-alias
                android:name="com.example.RunScript"
                android:permission="com.example.permission.RUN_SCRIPT"
                android:targetActivity="RemoteInterface" >
                <intent-filter>
                    <action android:name="com.example.RUN_SCRIPT" />
    
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity-alias>
    
            <activity
                android:name="com.example.TermPreferences"
                android:label="@string/preferences" />
            <activity
                android:name="com.example.WindowList"
                android:label="@string/window_list" />
    
            <service android:name="com.example.TermService" />
    
            <activity
                android:name="com.example.MainActivity"
                android:label="@string/title_activity_main" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name="com.example.TextBoxActivity"
                android:label="@string/title_activity_text_box" >
                
                <intent-filter>
                    <action android:name="android.intent.action.TextBoxActivity" />
    
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
                
            </activity>
            <activity
                android:name="com.example.SerialTerminalActivity"
                android:label="@string/title_activity_serial_terminal"
                android:screenOrientation="landscape" >
            </activity>
        </application>
    
    </manifest>
    


Comments

  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Something to do with android context (as opposed to java context that we were talking about in another thread). When you open an activity you can pass it a context or something?

    Debug the hell out of it, surely you can find out what is different and is causing garbage?


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭Tar.Aldarion


    I would have thought it would be obvious yes, but I replicated the code in a program on it's own (ie using an activity to launch the second activity without my other unrelated classes) and it works fine. I just copied and pasted. I did a diff on all the files and there is practically nothing different, just unrelated things in the manifest as far as I can see. I'll just go through the whole manifest tomorrow (I spent a whole day debugging just to find this bug) and rewrite it or something.

    I was just curious as to how it can appear that everything is working normally but that it is not. I would have thought once I launch the activity everything is exactly the same no matter how I launch it (as I am passing nothing). Anyway as long as it's not just something immediately stupid I'm doing that's fine, I can find the error.

    Another thing that took me hours was figuring out putting a toast into a method can make that method not run, and not spit any errors.
    Don't remind me about yesterday, that was one of the stupidest of my stupid questions.


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Off the top of my head (without going through the code in detail) the only thing that comes to mind is that if you're invoking openTextTerminal from outside your activity, you should specifically pass it the context and reference that rather than this.

    Given that, I suspect this is not the issue, but it's interesting that the activity launches and you can make your connection, but the data sent is flawed and the response doesn't happen.

    How do you pass the data from one activity to another?


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭Tar.Aldarion


    OpenTextTerminal() is a method inside the activity MainActivity. If I pass it 'this' or 'MainActivity.this' in the intent the same thing happens. I am really confused about contexts so i really thought that was the problem. As in I don't know yet why I pass 'this' vs 'MainActivity.this' vs 'getApplication()' vs 'getApplicationContext()' vs 'getBaseContext() ' so I am sure I send around the wrong context a lot.

    I found the culprit in onCreate(), it was a library call. All I had to do was move it from the launching activity to the called activity. That will teach me to just follow the API instructions blindly. The garbage data gave it away. I turned on warnings and saw that the garbage data was getting sent back but my method was not being called to receive it.

    So I had "SlickUSB2Serial.initialize(this);" in the wrong activity.
    initialize(android.content.Context context) 
         [I] initialize must be called when your app first starts up (in onCreate).[/I]
    


Advertisement