当我在AVD中运行我的应用程序时,我一直收到此错误。
你应该知道的事情:
我是一名中级程序员(仍在学习)所以请帮助我理解你的答案。 : - )
我尝试过很多方法来解决这个问题:
public class InputPage extends Activity {
更改为public class InputPage extends AppCompatActivity {
。 Android清单文件中还有一个错误,表示.InputPage
activity没有默认构造函数。
希望有所帮助。
这是java代码
package com.example.***************;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.Button;
import android.content.Intent;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
requestWindowFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_main);
}
public void toInputScreen(View view){
Button addButton = (Button)findViewById(R.id.addButton);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1 = new Intent(MainActivity.this, InputPage.class);
startActivity(intent1);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.cohen.not_at_homes_app.MainActivity"
android:background="#ffffff">
<Button
android:id="@+id/addButton"
android:layout_width="150dp"
android:layout_height="48dp"
android:layout_marginBottom="16dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="8dp"
android:layout_marginStart="16dp"
android:layout_marginTop="449dp"
android:onClick="toInputScreen"
android:background="@drawable/buttons"
android:text="@string/ButtonAddButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<Button
android:id="@+id/deleteButton"
android:layout_width="148dp"
android:layout_height="47dp"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="449dp"
android:text="@string/ButtonDeleteButton"
android:background="@drawable/buttons"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<LinearLayout
android:layout_width="341dp"
android:layout_height="519dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:orientation="horizontal"
android:id="@+id/linearLayout"
android:clickable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="0.0">
</LinearLayout>
</android.support.constraint.ConstraintLayout>
上面的代码是我来自的活动(java,XML)。
定义的按钮应该启动下面的活动(java,XML)。
package com.example.*****.*****************;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class InputPage extends Activity {
public String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void name(String name) {
this.name = name;
name = getName();
}
public InputPage(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void toInputScreen2(View view) {
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent2 = new Intent(InputPage.this, InputPage2.class);
startActivity(intent2);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".InputPage"
android:background="@color/BackgroundGrey">
<TextView
android:id="@+id/mapNo"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:text="@string/mapNoTextBox"
android:textColor="@color/textColour"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.04"
app:layout_constraintVertical_bias="0.13" />
<TextView
android:id="@+id/inputPageTitle"
android:layout_width="178dp"
android:layout_height="21dp"
android:text="@string/inputPageTitle"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/textColour"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintVertical_bias="0.01999998" />
<EditText
android:id="@+id/noInput"
android:layout_width="48dp"
android:layout_height="39dp"
android:ems="10"
android:inputType="number"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintVertical_bias="0.13"
app:layout_constraintHorizontal_bias="0.51" />
<TextView
android:layout_width="150dp"
android:layout_height="38dp"
android:text="@string/location"
android:textColor="@color/textColour"
android:textSize="30sp"
android:textStyle="bold"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintHorizontal_bias="0.041"
app:layout_constraintVertical_bias="0.24" />
<EditText
android:id="@+id/location"
android:layout_width="177dp"
android:layout_height="44dp"
android:ems="10"
android:inputType="textPersonName"
android:hint="@string/hintText"
android:textColorHint="@color/hintTextColour"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.952"
app:layout_constraintVertical_bias="0.243" />
<EditText
android:id="@+id/dateBox"
android:layout_width="133dp"
android:layout_height="44dp"
android:ems="10"
android:inputType="date"
android:hint="@string/dateBoxHint"
android:textColorHint="@color/hintTextColour"
android:layout_marginLeft="61dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintHorizontal_bias="0.335"
app:layout_constraintVertical_bias="0.374" />
<TextView
android:layout_width="101dp"
android:layout_height="34dp"
android:text="@string/dateBox"
android:textColor="@color/textColour"
android:textStyle="bold"
android:textSize="30sp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.032"
app:layout_constraintVertical_bias="0.369" />
<Button
android:id="@+id/button2"
android:layout_width="165dp"
android:layout_height="58dp"
android:text="@string/continueButton"
android:background="@drawable/buttons"
android:layout_marginRight="8dp"
android:onClick="toInputScreen2"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintVertical_bias="0.778" />
</android.support.constraint.ConstraintLayout>
这是AndroidManifest文件。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cohen.not_at_homes_app">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@android:style/Theme.NoTitleBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".InputPage">
</activity>
<activity android:name=".InputPage2">
</activity>
</application>
</manifest>
这是build.gradle文件
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.example.cohen.notathomesapp"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile 'com.cuboid:cuboidcirclebutton:1.0.3'
}
请帮助,感谢所有信息。
答案 0 :(得分:1)
更改你的onCreate()
Button addButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inputPage );
addButton= (Button)findViewById(R.id.addButton);
}
答案 1 :(得分:0)
活动类应该有构造函数。你的InputPage活动类有一个构造函数,它接受一个String参数,删除它,一切都会正常工作。
public class InputPage extends Activity {
//.........Remove this constructor......
public InputPage(String name) {
this.name = name;
}
//.......................................
}
您仍然可以通过意图将数据传递给活动
示例 -
启动InputPage活动 -
String name="Jhon doe";
Intent intent = new Intent(context, InputPage.class);
intent.putExtra("key",name);
startActivity(intent)
在InputPage活动中检索此数据
public class InputPage extends Activity {
//...Other Methods....
@Override
protected void onCreate(bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.input_page);
//get this intent which started this activity
Intent intent = getIntent();
//get the data from the intent
String name = intent.getStringExtra("key");
}
//...Other Methods....
}