下面我粘贴了应用程序的 java、xml 和 logcat 的副本。
这是我的java代码
package my.example.myproject;
import my.example.myproject.util.SystemUiHider;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
/**
* An example full-screen activity that shows and hides the system UI (i.e.
* status bar and navigation/system bar) with user interaction.
*
* @see SystemUiHider
*/
public class FullscreenActivity extends Activity {
/**
* Whether or not the system UI should be auto-hidden after
* {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
*/
private static final boolean AUTO_HIDE = true;
/**
* If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
* user interaction before hiding the system UI.
*/
private static final int AUTO_HIDE_DELAY_MILLIS = 3000;
/**
* If set, will toggle the system UI visibility upon interaction. Otherwise,
* will show the system UI visibility upon interaction.
*/
private static final boolean TOGGLE_ON_CLICK = true;
/**
* The flags to pass to {@link SystemUiHider#getInstance}.
*/
private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;
/**
* The instance of the {@link SystemUiHider} for this activity.
*/
private SystemUiHider mSystemUiHider;
Button iol,help,about;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
final View controlsView = findViewById(R.id.fullscreen_content_controls);
iol = (Button)findViewById(R.id.iolCalculation);
help = (Button)findViewById(R.id.sHelp);
about = (Button)findViewById(R.id.sAbout);
Thread iolthread=new Thread(){
public void run(){
try{
iol.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
setContentView(R.layout.selection);
}
});
Intent iolIntent=new Intent(FullscreenActivity.this,Selection.class);
startActivity(iolIntent);
}
finally{
finish();
}
}
};
iolthread.start();
// Set up an instance of SystemUiHider to control the system UI for
// this activity.
mSystemUiHider = SystemUiHider.getInstance(this, controlsView,
HIDER_FLAGS);
mSystemUiHider.setup();
mSystemUiHider
.setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
// Cached values.
int mControlsHeight;
int mShortAnimTime;
@Override
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public void onVisibilityChange(boolean visible) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
// If the ViewPropertyAnimator API is available
// (Honeycomb MR2 and later), use it to animate the
// in-layout UI controls at the bottom of the
// screen.
if (mControlsHeight == 0) {
mControlsHeight = controlsView.getHeight();
}
if (mShortAnimTime == 0) {
mShortAnimTime = getResources().getInteger(
android.R.integer.config_shortAnimTime);
}
controlsView
.animate()
.translationY(visible ? 0 : mControlsHeight)
.setDuration(mShortAnimTime);
} else {
// If the ViewPropertyAnimator APIs aren't
// available, simply show or hide the in-layout UI
// controls.
controlsView.setVisibility(visible ? View.VISIBLE
: View.GONE);
}
if (visible && AUTO_HIDE) {
// Schedule a hide().
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
}
});
// Set up the user interaction to manually show or hide the system UI.
controlsView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (TOGGLE_ON_CLICK) {
mSystemUiHider.toggle();
} else {
mSystemUiHider.show();
}
}
});
// Upon interacting with UI controls, delay any scheduled hide()
// operations to prevent the jarring behavior of controls going away
// while interacting with the UI.
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Trigger the initial hide() shortly after the activity has been
// created, to briefly hint to the user that UI controls
// are available.
delayedHide(100);
}
/**
* Touch listener to use for in-layout UI controls to delay hiding the
* system UI. This is to prevent the jarring behavior of controls going away
* while interacting with activity UI.
*/
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (AUTO_HIDE) {
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
return false;
}
};
Handler mHideHandler = new Handler();
Runnable mHideRunnable = new Runnable() {
@Override
public void run() {
mSystemUiHider.hide();
}
};
/**
* Schedules a call to hide() in [delay] milliseconds, canceling any
* previously scheduled calls.
*/
private void delayedHide(int delayMillis) {
mHideHandler.removeCallbacks(mHideRunnable);
mHideHandler.postDelayed(mHideRunnable, delayMillis);
}
}
XML
<FrameLayout 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:background="@color/black"
tools:context=".FullscreenActivity"
android:id="@+id/frame_layout">
<LinearLayout
android:id="@+id/fullscreen_content_controls"
style="?buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="@color/black_overlay"
android:orientation="horizontal"
tools:ignore="UselessParent" >
</LinearLayout>
<Button
style="@style/ButtonBarButton"
android:layout_width="222dp"
android:layout_height="62dp"
android:layout_marginLeft="45dp"
android:layout_marginTop="75dp"
android:background="@color/black"
android:paddingLeft="20dp"
android:id="@+id/iolCalculation"
android:text="@string/Button"
android:textColor="@color/WhiteSmoke"
android:textStyle="bold" />
<Button
style="@style/ButtonBarButton"
android:layout_width="222dp"
android:layout_height="62dp"
android:layout_marginLeft="45dp"
android:id="@+id/sHelp"
android:layout_marginTop="139dp"
android:background="@color/Black"
android:gravity="center"
android:paddingLeft="10dp"
android:text="@string/Button1"
android:textColor="@color/WhiteSmoke"
android:textStyle="bold" />
<Button
style="@style/ButtonBarButton"
android:layout_width="222dp"
android:layout_height="62dp"
android:id="@+id/sAbout"
android:layout_marginLeft="45dp"
android:layout_marginTop="203dp"
android:background="@color/Black"
android:paddingLeft="10dp"
android:text="@string/Button2"
android:textColor="@color/WhiteSmoke"
android:textStyle="bold" >
</Button>
</FrameLayout>
日志猫
12-16 10:48:26.975: E/Trace(767): error opening trace file: No such file or directory (2)
12-16 10:51:38.199: E/AndroidRuntime(767): FATAL EXCEPTION: main
12-16 10:51:38.199: E/AndroidRuntime(767): java.lang.NullPointerException
12-16 10:51:38.199: E/AndroidRuntime(767): at com.android.internal.widget.ActionBarView$3.onClick(ActionBarView.java:168)
12-16 10:51:38.199: E/AndroidRuntime(767): at android.view.View.performClick(View.java:4202)
12-16 10:51:38.199: E/AndroidRuntime(767): at android.view.View$PerformClick.run(View.java:17340)
12-16 10:51:38.199: E/AndroidRuntime(767): at android.os.Handler.handleCallback(Handler.java:725)
12-16 10:51:38.199: E/AndroidRuntime(767): at android.os.Handler.dispatchMessage(Handler.java:92)
12-16 10:51:38.199: E/AndroidRuntime(767): at android.os.Looper.loop(Looper.java:137)
12-16 10:51:38.199: E/AndroidRuntime(767): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-16 10:51:38.199: E/AndroidRuntime(767): at java.lang.reflect.Method.invokeNative(Native Method)
12-16 10:51:38.199: E/AndroidRuntime(767): at java.lang.reflect.Method.invoke(Method.java:511)
12-16 10:51:38.199: E/AndroidRuntime(767): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-16 10:51:38.199: E/AndroidRuntime(767): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-16 10:51:38.199: E/AndroidRuntime(767): at dalvik.system.NativeStart.main(Native Method)
我在stackoverflow中尝试了所有可能的解决方案,但徒劳无功。帮助我。我认为xml到java显示内容主要存在问题。我还需要为xml中的每个布局输入id以及java代码如何检测要显示的布局
这是我选择的java代码和xml
package com.example.iolcalci;
import android.app.Activity;
import android.os.Bundle;
public class Selection extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.selective);
}
}
<?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"
android:background="@color/black"
android:orientation="vertical" >
<Spinner
android:id="@+id/formulae"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:background="@color/LightSkyBlue"
android:prompt="@string/prompt"
android:entries="@array/formulas" />
<TextView
android:id="@+id/k2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/k1"
android:layout_below="@+id/k1"
android:layout_marginTop="38dp"
android:text="@string/K2"
android:textColor="@color/White"
android:textSize="25sp" />
<TextView
android:id="@+id/al_const"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/al"
android:layout_below="@+id/al"
android:layout_marginTop="44dp"
android:text="@string/Rx"
android:textColor="@color/White"
android:textSize="20sp" />
<TextView
android:id="@+id/al"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/k2"
android:layout_below="@+id/k2"
android:layout_marginTop="38dp"
android:text="@string/AL"
android:textColor="@color/White"
android:textSize="25sp" />
<EditText
android:id="@+id/k2_editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/al"
android:layout_alignRight="@+id/al_editText"
android:layout_alignTop="@+id/k2"
android:layout_marginLeft="120dp"
android:background="@color/black"
android:ems="10"
android:inputType="numberDecimal"
android:textColorLink="@color/white" />
<EditText
android:id="@+id/al_editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/formulae"
android:layout_alignTop="@+id/al"
android:layout_marginLeft="120dp"
android:ems="10"
android:inputType="numberDecimal" />
<EditText
android:id="@+id/al_const_editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/al_const"
android:layout_alignRight="@+id/al_editText"
android:layout_marginLeft="120dp"
android:ems="10"
android:inputType="numberDecimal" />
<Button
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@color/LightSkyBlue"
android:text="@string/Result" />
<EditText
android:id="@+id/k1_editText"
android:layout_width="wrap_content"
android:layout_height="37dp"
android:layout_alignLeft="@+id/k2_editText"
android:layout_alignRight="@+id/formulae"
android:layout_below="@+id/formulae"
android:layout_marginTop="30dp"
android:background="@color/black"
android:ems="10"
android:inputType="numberDecimal"
android:textColorLink="@color/White" />
<TextView
android:id="@+id/k1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/k1_editText"
android:layout_alignParentLeft="true"
android:layout_marginLeft="27dp"
android:text="@string/K1"
android:textColor="@color/white"
android:textSize="25sp" />
</RelativeLayout>
新的 LOGCAT
12-21 10:44:50.419: W/Trace(766): Unexpected value from nativeGetEnabledTags: 0
12-21 10:44:50.573: W/Trace(766): Unexpected value from nativeGetEnabledTags: 0
12-21 10:44:50.573: W/Trace(766): Unexpected value from nativeGetEnabledTags: 0
12-21 10:44:50.923: D/AndroidRuntime(766): Shutting down VM
12-21 10:44:50.923: W/dalvikvm(766): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
12-21 10:44:50.933: E/AndroidRuntime(766): FATAL EXCEPTION: main
12-21 10:44:50.933: E/AndroidRuntime(766): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.iolcalci/com.example.iolcalci.MainActivity}: java.lang.NullPointerException
12-21 10:44:50.933: E/AndroidRuntime(766): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-21 10:44:50.933: E/AndroidRuntime(766): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-21 10:44:50.933: E/AndroidRuntime(766): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-21 10:44:50.933: E/AndroidRuntime(766): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-21 10:44:50.933: E/AndroidRuntime(766): at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 10:44:50.933: E/AndroidRuntime(766): at android.os.Looper.loop(Looper.java:137)
12-21 10:44:50.933: E/AndroidRuntime(766): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-21 10:44:50.933: E/AndroidRuntime(766): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 10:44:50.933: E/AndroidRuntime(766): at java.lang.reflect.Method.invoke(Method.java:511)
12-21 10:44:50.933: E/AndroidRuntime(766): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-21 10:44:50.933: E/AndroidRuntime(766): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-21 10:44:50.933: E/AndroidRuntime(766): at dalvik.system.NativeStart.main(Native Method)
12-21 10:44:50.933: E/AndroidRuntime(766): Caused by: java.lang.NullPointerException
12-21 10:44:50.933: E/AndroidRuntime(766): at com.example.iolcalci.MainActivity.onCreate(MainActivity.java:23)
12-21 10:44:50.933: E/AndroidRuntime(766): at android.app.Activity.performCreate(Activity.java:5104)
12-21 10:44:50.933: E/AndroidRuntime(766): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-21 10:44:50.933: E/AndroidRuntime(766): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-21 10:44:50.933: E/AndroidRuntime(766): ... 11 more
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.iolcalci"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.iolcalci.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>
<activity
android:name="Selection"
android:exported="false"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name">
<intent-filter>
<action android:name="com.example.iolcalci.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>