gpt4 book ai didi

Android 手电筒崩溃 mainactivity

转载 作者:行者123 更新时间:2023-12-02 06:38:45 27 4
gpt4 key购买 nike

大家好,我正在尝试为我的 Android 手机开发一个手电筒应用程序,但是当我运行它时出现错误。 GUI 有 2 个按钮:1 个用于开灯,另一个用于关灯

这是代码:

package com.example.flash;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;

public class Flash extends ActionBarActivity {


private final static String LOG_TAG = "FlashLight";

private Button mOnBtn;
private Button mOffBtn;

private Camera mCamera;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flash);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}

mOnBtn = (Button) findViewById(R.id.onButton);
mOnBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
processOnClick();
}
});

mOffBtn = (Button) findViewById(R.id.offButton);
mOffBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
processOffClick();
}
});
}

private void processOffClick(){
if( mCamera != null ){
Parameters params = mCamera.getParameters();
params.setFlashMode( Parameters.FLASH_MODE_OFF );
mCamera.setParameters( params );
}
}

private void processOnClick(){
if( mCamera != null ){
Parameters params = mCamera.getParameters();
params.setFlashMode( Parameters.FLASH_MODE_TORCH );
mCamera.setParameters( params );
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.flash, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_flash,
container, false);
return rootView;
}
}

}

我已在 androidmanifest.xml 中添加了权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.flash"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.flash.Flash"
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>

这里是 logcat

    08-19 12:58:54.426: E/AndroidRuntime(17639): FATAL EXCEPTION: main
08-19 12:58:54.426: E/AndroidRuntime(17639): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.flash/com.example.flash.Flash}: java.lang.NullPointerException
08-19 12:58:54.426: E/AndroidRuntime(17639): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-19 12:58:54.426: E/AndroidRuntime(17639): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-19 12:58:54.426: E/AndroidRuntime(17639): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-19 12:58:54.426: E/AndroidRuntime(17639): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-19 12:58:54.426: E/AndroidRuntime(17639): at android.os.Handler.dispatchMessage(Handler.java:99)
08-19 12:58:54.426: E/AndroidRuntime(17639): at android.os.Looper.loop(Looper.java:137)
08-19 12:58:54.426: E/AndroidRuntime(17639): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-19 12:58:54.426: E/AndroidRuntime(17639): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 12:58:54.426: E/AndroidRuntime(17639): at java.lang.reflect.Method.invoke(Method.java:525)
08-19 12:58:54.426: E/AndroidRuntime(17639): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-19 12:58:54.426: E/AndroidRuntime(17639): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-19 12:58:54.426: E/AndroidRuntime(17639): at dalvik.system.NativeStart.main(Native Method)
08-19 12:58:54.426: E/AndroidRuntime(17639): Caused by: java.lang.NullPointerException
08-19 12:58:54.426: E/AndroidRuntime(17639): at com.example.flash.Flash.onCreate(Flash.java:40)
08-19 12:58:54.426: E/AndroidRuntime(17639): at android.app.Activity.performCreate(Activity.java:5133)
08-19 12:58:54.426: E/AndroidRuntime(17639): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-19 12:58:54.426: E/AndroidRuntime(17639): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-19 12:58:54.426: E/AndroidRuntime(17639): ... 11 more

我不知道出了什么问题...请帮助我

fragment_flash.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:background="#4F4F4F"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.flash.Flash$PlaceholderFragment" >

<Button
android:id="@+id/onButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="93dp"
android:layout_marginTop="213dp"
android:text="On" />

<Button
android:id="@+id/offButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/onButton"
android:layout_alignBottom="@+id/onButton"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@+id/onButton"
android:text="Off" />

</RelativeLayout>

最佳答案

试试这个..

改变这个..

setContentView(R.layout.activity_flash);

setContentView(R.layout.fragment_flash);

因为 Button View 必须仅引用特定布局

并删除PlaceholderFragment.class

然后也删除

    if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}

编辑

内部processOnClick()方法

            Camera camera = Camera.open();
camera.startPreview();
Parameters params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();

关于Android 手电筒崩溃 mainactivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25381737/

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