我是Java新手,所以我想道歉并请求您理解这个问题。 我正在尝试使用可以切换到其他活动的微调器。任何时候我运行模拟器,我得到一个不幸的是,您的应用程序已停止错误。我是否需要在我切换到的活动中添加除下面代码之外的其他内容?
public class LakeOptions extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lake);``
}
这是我的代码来自附加到xml的活动,其中包含我的微调器。我是否需要在xml spinner标签中添加任何内容?我再一次为自己的无知道歉。
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.counties, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
class SpinnerActivity extends Activity implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
switch(pos) {
case 0:
Intent j = new Intent ( this, JasperOptions.class);
startActivity(j);
break;
case 1:
Intent k = new Intent(this, LakeOptions.class);
startActivity(k);
break;
case 2:
Intent l = new Intent(this, LaporteOptions.class);
startActivity(l);
break;
case 3:
Intent n = new Intent(this, NewtonOptions.class);
startActivity(n);
break;
case 4:
Intent p = new Intent(this, PorterOptions.class);
startActivity(p);
break;
}
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我的logcat错误如下
03-28 18:24:28.320 1928-1928/com.helpfinder.app D/dalvikvm﹕ GC_FOR_ALLOC freed 65K, 5% free 3043K/3176K, paused 185ms, total 199ms
03-28 18:24:28.480 1928-1928/com.helpfinder.app I/dalvikvm-heap﹕ Grow heap (frag case) to 6.472MB for 3609872-byte allocation
03-28 18:24:28.940 1928-1937/com.helpfinder.app D/dalvikvm﹕ GC_FOR_ALLOC freed 2K, 3% free 6565K/6704K, paused 451ms, total 451ms
03-28 18:24:32.030 1928-1928/com.helpfinder.app D/gralloc_goldfish﹕ Emulator without GPU emulation detected.
03-28 18:24:47.750 1928-1928/com.helpfinder.app D/dalvikvm﹕ GC_FOR_ALLOC freed 66K, 2% free 6769K/6900K, paused 94ms, total 99ms
03-28 18:24:56.250 1928-1928/com.helpfinder.app I/Choreographer﹕ Skipped 139 frames! The application may be doing too much work on its main thread.
03-28 18:24:56.960 1928-1928/com.helpfinder.app W/InputEventReceiver﹕ Attempted to finish an input event but the input event receiver has already been disposed.
03-28 18:24:57.070 1928-1928/com.helpfinder.app W/InputEventReceiver﹕ Attempted to finish an input event but the input event receiver has already been disposed.
03-28 18:24:57.070 1928-1928/com.helpfinder.app W/ViewRootImpl﹕ Dropping event due to root view being removed: MotionEvent { action=ACTION_UP, id[0]=0, x[0]=181.0, y[0]=531.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=2456198, downTime=2456110, deviceId=0, source=0x1002 }
03-28 18:24:57.200 1928-1928/com.helpfinder.app W/InputEventReceiver﹕ Attempted to finish an input event but the input event receiver has already been disposed.
03-28 18:24:57.200 1928-1928/com.helpfinder.app W/ViewRootImpl﹕ Dropping event due to root view being removed: MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=181.0, y[0]=531.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=2456404, downTime=2456404, deviceId=0, source=0x1002 }
03-28 18:25:06.000 1928-1928/com.helpfinder.app I/Choreographer﹕ Skipped 57 frames! The application may be doing too much work on its main thread.
03-28 18:25:26.750 1928-1928/com.helpfinder.app I/Choreographer﹕ Skipped 55 frames! The application may be doing too much work on its main thread.
这是我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.helpfinder.app" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.helpfinder.app.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.helpfinder.app.JasperOptions"
android:label="JasperOptions">
</activity>
<activity
android:name="com.helpfinder.app.JasperFood"
android:label="JasperFood">
</activity>
<activity
android:name="com.helpfinder.app.JasperHousing"
android:label="JasperHousing">
</activity>
<activity
android:name="com.helpfinder.app.JasperAssistance"
android:label="JasperAssistance">
</activity>
<activity
android:name="com.helpfinder.app.JasperOtherServices"
android:label="JasperOtherServices">
</activity>
<activity
android:name="com.helpfinder.app.LakeOptions"
android:label="LakeOptions">
</activity>
<activity
android:name="com.helpfinder.app.LakeFood"
android:label="LakeFood">
</activity>
<activity
android:name="com.helpfinder.app.LakeHousing"
android:label="Lake County Homeless Shelters"
>
</activity>
<activity
android:name="com.helpfinder.app.LakeAssistance"
android:label="LakeAssistance">
</activity>
<activity
android:name="com.helpfinder.app.LakeOtherServices"
android:label="LakeOtherServices">
</activity>
<activity
android:name="com.helpfinder.app.LaporteOptions"
android:label="LaporteOptions">
</activity>
<activity
android:name="com.helpfinder.app.LaporteFood"
android:label="LaporteFood">
</activity>
<activity
android:name="com.helpfinder.app.LaporteHousing"
android:label="LaporteHousing">
</activity>
<activity
android:name="com.helpfinder.app.LaporteAssistance"
android:label="LaporteAssistance">
</activity>
<activity
android:name="com.helpfinder.app.LaporteOtherServices"
android:label="LaporteOtherServices">
</activity>
+
<activity
android:name="com.helpfinder.app.NewtonOptions"
android:label="NewtonOptions">
</activity>
<activity
android:name="com.helpfinder.app.NewtonFood"
android:label="NewtonFood">
</activity>
<activity
android:name="com.helpfinder.app.NewtonHousing"
android:label="NewtonHousing">
</activity>
<activity
android:name="com.helpfinder.app.NewtonAssistance"
android:label="NewtonAssistance">
</activity>
<activity
android:name="com.helpfinder.app.NewtonOtherServices"
android:label="NewtonOtherServices">
</activity>
<activity
android:name="com.helpfinder.app.PorterOptions"
android:label="PorterOptions">
</activity>
<activity
android:name="com.helpfinder.app.PorterFood"
android:label="PorterFood">
</activity>
<activity
android:name="com.helpfinder.app.PorterHousing"
android:label="PorterHousing">
</activity>
<activity
android:name="com.helpfinder.app.PorterAssistance"
android:label="PorterAssistance">
</activity>
<activity
android:name="com.helpfinder.app.PorterOtherServices"
android:label="PorterOtherServices">
</activity>
</application>
</manifest>
答案 0 :(得分:1)
试试这个..
将第一个元素从字符串数组更改为选择
<string-array name="counties">
<item name="id">Select</item>
so on..
在用户选择微调器后,您可以在onCreate
spinner.setSelection(0);
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int pos, long arg3) {
// TODO Auto-generated method stub
switch(pos) {
case 1:
Intent j = new Intent ( this, JasperOptions.class);
startActivity(j);
break;
case 2:
Intent k = new Intent(this, LakeOptions.class);
startActivity(k);
break;
case 3:
Intent l = new Intent(this, LaporteOptions.class);
startActivity(l);
break;
case 4:
Intent n = new Intent(this, NewtonOptions.class);
startActivity(n);
break;
case 5:
Intent p = new Intent(this, PorterOptions.class);
startActivity(p);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
答案 1 :(得分:0)
这样做
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.counties, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
switch (pos) {
case 0:
Intent j = new Intent(this, JasperOptions.class);
startActivity(j);
break;
case 1:
Intent k = new Intent(this, LakeOptions.class);
startActivity(k);
break;
case 2:
Intent l = new Intent(this, LaporteOptions.class);
startActivity(l);
break;
case 3:
Intent n = new Intent(this, NewtonOptions.class);
startActivity(n);
break;
case 4:
Intent p = new Intent(this, PorterOptions.class);
startActivity(p);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
}