1

当我将一个活动设置为启动器时,它可以正常工作,但是当我从另一个活动启动此活动时,它会以显示方式正常打开,某些功能可以正常工作而有些不能?!对我来说很困惑。

基本上,如果我打开它,因为启动器数据可以通过串行发送和接收。但是,如果我从另一个活动中打开它,则绝对废话会通过串行发送,并且什么都没有返回。但是某些部分可以工作,例如建立串行连接?!

在启动活动中,这是打开我想要的活动的代码:

public void openTextTerminal(View view)
{
    Intent intent = new Intent(this, TextBoxActivity.class);
    startActivity(intent);      
}

这是清单:(我认为我什至不需要意图过滤器?!)

<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>

完整清单:

<?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>

我自己复制了一个程序中的代码(即使用一个活动来启动第二个活动而没有我的其他不相关的类)并且它工作正常。我只是复制和粘贴。我对所有文件进行了比较,据我所知,实际上没有什么不同,只是清单中不相关的东西。明天我将检查整个清单(我花了一整天的时间调试只是为了找到这个错误)并重写它或其他东西。

我只是好奇如何看起来一切正常,但事实并非如此。我会认为一旦我启动了活动,无论我如何启动它,一切都是一样的(因为我什么都没传递)。

可能传递了错误的上下文,这是完整的代码:

public class MainActivity extends Activity implements OnClickListener,       OnItemSelectedListener, AdapterConnectionListener, DataListener{


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 USB2SerialAdapter mSelectedAdapter;
private TextView mCurrentSettings;

private Button mUpdateSettings;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);

    mConnect = (Button)findViewById(R.id.deviceConnect);
    mConnect.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);


    SlickUSB2Serial.initialize(this);



}

public void openTerminal(View view) {
    // Do something in response to button
    Intent intent = new Intent(this, Term.class);
    startActivity(intent);
}

public void openTextTerminal(View view) {
    // Do something in response to button
    Intent intent = new Intent(this, TextBoxActivity.class);
    startActivity(intent);

}

public void openSerialTerminal(View view) {
    // Do something in response to button
    Intent intent = new Intent(this, SerialTerminalActivity.class);
    startActivity(intent);

}

@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){
    Toast.makeText(this, "in changeselectedadapter", Toast.LENGTH_SHORT).show();
    //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();
}


@Override
public void onClick(View v) {

    if(v==mConnect){
        SlickUSB2Serial.autoConnect(this);
        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("");

            }

        Intent intent = new Intent(this, SerialTerminalActivity.class);
        startActivity(intent);
    }


    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(MainActivity.this); 
                    }
                else
                    {
                            SlickUSB2Serial.connectFTDI(MainActivity.this);     
                    }       
            }
        }).create();
        dialog.show();
        return;
    }
    Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}

private void updateCurrentSettingsText(){
    mCurrentSettings.setText("Current Settings Areeee:     "+mBaudSpinner.getSelectedItem().toString()
            +", "+mDataSpinner.getSelectedItem().toString()
            +", "+mParitySpinner.getSelectedItem().toString()
            +", "+mStopSpinner.getSelectedItem().toString());
    }

@Override
public void onDataReceived(int arg0, byte[] arg1) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "IN ONDATARECIEVED OHOH", Toast.LENGTH_SHORT).show();
}


public void onDestroy() {
    SlickUSB2Serial.cleanup(this);
    super.onDestroy();
}

}
4

2 回答 2

1

在这里,您已将用于上下文,首先您应该了解不同的上下文。

  • this指您当前的对象。在您的情况下,您必须在内部类或某些 ClickEvent 中实现了意图,这就是它所指向的。

  • Activity.this指向您当前所在的 Activity 实例。

  • getApplicationContext()指应用程序的上下文。

现在,如果this上下文直接在oncreate()Activity 的下方,而不是在任何其他类或某些按钮的onClick()事件中,那么它与 Activity 的上下文相同。

但是getApplicationContext()当同一个活动结束时,最好使用 ,因为活动的上下文消失了。

于 2012-12-13T11:53:05.973 回答
0

我在启动活动的 onCreate() 中找到了罪魁祸首,它是一个库调用。我所要做的就是将它从启动活动转移到被调用活动。这将教会我盲目地遵循 API 说明。

So I had SlickUSB2Serial.initialize(this); in the wrong activity.

initialize(android.content.Context context) 
initialize must be called when your app first starts up (in onCreate).
于 2012-12-13T11:55:48.743 回答