Android问题 - findViewById和Intent在此活动中不起作用

时间:2016-01-19 22:28:10

标签: java android android-intent

我必须得到以下代码才能用于学校项目。 WeekActivity应该会在稍后显示一个时间表。

然而,有两个小错误: 由于某种原因“最终TableRow表=(TableRow)findViewById(R.id.tableRowDays);”无法在此课程中解决,但Android Studio不会给我任何关于可能出现问题的提示。

链接到其他Activity的最后一个方法中的小Intent也不起作用。 Android工作室说它无法解析Intent的构造函数? :?

如果你们中的任何人看到我没有看到的东西,我们将不胜感激。我被困在这里并且时间紧迫。 > _&LT ;;

public class WeekActivity extends AppCompatActivity {

List<DatensatzStundenplan> inhalte;
int anzahlTage = 5;
int anzahlStundenMax = 11;
StundenplanTabelle tabelle;
ScaleGestureDetector.SimpleOnScaleGestureListener scaleGestureDetector;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_week);

    scaleGestureDetector = new OnScaleGestureListenerWeek();
 //techotopia SimpleOnScaleGestureListener


    final TableRow table = (TableRow) findViewById(R.id.tableRowDays);

    TextView tv;
    tv = (TextView) table.findViewById(R.id.day1);
    tv.setText("Mo");

    tv = (TextView) table.findViewById(R.id.day2);
    tv.setText("Di");

    tv = (TextView) table.findViewById(R.id.day3);
    tv.setText("Mi");

    tv = (TextView) table.findViewById(R.id.day4);
    tv.setText("Do");

    tv = (TextView) table.findViewById(R.id.day5);
    tv.setText("Fr");

    tabelle = new StundenplanTabelle();
    System.out.println(tabelle.getRows());
    inhalte = tabelle.getRows();


    faecherEintragen(this);
}

private void faecherEintragen(Context context) {
    final ListView weekTable = (ListView) findViewById(R.id.weekTable);
    LayoutInflater li = LayoutInflater.from(context);

    for(int i = 0; i<anzahlStundenMax; i++){
        View tr = li.inflate(R.layout.table_row_subjects, null, false);

        TextView tv;
        tv = (TextView) tr.findViewById(R.id.cell_1);
        tv.setText(inhalte.get(i).getFachMo());

        tv = (TextView) tr.findViewById(R.id.cell_2);
        tv.setText(inhalte.get(i).getFachDi());

        tv = (TextView) tr.findViewById(R.id.cell_3);
        tv.setText(inhalte.get(i).getFachMi());

        tv = (TextView) tr.findViewById(R.id.cell_4);
        tv.setText(inhalte.get(i).getFachDo());

        tv = (TextView) tr.findViewById(R.id.cell_5);
        tv.setText(inhalte.get(i).getFachFr());

        weekTable.addView(tr);
    }
}

private void buttonPressed(){
    Intent i;
    i = new Intent(this, DayActivity.class);
    startActivity(i);
}

}

这是xml文件activity_week:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

<ScrollView android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <TableLayout
        android:id="@+id/tableDays"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <TableRow
        android:id="@+id/tableRowDays"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <TextView
            android:id="@+id/day1"
            android:textColor="#29088A"
            android:text="MO"
            android:textSize="@dimen/abc_text_size_large_material"
            android:width="80dp"
            />

        <TextView
            android:id="@+id/day2"
            android:textColor="#29088A"
            android:text="DI"
            android:textSize="@dimen/abc_text_size_large_material"
            android:width="80dp"
            />

        <TextView
            android:id="@+id/day3"
            android:textColor="#29088A"
            android:text="MI"
            android:textSize="@dimen/abc_text_size_large_material"
            android:width="80dp"
            />

        <TextView
            android:id="@+id/day4"
            android:textColor="#29088A"
            android:text="DO"
            android:textSize="@dimen/abc_text_size_large_material"
            android:width="80dp"
            />

        <TextView
            android:id="@+id/day5"
            android:textColor="#29088A"
            android:text="FR"
            android:textSize="22sp"
            android:width="80dp"
            />
        </TableRow>
    </TableLayout>

   <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/weekTable"
        android:layout_above="@+id/tableDays"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        />

        <!--        <TableLayout
                    android:id="@+id/weekTable"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                </TableLayout>-->

            </RelativeLayout>
        </ScrollView>
</RelativeLayout>

...这是table_row_subjects.xml,如果应该是相关的:

    <?xml version="1.0" encoding="UTF-8"?>
   <TableRow xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="?android:attr/listPreferredItemHeight"
       android:focusable="true"
       android:focusableInTouchMode="false"
       android:clickable="true"
       android:id="@+id/tableRowSubjects"
       >

    <TextView
        android:id="@+id/cell_1"
        android:width="80dp"
        android:text="TEST"
       />

    <TextView
        android:id="@+id/cell_2"
        android:width="80dp"
        />

    <TextView
        android:id="@+id/cell_3"
        android:width="80dp"
        />

    <TextView
        android:id="@+id/cell_4"
        android:width="80dp"
        />

    <TextView
        android:id="@+id/cell_5"
        android:width="80dp"
        />

   </TableRow>

...和清单,按要求:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="de.dbg.aylin.stundenplan" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".WeekActivity"
            android:label="WeekActivity"
            android:theme="@style/Theme.AppCompat.Light">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".DayActivity"
            android:label="DayActivity"
            android:theme="@style/Theme.AppCompat.Light"/>

    </application>

</manifest>

2 个答案:

答案 0 :(得分:0)

尝试在公共类WeekActivity下声明TextView tv。 您在Manifest文件中的DayActivity类需要如下所示:

<activity
android:name=".DayActivity"
android:label="DayActivity"
android:theme="@style/Theme.AppCompat.Light">
<intent-filter>
<action android:name="de.dbg.aylin.stundenplan.dayactivity" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

然后你就可以像这样来训练意图:

Intent i = new Intent("de.dbg.aylin.stundenplan.dayactivity");
startActivity(i);

答案 1 :(得分:0)

change your code to this..

final TableRow table = (TableRow) findViewById(R.id.tableRowSubjects);

    TextView tv;
    tv = (TextView) table.findViewById(R.id.cell_1);
    tv.setText("Mo");

    tv = (TextView) table.findViewById(R.id.cell_2);
    tv.setText("Di");

    tv = (TextView) table.findViewById(R.id.cell_3);
    tv.setText("Mi");

    tv = (TextView) table.findViewById(R.id.cell_4);
    tv.setText("Do");

    tv = (TextView) table.findViewById(R.id.cell_5);
    tv.setText("Fr");