我对数组的意图有疑问。我想将矩形数组发送到另一个包。我正在关注this tutorial。不幸的是,我对Intent仍然有疑问。所以我有课ActivityToSend
。我想将storePrintIMC
发送到另一个包的另一个类(SamplePreferenceActivity
)中。ActivityToSend
类:
public class ActivityToSend extends Activity {
private Activity activity;
private ArrayList<String[]> data;
private static LayoutInflater inflater = null;
public String[] inkLevels = new String[5];
View message;
String [][] storePrintIMC= new String [4][4];
public static final String ARRAYS_COUNT = "com.Status.App.ARRAYS_COUNT";
public static final String ARRAY_INDEX = "com.Status.App.ARRAY_INDEX";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final String data[][] = storePrintIMC;
Bundle bundle = new Bundle();
int count = data.length;
bundle.putInt(ARRAYS_COUNT, count);
for (int i = 0; i < count; i++)
{
bundle.putStringArray(ARRAY_INDEX + i, data[i]);
}
Intent intent = new Intent(this, SamplePreferenceActivity.class);
intent.putExtras(bundle);
startActivity(intent);
}
}
对于不同包中的第二个活动,我使用:
public class SamplePreferenceActivity extends PreferenceActivity {
private static final int DIALOG_READ_ME = 1;
public String[] inkLevels = new String[5];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
int count = bundle.getInt(ActivityToSend.ARRAYS_COUNT, 0);
ArrayList<String[]> arrays = new ArrayList<String[]>(count);
for (int i = 0; i < count; i++)
arrays.add(bundle.getStringArray(ActivityToSmartWatch.ARRAY_INDEX + i));
String[][] data = arrays.toArray(new String[][]{});
}
}
}
我还对Android清单做了一些更改。对于第二个包,我输入:
<activity android:name=".SamplePreferenceActivity " android:label="@string/preference_activity_title">
<meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.PrinterStatus.AppLab.SamplePreferenceActivity " />
- <intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity android:name="com.sonyericsson.extras.liveware.extension.controlsample.SamplePreferenceActivity" android:parentActivityName="com.PrinterStatus.AppLab.ActivityToSend" />
<service android:name="com.sonyericsson.extras.liveware.extension.controlsample.SampleExtensionService" />
我在第一个程序包中收到一个错误:
Intent intent = new Intent(this, SamplePreferenceActivity.class);
我不知道我要改变什么。你有什么主意吗?
最佳答案
由于SamplePreferenceActivity
在不同的程序包中,因此您需要将该类导入到ActivityToSend
中。就像任何其他导入一样。从清单中的内容来看,您需要将其放在com.PrinterStatus.AppLab.ActivityToSend.java
的顶部附近(以及其他import
语句):
import com.sonyericsson.extras.liveware.extension.controlsample.SamplePreferenceActivity;
如果使用的是Eclipse,则可以使用ctrl-shift-O自动解决导入问题。