在下面的代码中,我的audioRecord
对象没有初始化.我尝试将其移动到该onCreate
方法并使其成为全局的.我已记录状态,并返回一个值1
,表示可以使用.调试器表示startRecording
正在未初始化的对象上调用它.它还说它无法获得音频源.
为什么我会收到这些错误?
package com.tecmark;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class recorder extends Activity {
private Thread thread;
private boolean isRecording;
private AudioRecord recorder;
private FileOutputStream os;
private BufferedOutputStream bos;
private DataOutputStream dos;
private TextView text;
private int audioSource = MediaRecorder.AudioSource.MIC;
private int sampleRate = 22050;
private int channel = AudioFormat.CHANNEL_CONFIGURATION_MONO; …
Run Code Online (Sandbox Code Playgroud) 我有一个蓝牙设备已连接到我在4.4.2之前尝试的所有Android版本.现在,它没有连接Galaxy Tab 4或S3.Tab 3与4.1.2连接良好.问题似乎发生在AcceptThread
尝试初始化时BluetoothSocket
.我正在使用的代码基于sdk中的聊天示例.
我的接受代码
private class AcceptThread extends Thread {
// The local server socket
private BluetoothServerSocket mmServerSocket;
public boolean successInit = false;
public AcceptThread() {
closeAllConnections();
BluetoothServerSocket tmp = null;
// Create a new listening server socket
while(!successInit) {
try {
tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
successInit= true;
Log.i("TAG", "Socket Server Created");
}
catch(Exception e) {
Log.i("TAG", e.getMessage());
successInit = false;
}
}
mmServerSocket = tmp;
}
public void run() {
BluetoothSocket socket = null; …
Run Code Online (Sandbox Code Playgroud) 我的设备运行Android 5.1.1,我发现如果我使用
Toast.makeText(this, "This is a toast", Toast.LENGTH_SHORT).show();
Run Code Online (Sandbox Code Playgroud)
我懂了:
但如果我用getApplicationContext()
而不是this
,
Toast.makeText(getApplicationContext(), "This is a toast", Toast.LENGTH_SHORT).show();
Run Code Online (Sandbox Code Playgroud)
我懂了:
两者都是直接从活动中调用的.
为什么是这样?
android android-context android-toast android-5.1.1-lollipop
我是Android开发的新手,遇到了一个问题,我正在尝试创建一个在图库中显示的图像数组,当我点击图片时,它会显示底部的图片.当我运行应用程序时,它崩溃了.我能得到的任何帮助都会非常有帮助.并提前感谢.
我的问题是
NullPointerException
?谢谢
我的布局:
<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: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=".PicturesActivity" >
<Gallery
android:id="@+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp" />
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/trophykiss" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我的课:
public class PicturesActivity extends Activity {
Bitmap[] myImages = new Bitmap[] {
BitmapFactory.decodeResource(getResources(), R.drawable.champions),
BitmapFactory.decodeResource(getResources(), R.drawable.trophykiss),
BitmapFactory.decodeResource(getResources(), R.drawable.championstwo),
BitmapFactory.decodeResource(getResources(), R.drawable.trophies),
BitmapFactory.decodeResource(getResources(), R.drawable.culture),
BitmapFactory.decodeResource(getResources(), R.drawable.maintrophy),
BitmapFactory.decodeResource(getResources(), R.drawable.dive),
BitmapFactory.decodeResource(getResources(), R.drawable.naijamain),
BitmapFactory.decodeResource(getResources(), R.drawable.ethiopia),
BitmapFactory.decodeResource(getResources(), R.drawable.peru),
BitmapFactory.decodeResource(getResources(), R.drawable.funtime),
BitmapFactory.decodeResource(getResources(), R.drawable.skils),
BitmapFactory.decodeResource(getResources(), R.drawable.gabon),
BitmapFactory.decodeResource(getResources(), R.drawable.gambia),
BitmapFactory.decodeResource(getResources(), R.drawable.guinea)
};
private ImageView imageView; …
Run Code Online (Sandbox Code Playgroud) 我的应用程序的一部分通过蓝牙连接到设备,通常工作正常但偶尔它不会连接,我得到以下错误
03-11 10:29:20.328: E/BluetoothComService(8059): accept() failed
03-11 10:29:20.328: E/BluetoothComService(8059): java.io.IOException: Operation Canceled
03-11 10:29:20.328: E/BluetoothComService(8059): at android.bluetooth.BluetoothSocket.acceptNative(Native Method)
03-11 10:29:20.328: E/BluetoothComService(8059): at android.bluetooth.BluetoothSocket.accept(BluetoothSocket.java:316)
03-11 10:29:20.328: E/BluetoothComService(8059): at android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:105)
03-11 10:29:20.328: E/BluetoothComService(8059): at android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:91)
03-11 10:29:20.328: E/BluetoothComService(8059): at com.mypackage.name.bluetooth.BluetoothService$AcceptThread.run(BluetoothService.java:298)
Run Code Online (Sandbox Code Playgroud)
这是我得到例外的那一行
socket = mmServerSocket.accept();
Run Code Online (Sandbox Code Playgroud)
这是完整的AcceptThread
private class AcceptThread extends Thread {
// The local server socket
private BluetoothServerSocket mmServerSocket;
public boolean successInit = false;
public AcceptThread() {
closeAllConnections();
/*
* if(mmServerSocket != null) { try { mmServerSocket.close(); } catch
* (IOException e) …
Run Code Online (Sandbox Code Playgroud) 我设计了两个Android布局,应该按顺序显示.因此,第一个布局必须显示在页面顶部,第二个布局必须显示在页面底部.但是,以下代码仅显示第一个布局.我怎样才能显示第二个布局?
主要布局是
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/first_layout"/>
<include layout="@layout/second_layout"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
第一个xml布局是
<LinearLayout 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="net.simplifiedcoding.androidloginapp.UserProfile">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView3"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
第二个布局是
<LinearLayout 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:orientation="vertical"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:id="@+id/textView" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editTextName" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:id="@+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editTextAddress" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Insert"
android:onClick="insert"
android:id="@+id/button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewResult" …
Run Code Online (Sandbox Code Playgroud) 这是与主题相关的清单代码:
<activity
android:name="smartHomes.capstone.GeneralAndSecurity"
android:label="@string/title_activity_general_and_security"
android:parentActivityName="smartHomes.capstone.HomePage" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="smartHomes.capstone.HomePage" />
</activity>
Run Code Online (Sandbox Code Playgroud)
这是错误日志:
03-20 21:19:18.227: E/AndroidRuntime(787): FATAL EXCEPTION: main
03-20 21:19:18.227: E/AndroidRuntime(787): java.lang.IllegalArgumentException: Activity GeneralAndSecurity does not have a parent activity name specified. (Did you forget to add the android.support.PARENT_ACTIVITY <meta-data> element in your manifest?)
03-20 21:19:18.227: E/AndroidRuntime(787): at android.support.v4.app.NavUtils.navigateUpFromSameTask(NavUtils.java:177)
03-20 21:19:18.227: E/AndroidRuntime(787): at smartHomes.capstone.GeneralAndSecurity.onOptionsItemSelected(GeneralAndSecurity.java:41)
03-20 21:19:18.227: E/AndroidRuntime(787): at com.actionbarsherlock.app.SherlockActivity.onMenuItemSelected(SherlockActivity.java:208)
Run Code Online (Sandbox Code Playgroud)
我能否知道为什么返回按钮会给我一个错误?
我SharedPreferences
用来在本地保存服务器数据,当设备进入关闭状态时,值SharedPreferences
丢失,再次从服务器下载.
public void saveCollection(Context context)
{
SharedPreferences settings = context.getSharedPreferences(context.getString(R.string.restore_values), 0);
SharedPreferences.Editor e = settings.edit();
e.clear();
e.putStringSet(context.getString(R.string.collection), collection);
e.commit();
}
Run Code Online (Sandbox Code Playgroud)
我做过这个,但没有运气.
我希望在我的活动中显示50度,TextView
如下所示
我真的不知道如何做到这一点,通过谷歌搜索我找到了这个XML代码
android:text="50°"
Run Code Online (Sandbox Code Playgroud)
我不知道上面的代码是什么.
任何人都可以向我解释究竟是什么以及它是如何工作的.
我用ActionBar和2个标签创建了非常基本的Android应用程序.它运作正常.然后我添加了locale资源目录,res/values-de
并在里面添加了strings.xml
用德语翻译的字符串.我将Nexus7上的语言更改为德语,部署后无法启动应用程序.我在模拟器上遇到了同样的问题.
请问我可以帮助我在移动设备上使用一些非默认语言环境运行此应用程序吗?在我的情况下德国......
报告以下错误.
02-07 15:32:41.314: W/dalvikvm(16682): threadid=1: thread exiting with uncaught exception (group=0x40d82930)
02-07 15:32:41.324: E/AndroidRuntime(16682): FATAL EXCEPTION: main
02-07 15:32:41.324: E/AndroidRuntime(16682): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tabactionbar/com.example.tabactionbar.MainActivity}: java.lang.NullPointerException
02-07 15:32:41.324: E/AndroidRuntime(16682): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-07 15:32:41.324: E/AndroidRuntime(16682): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-07 15:32:41.324: E/AndroidRuntime(16682): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-07 15:32:41.324: E/AndroidRuntime(16682): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-07 15:32:41.324: E/AndroidRuntime(16682): at android.os.Handler.dispatchMessage(Handler.java:99)
02-07 15:32:41.324: E/AndroidRuntime(16682): at android.os.Looper.loop(Looper.java:137)
02-07 15:32:41.324: E/AndroidRuntime(16682): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-07 15:32:41.324: E/AndroidRuntime(16682): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 15:32:41.324: E/AndroidRuntime(16682): at java.lang.reflect.Method.invoke(Method.java:511)
02-07 …
Run Code Online (Sandbox Code Playgroud) android ×10
java ×2
android-xml ×1
bitmap ×1
bluetooth ×1
iobluetooth ×1
ioexception ×1
locale ×1
localization ×1
tabs ×1
xml ×1