尝试创建一个应用程序,我正在尝试创建一个简单的转换器。当我运行模拟器时,应用程序很快崩溃,所以我不确定它为什么会崩溃。我允许我运行模拟器
以下是我在logcat中看到的内容:
07-09 20:28:10.220 17354-17354/com.example.adrew.convertmidterm E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.adrew.convertmidterm, PID: 17354
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.adrew.convertmidterm/com.example.adrew.convertmidterm.MainActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
at android.app.Activity.setContentView(Activity.java:2145)
at com.example.adrew.convertmidterm.MainActivity.onCreate(MainActivity.java:14)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference at com.example.adrew.convertmidterm.MainActivityFragment.onCreateView(MainActivityFragment.java:33)
at android.app.Fragment.performCreateView(Fragment.java:2053)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:870)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1045)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1147)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2116)
at android.app.Activity.onCreateView(Activity.java:5328)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
at android.app.Activity.setContentView(Activity.java:2145)
at com.example.adrew.convertmidterm.MainActivity.onCreate(MainActivity.java:14)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
07-09 20:29:02.966 17354-17367/com.example.adrew.convertmidterm W/art﹕ Suspending all threads took: 7.769ms
这是我的代码:
package com.example.adrew.convertmidterm;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.view.View.OnClickListener;
/**
* A placeholder fragment containing a simple view.
*/
public class MainActivityFragment extends Fragment {
public MainActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//return inflater.inflate(R.layout.fragment_main, container, false);
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
//all test
//original three without getView().
//final EditText editCentimeters = (EditText) findViewById(R.id.editCentimeters);
final EditText editCentimeters = (EditText) getView().findViewById(R.id.editCentimeters);
final EditText editInches = (EditText) getView().findViewById(R.id.editInches);
Button buttonConvert = (Button) getView().findViewById(R.id.buttonConvert);
//buttonConvert.setOnClickListener(new OnClickListener() {
buttonConvert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
double centimeters = Double.valueOf( editCentimeters.getText().toString());
double inches = centimeters * 0.393700787 ;
editInches.setText(String.valueOf(inches));
}
});
//end test
return rootView;
}
}
的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.adrew.convertmidterm" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Fragment_main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivityFragment">
<TextView
android:id="@+id/textCentimeters"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="34dp"
android:text="Centimeters:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textInches"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editCentimeters"
android:layout_marginTop="40dp"
android:layout_toLeftOf="@+id/editCentimeters"
android:text="Inches:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editInches"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textInches"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="number" />
<EditText
android:id="@+id/editCentimeters"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textCentimeters"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="number" />
<Button
android:id="@+id/buttonConvert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Convert" />
</RelativeLayout>
答案 0 :(得分:1)
您无法使用getView()
作为参考,因为您的View
将在return rootView;
中创建。
return rootView;
之后 getView()
。您收到NullPointerException
,因为它尚未创建。
您需要更改
final EditText editCentimeters = (EditText) getView().findViewById(R.id.editCentimeters);
final EditText editInches = (EditText) getView().findViewById(R.id.editInches);
Button buttonConvert = (Button) getView().findViewById(R.id.buttonConvert);
到
final EditText editCentimeters = (EditText) rootView.findViewById(R.id.editCentimeters);
final EditText editInches = (EditText) rootView.findViewById(R.id.editInches);
Button buttonConvert = (Button) rootView.findViewById(R.id.buttonConvert);