gpt4 book ai didi

java - 每当执行应用程序时,它在 android 模拟器中显示为空白

转载 作者:行者123 更新时间:2023-11-30 03:52:46 27 4
gpt4 key购买 nike

下面我粘贴了应用程序的 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

list 文件

    <?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>

最佳答案

从您的代码来看,我将推测这一行导致了错误:

setContentView(R.id.selection);

无需更改您从中调用“选择” Activity 的 Activity 的 contentView。你应该仔细阅读 Intent 。我假设单击按钮时,您希望弹出新 Activity 及其内容。为此,请执行以下操作:

try{
iol.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent iolIntent=new Intent(FullscreenActivity.this,Selection.class);
startActivity(iolIntent);
}
});
}

同时将按钮的布局宽度和布局高度设置为“wrap_content”。明确的测量不会很好地工作,因为有许多可能的分辨率。

android:layout_height="wrap_content"
android:layout_width="wrap_content"

如果有更多错误,请发布新的 logcat。

我在你的 selection.java 中找到了这个:

setContentView(R.layout.selective);

这意味着您的 res/layout/文件夹中应该有一个 selective.xml 文件。

这个 selective.xml 文件应该包含 EditText 的定义。例如,

<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" />

您需要使用这段代码在 Activity 文件中引用它:

EditText ed=new EditText findViewById(R.id.k2_editText);

现在将显示 EditText 字段。

如果你想让你的 EditText 对象成为全局对象,在类中声明它,然后在设置内容 View 后初始化它。像这样,

public class Selection extends Activity {
EditText ed;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.selective);
EditText ed=new EditText findViewById(R.id.k2_editText);
}

确保您始终在创建 Activity 和设置内容 View 之后初始化小部件对象,否则编译器将不得不初始化尚未引用的内容。只有在设置内容 View 后才会引用布局文件。只有在设置布局文件后,您才能将对象初始化为布局文件中存在的任何内容。

如果您喜欢这个答案,请投上一票。

关于java - 每当执行应用程序时,它在 android 模拟器中显示为空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13901089/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com