我是Android编程的新手。我正在关注Android开发者指南以开始使用Android。 尝试在模拟器上运行简单的Formular时,模拟器显示
“不幸的是,我的第一个应用程序停止了。”,
其中MyFormular
是应用程序的名称。问题是我从布局资源文件中创建了新布局,并尝试连接到新的loyout fror here
setContentView(R.layout.form_layout (the new layout is form_layout)
新布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="5">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Please submit your details"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="46dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:id="@+id/editText"
android:layout_marginTop="46dp"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="Name" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/editText2"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="46dp"
android:hint="Email" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/editText3"
android:layout_below="@+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="46dp"
android:hint="Password" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/editText4"
android:layout_below="@+id/editText3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="46dp"
android:hint="Confirm Password" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="5">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Terms & Conditions"
android:id="@+id/textView2"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="A Terms and Conditions can be used for both your "
android:id="@+id/textView3"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I agree"
android:id="@+id/checkBox"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="38dp"
android:layout_marginStart="38dp"
android:layout_marginBottom="38dp"
android:checked="false"
android:layout_marginRight="46dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Can't wait!Sign me up!"
android:id="@+id/button"
android:layout_alignTop="@+id/checkBox"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="85dp"
android:layout_marginEnd="75dp" />
</RelativeLayout>
</LinearLayout>
MainFormular.java
package com.example.melisa.myformular;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class MainFormular extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById (R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_formular, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
在android.app.Activity.performCreate(activity_main_formular
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main_formular" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
logcat的
日志是,
04-30 09:13:11.176 10175-10175 / com.example.melisa.myformular I / Process:发送信号。 PID:10175 SIG:9 04-30 10:03:45.336 10427-10427 / com.example.melisa.myformular W / System:ClassLoader引用未知路径:/data/app/com.example.melisa.myformular-1/lib/x86 04-30 10:03:47.008 10427-10427 / com.example.melisa.myformular W / System:ClassLoader引用未知路径:/data/app/com.example.melisa.myformular-1/lib/x86 04-30 10:03:47.112 10427-10427 / com.example.melisa.myformular W / art:在Android 4.1之前,方法android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter ,android.content.res.ColorStateList,android.graphics.PorterDuff $ Mode)会错误地覆盖android.graphics.drawable.Drawable中的package-private方法 04-30 10:03:47.128 10427-10427 / com.example.melisa.myformular D / AndroidRuntime:关闭虚拟机
04-30 10:03:47.128 10427-10427 / com.example.melisa.myformular E / AndroidRuntime:FATAL EXCEPTION:main 过程:com.example.melisa.myformular,PID:10427 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.melisa.myformular / com.example.melisa.myformular.MainFormular}:java.lang.NullPointerException:尝试调用虚方法'void android.support.design。在null对象引用上的widget.FloatingActionButton.setOnClickListener(android.view.View $ OnClickListener)' 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 在android.app.ActivityThread.-wrap11(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.support.design.widget.FloatingActionButton.setOnClickListener(android.view.View $ OnClickListener)' 在com.example.melisa.myformular.MainFormular.onCreate(MainFormular.java:22) 在android.app.Activity.performCreate(Activity.java:6237) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 在android.app.ActivityThread.-wrap11(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:0)
请删除此代码并尝试运行您的应用......
在你的布局中,FloatingActionButton视图不存在,你正试图绑定并点击事件......
FloatingActionButton fab = (FloatingActionButton) findViewById (R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
这也是......
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_formular, menu);
return true;
}
享受编码......