我正在制作时间表应用程序。并发生此错误
我创建了两个活动。一个是Main_activity,另一个是Popup_Activity。我想将数据从Popup_Activity发送到Main_Activity。
我创建了微调器以选择上课时间
public class PopupActivity extends Activity {
TextView textView,lecture,room,time;
Button decide;
EditText enterroom, enterlectrue;
Spinner spinner;
int item;
ClassTime CT = new ClassTime();
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.popup_activity);
textView = (TextView) findViewById(R.id.setlecture);
lecture = (TextView) findViewById(R.id.lecture) ;
time = (TextView) findViewById(R.id.time);
room = (TextView) findViewById(R.id.room);
enterroom = (EditText) findViewById(R.id.enterroom);
enterlectrue = (EditText) findViewById(R.id.enterlecture);
decide = (Button) findViewById(R.id.decide);
spinner = (Spinner) findViewById(R.id.timemenu);
ArrayAdapter <String> adapter = new ArrayAdapter <String>
(this, R.layout.support_simple_spinner_dropdown_item, CT.getTimeList());
spinner.setAdapter(adapter);
}
并且我已经创建了ClassTime类。
public class ClassTime {
private final static Timetable[] TIMES = {
new Timetable("Mon1",R.id.mon1),
new Timetable( "Mon2",R.id.mon2), ...
};
private final static Map<String,Integer> TIME_MAP = new HashMap<>();
private final static String[] TIME_LIST;
static {
for (Timetable r: TIMES) {
TIME_MAP.put(r.getTimemenu(),r.gettime());
}
TIME_LIST = new String[TIME_MAP.size()];
TIME_MAP.keySet().toArray(TIME_LIST);
Arrays.sort(TIME_LIST);
}
public String[] getTimeList() { return TIME_LIST; }
public int getid(String id) { return TIME_MAP.get(id); }
}
我认为适配器有问题。因为没有这条线,应用程序会运行良好。但是,如果我插入了适配器,则只要按下按钮以调用pop_activity,应用程序就会关闭。
这是main_activity的一部分
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
select_time = (TextView) findViewById(R.id.first);
return_button = (Button) findViewById(R.id.popup);
setLayout();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode,resultCode,intent);
if (resultCode==RESULT_OK) {
int id = intent.getIntExtra("classtime",1);
if(requestCode ==1) {
if (intent == null) {
}
else
{
lecture = intent.getStringExtra("lecture");
classroom = intent.getStringExtra("classroom");
}
for (int i = 0; i < textViews.length; i++) {
if (idArray[i] == id)
select_time = (TextView) findViewById(idArray[i]);
}
select_time.setText(lecture);
}
}
}
我想从微调器中获取textview的ID。并使用lecture
和classroom
运行应用程序我应该更改什么?
**编辑
这是错误:
Binary XML file line #19: Failed to resolve attribute at index 1: TypedValue{t=0x2/d=0x7f020072 a=-1}
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 1: TypedValue{t=0x2/d=0x7f020072 a=-1}
at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:761)
at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:7060)
at android.view.ViewGroup$LayoutParams.<init>(ViewGroup.java:7012)
at android.view.ViewGroup.generateLayoutParams(ViewGroup.java:5951)
at android.view.LayoutInflater.inflate(LayoutInflater.java:505)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:380)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:371)
at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:194)
at android.widget.Spinner.onMeasure(Spinner.java:592)
at android.view.View.measure(View.java:19734)
at android.widget.TableRow.getColumnsWidths(TableRow.java:313)
at android.widget.TableLayout.findLargestCells(TableLayout.java:505)
at android.widget.TableLayout.measureVertical(TableLayout.java:470)
at android.widget.TableLayout.onMeasure(TableLayout.java:436)
at android.view.View.measure(View.java:19734)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:687)
at android.view.View.measure(View.java:19734)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2271)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1331)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1607)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
at android.view.Choreographer.doCallbacks(Choreographer.java:683)
at android.view.Choreographer.doFrame(Choreographer.java:619)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
** Popup_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#eeeeee"
android:orientation="vertical"
android:layout_width="300dp"
android:layout_height="wrap_content">
<TableLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:id="@+id/setlecture"
android:layout_width="76dp"
android:layout_height="53dp"
android:layout_span="2"
android:background="#ff7a00"
android:gravity="center"
android:text="Set Lecutre"
android:textColor="#fff"
android:textSize="22sp" />
</TableRow>
<TableRow>
<TextView
android:layout_height="match_parent"
android:text="Time"
android:id="@+id/time"
android:gravity="center"
android:textAlignment="center" />
<Spinner
android:id="@+id/timemenu"
android:layout_width="249dp"
android:layout_height="30dp" />
</TableRow>
<TableRow>
<TextView
android:text="Lecture"
android:id="@+id/lecture"
android:gravity="center"/>
<EditText
android:id="@+id/enterlecture"/>
</TableRow>
<TableRow>
<TextView
android:text="Room"
android:id="@+id/room"
android:gravity="center"/>
<EditText
android:id="@+id/enterroom"/>
</TableRow>
<TableRow>
<Button
android:layout_width="match_parent"
android:layout_height="53dp"
android:background="#00000000"
android:gravity="center"
android:onClick="mOnClose"
android:padding="16dp"
android:text="Return"
android:layout_span="2"
android:id="@+id/decide"
android:textColor="#ff7a00"
android:textSize="15sp" />
</TableRow>
</TableLayout>
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
之所以发生这种情况,是因为您的App主题不正确。在styles.xml
中将应用程序主题更改为“ Theme.AppCompat.Light.DarkActionBar”:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
和colors.xml
中的
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
和清单文件中
android:theme="@style/AppTheme"
这对我有用。 祝你有美好的一天!