我创建了我的第一个Android应用程序,我有4个页面,我已经设置了一个页面作为菜单,它有3个按钮在它之间切换页面。但是我似乎在我的代码中出现错误,如果有人可以看一看,看看我在哪里出错。
这是我的菜单页面( activity_main )
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ifmdb.MainActivity"
tools:ignore="MergeRootFrame,HardcodedText" >
<TextView
android:id="@+id/txtMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_gravity="center"
android:text="@string/please_click_on_the_options_available"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/btnDOCare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/txtDOCare"
android:layout_below="@+id/txtDOCare"
android:layout_marginTop="30dp"
android:layout_gravity="center_vertical"
android:onClick="openDOCare"
android:text="@string/duty_of_care" />
<TextView
android:id="@+id/txtDOCare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/btnDock"
android:layout_marginLeft="202dp"
android:layout_marginTop="44dp"
android:layout_gravity="center"
android:text="@string/click_for_duty_of_care"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/txtVeCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtDOCare"
android:layout_below="@+id/txtMain"
android:layout_marginTop="36dp"
android:text="@string/click_for_vehicle_checklist"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/btnCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btnDOCare"
android:layout_below="@+id/txtVeCheck"
android:layout_marginTop="37dp"
android:onClick="openChecklist"
android:text="@string/checklist" />
<TextView
android:id="@+id/txtDock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/txtDOCare"
android:layout_below="@+id/btnCheck"
android:layout_marginTop="36dp"
android:text="@string/click_for_docket"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/btnDock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/btnCheck"
android:layout_below="@+id/txtDock"
android:layout_marginTop="38dp"
android:onClick="openDocket"
android:text="@string/docket" />
</RelativeLayout>
这是我的Java类(主要活动)
package com.example.ifmdb;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnHome = (Button)findViewById(R.id.btnHome);
btnHome.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(),
startActivityForResult(myIntent, 0);
}
});
}
}
这是我的第二页(核对清单)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ifmdb.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/vehicle_safety_checklist"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/please_check_each_box"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.58" >
<TextView
android:id="@+id/txtInt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="@string/interior"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/txtEx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/txtInt"
android:layout_alignBottom="@+id/txtInt"
android:layout_alignParentRight="true"
android:layout_marginRight="113dp"
android:text="@string/exterior"
android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckBox
android:id="@+id/cboxFLev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/txtInt"
android:text="@string/fuel_level" />
<CheckBox
android:id="@+id/cboxTires"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/cboxFLev"
android:layout_alignBottom="@+id/cboxFLev"
android:layout_alignLeft="@+id/txtEx"
android:text="@string/tires_wheels" />
<CheckBox
android:id="@+id/cboxWWash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/cboxFLev"
android:text="@string/windscreen_washer" />
<CheckBox
android:id="@+id/cboxExhaust"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/cboxWWash"
android:layout_alignBottom="@+id/cboxWWash"
android:layout_alignLeft="@+id/cboxTires"
android:text="@string/exhaust" />
<CheckBox
android:id="@+id/cboxSWheel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/cboxWWash"
android:text="@string/steering_wheel" />
<CheckBox
android:id="@+id/cboxLights"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/cboxSWheel"
android:layout_alignBottom="@+id/cboxSWheel"
android:layout_alignLeft="@+id/cboxExhaust"
android:text="@string/lights_reflectors" />
<CheckBox
android:id="@+id/cboxBrakes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/cboxSWheel"
android:text="@string/brakes" />
<CheckBox
android:id="@+id/cboxELeaks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/cboxBrakes"
android:layout_alignBottom="@+id/cboxBrakes"
android:layout_alignLeft="@string/_id_cboxlights"
android:text="@string/exterior_leaks" />
<CheckBox
android:id="@+id/cboxClutch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/cboxBrakes"
android:text="@string/clutch" />
<CheckBox
android:id="@+id/cbocBody"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/cboxClutch"
android:layout_alignBottom="@+id/cboxClutch"
android:layout_alignLeft="@string/_id_cboxeleaks"
android:text="@string/body" />
<CheckBox
android:id="@+id/cboxHorn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/cboxClutch"
android:text="@string/horn" />
<TextView
android:id="@+id/txtEng"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/cbocBody"
android:layout_below="@+id/cboxHorn"
android:text="@string/engine"
android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckBox
android:id="@+id/cboxHeater"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/txtEng"
android:text="@string/heater" />
<CheckBox
android:id="@+id/cboxOil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/cboxExhaust"
android:layout_below="@+id/cboxHeater"
android:text="@string/oil_level" />
<CheckBox
android:id="@+id/cboxSBelt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/cboxOil"
android:layout_alignBottom="@+id/cboxOil"
android:layout_alignParentLeft="true"
android:text="@string/seat_belts" />
<CheckBox
android:id="@+id/cboxWLight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/cboxCool"
android:layout_alignBottom="@+id/cboxCool"
android:layout_alignParentLeft="true"
android:text="@string/warning_lights" />
<CheckBox
android:id="@+id/cboxBelt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/cboxCool"
android:layout_below="@+id/cboxCool"
android:text="@string/belts" />
<CheckBox
android:id="@+id/cboxMirrors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/cboxWLight"
android:text="@string/mirrors" />
<CheckBox
android:id="@+id/cboxLeaks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/cboxBelt"
android:layout_below="@+id/cboxBelt"
android:text="@string/engine_leaks" />
<CheckBox
android:id="@+id/cboxCool"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/cboxOil"
android:layout_below="@+id/cboxOil"
android:text="@string/coolant_level" />
<TextView
android:id="@+id/txtOther"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/cboxLeaks"
android:layout_alignParentLeft="true"
android:text="@string/other"
android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckBox
android:id="@+id/cboxScrew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtEng"
android:layout_below="@+id/cboxLeaks"
android:text="@string/loose_bolts_screws" />
<CheckBox
android:id="@+id/cboxWTri"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/cboxScrew"
android:layout_alignBottom="@+id/cboxScrew"
android:layout_alignParentLeft="true"
android:text="@string/warning_triangle" />
<CheckBox
android:id="@+id/cboxFire"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/cboxWTri"
android:text="@string/fire_extinguisher_first_aid_kit" />
<TextView
android:id="@+id/txtAddCom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="@string/additional_comments"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/txtAddComBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/cboxLights"
android:layout_below="@+id/txtAddCom"
android:layout_marginTop="29dp" />
<TextView
android:id="@+id/txtFault"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="27dp"
android:text="@string/in_the_event_of_serious_faults_do_not_drive_the_vehicle"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/txtDefects"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/txtAddComBox"
android:layout_marginTop="110dp"
android:text="@string/defects"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/txtDefectsBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/cboxScrew"
android:layout_below="@+id/txtDefects"
android:layout_marginTop="25dp" />
<Button
android:id="@+id/btnHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/cboxOil"
android:text="@string/home" />
</RelativeLayout>
</LinearLayout>
这是该页面的java类(清单)
package com.example.ifmdb;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Checklist extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.checklist);
Button btnCheck = (Button) findViewById(R.id.btnCheck);
btnCheck.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
}
我的代码行出错了
Intent myIntent = new Intent(view.getContext(),
startActivityForResult(myIntent, 0);
这是错误---
The constructor Intent(Context, void) is undefined
Syntax error, insert ")" to complete Variable Initializer
答案 0 :(得分:0)
试试此代码
public class MainActivity extends Activity {
private Activity mActivity = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActivity = this;
Button btnHome = (Button)findViewById(R.id.btnHome);
btnHome.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(mActivity.getApplicationContext(),Checklist.class);
mActivity.startActivityForResult(myIntent, 0);
}
});
}
}
答案 1 :(得分:0)
在代码中它缺少语法。
您可以尝试这种方式。希望它有所帮助。
btnHome.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(getActivity(),Checklist.class);
startActivity(myIntent);
}
});
答案 2 :(得分:0)
我修好了,你是对的Atul O Holic我错过了部分代码。
继承人的工作解决方案
package com.example.ifmdb;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnHome = (Button)findViewById(R.id.btnHome);
btnHome.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(),Checklist.class);
startActivityForResult(myIntent, 0);
}
});
}
}