Java 不明确的导入类型';打印作业';:';android.print.printjob';或';android.printservice.printjob';android打印应用程序出错
我叫易卜拉欣。我8年前来到美国。我没有口音,我的声音很正常。从12岁起,我就对计算机维修和开发产生了兴趣。我第一次开始编写我高中一年级的代码是在去年。我正在为使用蓝牙打印机的人开发一个应用程序。 我在完成MainActivity.java页面时遇到了这个错误。 以下是我的mainactivity.java代码:Java 不明确的导入类型';打印作业';:';android.print.printjob';或';android.printservice.printjob';android打印应用程序出错,java,android,ambiguous,Java,Android,Ambiguous,我叫易卜拉欣。我8年前来到美国。我没有口音,我的声音很正常。从12岁起,我就对计算机维修和开发产生了兴趣。我第一次开始编写我高中一年级的代码是在去年。我正在为使用蓝牙打印机的人开发一个应用程序。 我在完成MainActivity.java页面时遇到了这个错误。 以下是我的mainactivity.java代码: package com.mycompany.printer; import android.app.*; import android.os.*; im
package com.mycompany.printer;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.bluetooth.*;
import android.print.*;
import android.printservice.*;
import android.view.View.*;
import android.print.PrintJob.*;
import java.awt.print.*;
import java.awt.*;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button connect= (Button) findViewById(R.id.connect);
connect.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View p1)
{
BluetoothAdapter ba = (BluetoothAdapter) getSystemService(BLUETOOTH_SERVICE);
ba.enable();
ba.startDiscovery();
}
});
Button disconnect=(Button) findViewById(R.id.disconnect);
disconnect.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View p1)
{
BluetoothAdapter ba = (BluetoothAdapter) getSystemService(BLUETOOTH_SERVICE);
ba.cancelDiscovery();
ba.disable();
}
});
Button print = (Button) findViewById(R.id.print);
print.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View p1)
{
PrintJob pj = (PrintJob) getSystemService(PRINT_SERVICE);
pj.start();
}
});
}
}
以下是我的main.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connect to printer"
android:id="@+id/connect"/>
<button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="disconnect from printer"
android:id="@+id/disconnect"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Print"
android:id="@+id/print"/>
</LinearLayout>
and finally, here is my R.java code:
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package com.mycompany.printer;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int connect=0x7f060000;
public static final int disconnect=0x7f060001;
public static final int print=0x7f060002;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
public static final class style {
public static final int AppTheme=0x7f050000;
}
}
我在导入类型“PrintJob”时遇到错误:“android.print.PrintJob”或“android.printservice.PrintJob”错误是说它不知道是使用android.print.PrintJob类还是使用android.printservice.PrintJob类。 在本例中,您将希望使用android.printservice.PrintJob,因为该类具有要调用的start()方法:
pj.start()代码>。
或者
将您导入的android.print.PrintJob替换为android.printservice.PrintJob。或
删除导入,然后在调用中声明完全限定名:
android.printservice.PrintJob pj=(android.printservice.PrintJob)getSystemService(PRINT_服务)
PrintJob pj=(PrintJob)getSystemService(PRINT_服务)
android.printservice.PrintManager pm=(PrintManager)getSystemService(PRINT_服务);
打印(字符串、打印文档适配器、属性) 欢迎来到苏伊卜拉欣。你应该把介绍放在你的个人资料页面上,而不是问题页面上。虽然这段代码可以回答问题,但提供关于这段代码为什么和/或如何回答问题的附加上下文会更好。谢谢你,但我放弃了这个项目。
Button print = (Button) findViewById(R.id.print);
print.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View p1)
{
PrintJob pj = (PrintJob) getSystemService(PRINT_SERVICE);
pj.start();
}