<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true">
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways"/>
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/my_tab_layout"
android:fillViewport="false"
app:tabMode="fixed"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/my_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
这是我的代码我遇到了一些麻烦而且无法正常工作,我已经为它制作了一个主要内容,但错误显示可以帮助我并解释代码如何工作 khg
这些是错误
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class Guiii extends JFrame{
private JButton menu;
private JButton custom;
public Guiii(){
super ("The Title");
setLayout(new FlowLayout());
menu = new JButton("menu Button");
add (menu);
Icon b = new ImageIcon (getClass().getResource("button.png"));
Icon x = new ImageIcon (getClass().getResource("greenbutton.png"));
custom = new JButton("Custom",b);
custom.setRolloverIcon(x);
add (custom);
HandlerClass handler = new HandlerClass();
menu.addActionListener(handler);
custom.addActionListener(handler);
}
private class HandlerClass implements ActionListener{
public void actionPerformed(ActionEvent event){
JOptionPane.showMessageDialog(null, String.format("~s", event.getActionCommand()));
}
}
}
这是代码的其余部分,但必须在另一个文件中:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
at Guiii.<init>(Guiii.java:22)
at main.main(main.java:7)
答案 0 :(得分:1)
图标的构造函数会抛出NPE,因为getResource
会返回null
。
将图像放在与Guiii类相同的文件夹中。