我正在开发一个 Android 项目,该项目应该显示所有打印机并且必须在 Android 中进行打印。我在我的 android 项目中使用了 JAVAX
库系统库。
以下是我向应用程序添加库的方法:
Open project properties by right clicking project and selecting properties
Select JAVA build path on the left pane
Select Libraries tab from right side
Click add Library button
Select JRE system library and add required package.
主要 Activity java代码:
package com.example.codeandroidprinter;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
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.widget.Toast;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button print = (Button) findViewById(R.id.print);
print.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "ddd", Toast.LENGTH_SHORT).show();
try
{
String printernames="";
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
for (int i = 0; i < printServices.length; i++) {
PrintService printer = printServices[i];
printernames = printernames + printer.getName() + ",";
}
System.out.print(printernames);
}
catch(Exception e)
{
System.out.println(e);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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);
}
}
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: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.codeandroidprinter.MainActivity$PlaceholderFragment" >
<Button
android:id="@+id/print"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
当我点击按钮时,我得到的结果为
java.lang.NoClassDefFoundError: javax.print.PrintServiceLookup
请帮我解决这个问题。
最佳答案
I have used JAVAX library system library for my android project.
那是行不通的。 Android一般不会加载java
/javax
来自其他地方的类(class),正如塞尔文所说,javax.print
不是 Android 本身的一部分。
I am working on a android project which should display all the printer and have to take print in Android.
欢迎您使用the printing APIs added to Android 4.4 。在此之前,尽管各种第三方工具(例如 Google 云打印)为人们提供了连接特定打印机的各种方式,但 Android 中并不支持打印。
关于android - ANDROID 中的 java.lang.NoClassDefFoundError : javax. print.PrintServiceLookup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25285680/